循环遍历其列由variables指定并且其行是i的列

我正在尝试创build一个macros来自动填充VLOOKUP列。 macros调用函数FindNectEmpty查找下一个空单元格,并将该单元格设置为variablesOpenCellVoucher(这是列标题)。 调用相同的函数来查找OpenCellVoucher单元下的空单元(该单元是variablesFillCellVoucher)。 然后我想在该列的所有单元格上执行一个VLOOKUP。 这就是我所拥有的,但是我在这条线上出现错误,需要一个对象。 为什么这不是一个对象? 任何想法来解决这个问题?

Range(FillCellVoucher.Column, i).Formula = Application.WorksheetFunction.VLookup(J2, DollarRanges.Range("$A$2:$B$14"), 2, 1) Dim OpenCellVoucher As Range Dim FillCellVoucher As Range Dim lngRow As Long Set NextCell = FindNextEmpty(ActiveWorkbook.Sheets("Master").Range("A1")) ActiveWorkbook.Sheets("Master").Select NextCell.Value = "Dollar Range of Remaining Balance" 'Find Open Vouchering column Set OpenCellVoucher = Worksheets("Master").Range("A1:O1").Find("Open for Vouchering Amt", =xlPart) 'Looks up next empty cell in row Set FillCellVoucher = FindNextEmpty(ActiveWorkbook.Sheets("Master").Range("A2")) ActiveWorkbook.Sheets("Master").Select lngRow = Cells(Rows.Count, OpenCellVoucher.Column).End(xlUp).Row For i = 1 To lngRow Range(i, FillCellVoucher.Column).Formula = _ "=VLookup(J" & i + 1 & ", 'DollarRanges'!$A$2:$B$14, 2, 1)" Next i 

当您在单元格中设置公式时,您需要将astring分配为astring:

 Range(FillCellVoucher.Column, i).Formula = _ "=VLookup(J2, DollarRanges.Range(" & "$A$2:$B$14" & "), 2, 1)" 

我认为“J2”就是你希望在细胞衰落的时候改变的。 如果这是你的意图,那么:

 Range(FillCellVoucher.Column, i).Formula = _ "=VLookup(J" & i + 1 & ", DollarRanges.Range(" & "$A$2:$B$14" & "), 2, 1)" 

我不明白“DollarRanges”是什么,但如果这对你有用,那么我不需要知道。

希望这可以帮助。

我想到了! 代码可以绝对简化..但这里是:

 Dim OpenCellVoucher As Range Dim FillCellVoucher As Range Dim myAddress As String Dim myColumn As String Dim myAdd As String Dim CellSelect As String 'Find Open Vouchering column Set OpenCellVoucher = Worksheets("Master").Range("A1:O1").Find("Open for Vouchering Amt", lookat:=xlPart) 'Looks up next empty cell in row Set FillCellVoucher = FindNextEmpty(ActiveWorkbook.Sheets("Master").Range("A2")) ActiveWorkbook.Sheets("Master").Select myAddress = FillCellVoucher.Address myAdd = Left(myAddress, 2) myColumn = Right(myAdd, 1) For i = 1 To RowsUsed CellSelect = myColumn & i + 1 ActiveWorkbook.Sheets("Master").Range(CellSelect).Formula = _ "=VLookup(J" & i + 1 & ", 'DollarRanges'!$A$2:$B$14, 2, 1)" Next i