我非常接近我的解决方案,但我似乎有一个障碍。我复制了下面的代码。在摘要中,我使用VLookup从另一个工作表中提取伪公式,用真正的单元格引用替换伪“ZZZ”,然后将这些单元格值转换为最终公式。这一切都很好,除了输入伪公式的用户不会使用错误的公式,所以我需要一些错误检查。首先,我需要任何不返回重命名结果的VLookup值。其次,我需要重命名任何无效的最终公式(例如遗漏括号)。这是我到目前为止所做的:
Public Sub VLookup()
ActiveSheet.EnableCalculation = True
'Define what our Rows are for the calculations
Dim NumRecords As Long
NumRecords = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Range("B" & Rows.Count).End(xlUp).Row
Dim CellsForFormula As Range
Set CellsForFormula = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Range("G2", "G" & NumRecords)
Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").Select
'Now Insert the VLookup
Dim WSLogic As Worksheet
Dim WSData As Worksheet
Set WSData = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data")
Set WSLogic = Workbooks("POVA Daily Reporter.xlsm").Worksheets("Logic Statements")
'Write the Vlookup in the cell
CellsForFormula(1, 1).Formula = _
"=VLOOKUP('Paste Daily Data'!B2,'Logic Statements'!A:D,4,False)"
'Copy the Vlookup down
CellsForFormula(1, 1).Copy _
Destination:=Range(CellsForFormula(2, 1), CellsForFormula(NumRecords - 1, 1))
'Make sure the formulas actually calculate
Workbooks("POVA Daily Reporter.xlsm").Worksheets("Paste Daily Data").UsedRange.Calculate
'Copy and Paste so we just keep the result of the Vlookup
CellsForFormula.Copy
CellsForFormula.PasteSpecial Paste:=xlPasteValues
'Now we can replace the "ZZZ" and "zzz" with the cell reference
Application.EnableEvents = False
Dim Cell As Variant
On Error Resume Next
For Each Cell In CellsForFormula
If Cell.Value = "#N/A" Then
Cell.Value = "Bill-to Not in POVA"
'ElseIf Cell.Formula.IsErr = "=" & Cell.Value Then
'Cell.Value = "Logic Code Incorrect"
Else
Cell.Formula = "=" & Cell.Value
ActiveSheet.EnableCalculation = True
End If
Next Cell
For Each Cell In CellsForFormula
Call Cell.Replace("ZZZ", Cell.Offset(0, -4).Address)
Cell.Application.WorksheetFunction = "="
ActiveSheet.EnableCalculation = True
Next Cell
For Each Cell In CellsForFormula
If Cell.Value = "#N/A" Then
Cell.Value = "Bill-to Not in POVA"
ActiveSheet.EnableCalculation = True
End If
Next Cell
Application.EnableEvents = True
Sheet.Calculate
End Sub
问题是那些无效的公式 - 因为On Error Resume Next,它只是让它们孤立无援:
AND(LEN($C$37)=10,ISNUMBER(VALUE($C$37))
非常感谢任何帮助!
答案 0 :(得分:0)
我相信您要找的是.WorksheetFunction
而不是.Function
。
例如,更改此内容:
For Each Cell In CellsForFormula
If Cell.Value = "#N/A" Then
Cell.Value = "Bill-to Not in POVA"
ElseIf Cell.Formula.IsErr = "=" & Cell.Value Then
'Cell.Value = "Logic Code Incorrect"
Else
Cell.Formula = "=" & Cell.Value
ActiveSheet.EnableCalculation = True
End If
Next Cell
对此:
For Each Cell In CellsForFormula
If Cell.Value = "#N/A" Then
Cell.Value = "Bill-to Not in POVA"
ElseIf Cell.WorksheetFormula.IsErr = "=" & Cell.Value Then
'Cell.Value = "Logic Code Incorrect"
Else
Cell.Formula = "=" & Cell.Value
ActiveSheet.EnableCalculation = True
End If
Next Cell