VBA执行计算

时间:2016-12-06 23:00:23

标签: excel vba excel-vba excel-2013 worksheet-function

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

  

'输入不匹配'

在阅读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

1 个答案:

答案 0 :(得分:1)

  

&#34; = RC7 + RC8&#34; /&#34; = VLookup(A2,Totals!B2:R100,3,False)&#34;

"=VLookup(删除等号。

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

不要试图在VBA中构建一个FormulaR1C1,而是在工作表上正确运行公式,然后将工作公式打印到立即窗口

enter image description here

更新

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