VBA 执行计算

VBA To Perform Calculation

我正在尝试使用 VBA 将计算添加到多个工作表的使用范围。问题是,我不断收到此错误

'Type Mismatch'

上线阅读ws.Cells(countie, 12).FormulaR1C1 =...

这是我的语法 - 如何解决此问题以便执行此语法?

Function JunctionTest()
Dim ws As Worksheet, countie As Long
For Each ws In ActiveWorkbook.Worksheets
  With ws
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
    LastRow = .Cells.Find(What:="*",After:=.Range("A1"),LookAt:=xlPart, _
                  LookIn:=xlFormulas, SearchOrder:=xlByRows,SearchDirection:=xlPrevious, _
                  MatchCase:=False).Row
  Else
    LastRow = 1
  End If
  For countie = 1 To LastRow
        ws.Cells(countie, 12).FormulaR1C1 = "=RC7+RC8" / "=VLookup(A2, Totals!B2:R100, 3, False)"
Next countie
End Function

编辑 --

Download Sample Workbook - Garbagedata.xlsx

"=RC7+RC8" / "=VLookup(A2, Totals!B2:R100, 3, False)"

去掉 "=VLookup( 中的等号。

您不能将 R1C1 符号与 A1 符号混合使用:RC7+RC8 & A2 不要混合使用

与其在 VBA 中尝试构建 FormulaR1C1,不如自杀,让公式在工作表上正常工作,然后将工作公式打印到即时 Window

更新

ws.Cells(countie, 12).FormulaR1C1 = "=RC7+RC8/VLOOKUP(RC1, Totals!R2C2:R100C18, 3, FALSE)"

Sub JunctionTest()
    Dim ws As Worksheet
    Dim lastRow As Long
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
            .Range("L2:L" & lastRow).FormulaR1C1 = "=RC7+RC8/VLOOKUP(RC1, Totals!R2C2:R100C18, 3, FALSE)"
        End With
    Next
End Sub