Пераўтварэнне формулы ячэйкі ў тэкст з дапамогай excel vba (Converting a cell's formula to text using excel vba)


問題描述

Пераўтварэнне формулы ячэйкі ў тэкст з дапамогай excel vba (Converting a cell's formula to text using excel vba)

I am writing a macro in Excel2003 to find all cells with formulas in a workbook and outputting their address and formula in a couple of columns on a different sheet.

I know I can show the formula for an individual cell using

Public Function ShowFormula(cell As Range) As String

    ShowFormula = cell.Formula

End Function

which works just fine, but since I didn't want to have to find all the cells by hand, I wrote the following macro to find them all for me

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

It finds all the cells just fine, but instead of displaying the formula as text, the list displays the formula results.

What am I missing to output the formulas as text instead of formulas?

‑‑‑‑‑

參考解法

方法 1:

Try this:

.Offset(i, 2).Value = "'" & CStr(cell.Formula)

Also, this will make things a bit quicker. Instead of 

For Each thisSheet In ThisWorkbook.Sheets
    If thisSheet.Index >= referenceRange.Parent.Index Then

try

For j = referenceRange.Parent.Index to Sheets.Count
    Set thisSheet = Sheets(j)

(by yu_ominaeDale M)

參考文件

  1. Converting a cell's formula to text using excel vba (CC BY‑SA 3.0/4.0)

#excel-2003 #excel #vba






相關問題

使用 VBA 根據 B 列的值重置 A 列中的值 (Reset values in column A based on the value of column B using VBA)

Пераўтварэнне формулы ячэйкі ў тэкст з дапамогай excel vba (Converting a cell's formula to text using excel vba)

刪除行並維護輸入範圍 (Delete Rows & Maintain Input Range)

從 VBA 自動排序中排除文本值 (Exclude Text Value From VBA Autosort)

Tại sao tôi không thể tạo biểu đồ này trong excel (sử dụng powershell) (Why can't I create this chart in excel (using powershell))

如何使用 VBA 從 Excel 中的公式中獲取單元格值? (How do I get the cell value from a formula in Excel using VBA?)

讓 Excel 2003 在 Word 文檔中進行查找並返回出現次數 (Have Excel 2003 do a Find in a Word document and return the number of occurences)

驗證下拉條件 (Validation Drop down on a condition)

在位於兩個不同工作表的兩個範圍內添加單元格 (Adding cells in two ranges which are located at two different sheets)

如何在excel中獲得所需的輸出? (How to get the required output in excel?)

從復雜查詢中獲取數據到 Excel (Getting data from a complex query to excel)

使用文本格式的用戶定義輸入從 excel 中檢索數據 (Retrieving data from excel with user defined input that is in text format)







留言討論