0

我非常接近我的解决方案,但我似乎有一个障碍。我已经复制了下面的代码。总之,我使用 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))

任何帮助将不胜感激!

4

1 回答 1

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
于 2013-10-24T22:15:42.640 回答