VBA执行计算
我正在尝试使用VBA将计算添加到多个工作表的使用范围。 问题是,我不断收到这个错误
'types不匹配'
上线阅读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
编辑 –
下载示例工作簿 – Garbagedata.xlsx
“= RC7 + RC8”/“= VLookup(A2,Totals!B2:R100,3,False)”
删除等于"=VLookup(
。
您不能混用R1C1
符号和A1
符号: RC7+RC8
和A2
不能混合使用
而不是杀死自己试图在VBA中build立FormulaR1C1,让公式在工作表上正确工作,然后将工作公式打印到即时窗口
UPDATE
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