Excel2003 でマクロを作成して、ブック内の数式を含むすべてのセルを検索し、それらのアドレスと数式を別のシートのいくつかの列に出力しています。
を使用して個々のセルの数式を表示できることを知っています
Public Function ShowFormula(cell As Range) As String
ShowFormula = cell.Formula
End Function
これは問題なく動作しますが、手動ですべてのセルを見つける必要がなかったので、次のマクロを書いてすべてを見つけました
Sub Macro2()
Dim i As Integer
Dim targetCells As Range
Dim cell As Range
Dim referenceRange As Range
Dim thisSheet As Worksheet
Set referenceRange = ActiveSheet.Range("CA1")
With referenceRange
For Each thisSheet In ThisWorkbook.Sheets
If thisSheet.Index >= referenceRange.Parent.Index Then
Set targetCells = thisSheet.Cells.SpecialCells(xlCellTypeFormulas, 23)
For Each cell In targetCells
If cell.HasFormula Then
.Offset(i, 0).Value = thisSheet.Name
.Offset(i, 1).Value = cell.Address
.Offset(i, 2).Value = CStr(cell.Formula)
i = i + 1
End If
Next
End If
Next
End With
End Sub
すべてのセルが正常に検出されますが、数式をテキストとして表示する代わりに、リストに数式の結果が表示されます。
数式ではなくテキストとして数式を出力するには何が欠けていますか?