Excel利用VBA列出工作表中的所有公式
时间:2022-07-31 02:23:53
如果我们要查看一个工作表中的所有公式,可以用VBA来实现。下面的VBA代码可以在工作簿中插入一个新工作表,并在其中列出指定工作表中的所有公式、公式所在单元格及其值。使用方法是将VBA代码放入标准模块中,选择一个工作表,然后执行代码。
Sub ListFormulas()
Dim FormulaCells As Range, Cell As Range
Dim FormulaSheet As Worksheet
Dim Row As Integer
'创建Range对象
On Error Resume Next
Set FormulaCells = Range("A1").SpecialCells(xlFormulas, 23)
'没有找到公式
If FormulaCells Is Nothing Then
MsgBox "当前工作表中没有公式!"
Exit Sub
End If
'增加一个新工作表
Application.ScreenUpdating = False
Set FormulaSheet = ActiveWorkbook.Worksheets.Add
FormulaSheet.Name = "“" & FormulaCells.Parent.Name & "”表中的公式"
'列标题
With FormulaSheet
Range("A1") = "公式所在单元格"
Range("B1") = "公式"
Range("C1") = "值"
Range("A1:C1").Font.Bold = True
End With
'读取公式,同时在状态栏中显示进度。
Row = 2
For Each Cell In FormulaCells
Application.StatusBar = Format((Row - 1) / FormulaCells.Count, "0%")
With FormulaSheet
Cells(Row, 1) = Cell.Address _
(RowAbsolute:=False, ColumnAbsolute:=False)
Cells(Row, 2) = " " & Cell.Formula
Cells(Row, 3) = Cell.Value
Row = Row + 1
End With
Next Cell
'调整列宽
FormulaSheet.Columns("A:C").AutoFit
Application.StatusBar = False
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
word文本框内部边距怎么设置
![](https://img.aspxhome.com/file/2023/8/32578_0s.png)
excel如何设置A4打印
如何在word中添加水印
Win10 1909更新后无法上网怎么办?Win10 1909无法上网解决方法
![](https://img.aspxhome.com/file/2023/4/48794_0s.jpg)
如何使用Excel 2013/2016中的查找和替换功能从日期和时间值中只删除日期的那部分
![](https://img.aspxhome.com/file/2023/2/a141332_0s.png)
如何实现word文档wps文档放大缩小文档显示?
![](https://img.aspxhome.com/file/2023/7/29027_0s.png)
Excel工程函数大全
![](https://img.aspxhome.com/file/2023/7/38277_0s.jpg)
excel如何批量匹配两张不同表格数据?(Vlookup函数法)
![](https://img.aspxhome.com/file/2023/9/a156079_0s.jpg)
excel表怎么自动分页后每页都有行头
做数据分析,这两个Excel功能非常有用
WORD里的剪贴板怎样使用
![](https://img.aspxhome.com/file/2023/5/34525_0s.gif)
word 如何调整图片颜色
![](https://img.aspxhome.com/file/2023/0/32940_0s.jpg)
在Word2010文档公式中添加数学符号
![](https://img.aspxhome.com/file/2023/5/29565_0s.jpg)
手把手教你学Excel第15课-Excel「查找与替换」对话框中的「查找」功能
![](https://img.aspxhome.com/file/2023/8/a154858_0s.jpg)
word人民币符号怎么打?Word输入人民币符号方法图解
![](https://img.aspxhome.com/file/2023/4/28844_0s.jpg)
excel表格中怎么制作环比增减的箭头?
![](https://img.aspxhome.com/file/2023/4/40714_0s.png)
Win10更新到一半时强制终止更新程序会怎么样?
![](https://img.aspxhome.com/file/2023/4/51094_0s.jpg)
word怎么添加柱状图呢?
![](https://img.aspxhome.com/file/2023/5/23645_0s.jpg)
excel表格如何删除重复数据
如何使用Excel过滤器和高级过滤器?(13笔画筛选和使用大全)
![](https://img.aspxhome.com/file/2023/2/a143312_0s.png)