生成所有excel单元格公式的平面列表
作者:互联网
我有一个用VBA和单元格公式编写的大型程序.我的任务是将其反向工程为C#winforms.我想从头开始,我需要在平面列表中查看所有单元格公式.
有现成的方法吗?提前致谢!
编辑:只是为了分享,在回答者的帮助下,我设法提出了这个建议:
解决方法:
在VBA中(很容易修改为vbscript),您可以使用高效的变量数组将所有工作表中的所有公式快速转储到平面txt文件中(更改路径以适合). code sourced from my article here
Const sFilePath = "C:\test\myfile.txt"
Sub CreateTxt_Output()
Dim ws As Worksheet
Dim rng1 As Range
Dim X
Dim lRow As Long
Dim lCol As Long
Dim strTmp As String
Dim lFnum As Long
lFnum = FreeFile
Open sFilePath For Output As lFnum
For Each ws In ActiveWorkbook.Worksheets
Print #lFnum, "*****" & ws.Name & "*****"
'test that sheet has been used
Set rng1 = ws.UsedRange
If Not rng1 Is Nothing Then
'only multi-cell ranges can be written to a 2D array
If rng1.Cells.Count > 1 Then
X = ws.UsedRange.Formula
For lRow = 1 To UBound(X, 1)
For lCol = 1 To UBound(X, 2)
'write each line to txt file
Print #lFnum, X(lRow, lCol)
Next lCol
Next lRow
Else
Print #lFnum, rng1.Formula
End If
End If
Next ws
Close lFnum
MsgBox "Done!", vbOKOnly
End Sub
[更新的部分-您可以使用SpecialCells在VBA中快速隔离公式.如果工作表上没有公式,则需要进行错误处理,请参见下面的GetFormula
Sub GetFormula()
Dim ws As Worksheet
Dim rng1 As Range
Dim rng2 As Range
For Each ws In ActiveWorkbook.Sheets
Set rng1 = Nothing
On Error Resume Next
Set rng1 = ws.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not rng1 Is Nothing Then
For Each rng2 In rng1.Areas
'dump cells here
Next rng2
End If
Next ws
End Sub
标签:c,excel,vba,excel-vba 来源: https://codeday.me/bug/20191207/2087077.html