我的拼图需要帮助。我写了一个工作正常的代码。现在,用户不仅要查看最终结果,还要查看用VBA编写的公式(用于审计目的),以验证结果是否正常。有没有办法在同一个单元格中显示BOTH结果和公式?
到目前为止我的代码:
Sub Vlookup_Condition_VAT_table()
Dim Rng As Range
Dim i As Long
Application.ScreenUpdating = False
Workbooks("GST_recovery_overclaim.xlsm").Worksheets("MonthlyData_Raw").Activate
'Identify the Destination location to start populating vlookuped values
Range("AK2").Activate
With Worksheets("MonthlyData_Raw").Cells
Set Rng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 2 To Rng.Rows.Count
'populate the destination range with
'vlookup values from the list vlookup table
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
Next
End With
Application.ScreenUpdating = True
End Sub
非常感谢, 拉斯
答案 0 :(得分:1)
如果您没有查看数千行,可以尝试以下操作。
而不是使用此行来打印vlookup公式的结果:
Rng.Cells(i, 37) = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)
您可以使用此更改将实际公式放入单元格
Rng.Cells(i, 37).Formula = "=VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False)"
--------------基于OP评论的更新---------------------------- -
通过ISERROR
函数添加错误捕获(未经测试)
Dim errMsg as String
errMsg = "Not in exception list"
Rng.Cells(i, 37).Formula = "=ISERROR(VLOOKUP(" & _
.Cells(i, 28).Address & "," & _
"'" & Sheets("VAT_apportionment_table_201310").name & "'!D:G," & _
"4," & _
"False), " & errMsg & ")"
我为错误消息创建了一个变量,原因有两个。
答案 1 :(得分:0)
也许用答案和公式为结果添加文字。
Rng.Cells(i, 37) = "Answer: " & Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False) & ", Formula: = Application.VLookup(.Cells(i, 28), Sheets("VAT_apportionment_table_201310").Range("D:G"), 4, False)"
答案 2 :(得分:0)
如果我理解正确,您希望显示在结果中输入的公式。这个怎么样?
'Assume that you have a Range("A1") equals to = cos(20)
Sub GetFormula()
Dim MyFormula As String
MyFormula = Replace(Range("A1").Formula, "=", "")
MsgBox ("Formula is : " & MyFormula _
& " Result is :" & Range("A1"))
End Sub