使用A1参考将R1C1中logging的VLookUp公式转换为VBA公式

我有这个loggingmacros,我想转换为纯VBAmacros代码,并从AE2公式复制到lastrow。

 "=VLOOKUP(RC[-22],'[test.xlsx]Sheet3'!C5:C6,2,0)" ' this is from another workbook 

试图转换为VBA。

 dim wbSLW as workbook dim wbSLWDir as String wbSLWDir = "C\Documents\test.xlsx" 'this is not the constant directory or file name set wbSLW = workbooks.open(wbSLWDir) ThisWorkbook.Activate With Thisworkbook.Sheets(1) .Range("AE2") = "=VLOOKUP(I2," & wbSLW & "!E:F,2,0)" ' error line end with 

当我转换它时,它返回Object does not support this property or method

清理logging的代码:

 With ActiveSheet.Range("AE2") .FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)" .AutoFill Destination:=Range("AE2:AE182"), Type:=xlFillDefault End With 'ActiveSheet 

RC[-22]是指在具有公式的单元格之前的22列的单元格
这里AE2是初始单元,所以RC[-22] = I2

C[-30]C[-29]分别指的是在具有公式的单元之前30列和29列的列
这里是AE2 ,所以C[-30] = AC[-29] = B

更改公式:

 Sheets(1).Range(perNum & 2).Formula = "=VLOOKUP(I2,Temp!A:B,2,0)" 

或者不用转换公式:

 Sheets(1).Range(perNum & 2).FormulaR1C1 = "=VLOOKUP(RC[-22],Temp!C[-30]:C[-29],2,0)" 

通过转到(Excel 2010)设置/公式,更改您的设置,使其不使用R1C1格式,然后取消选中R1C1参考样式。 重新录制你的macros,然后编辑它。 你不应该有任何“RC”引用,但是如果你这样做,那么把所有引用中有“RC”的引用改为与“I2”中相同的引用。

假设活动工作表名称是Sheet1

 ActiveCell.Value = Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("I2"), Sheets("Temp").Range("A:B"), 2, 0)