1

我正在尝试使用 vba 自动化一个 excel 过程。

从 2010 年开始,每个月都有一列(这可以无限延伸,或者至少在 excel 的限制内)。下面有大约 500 行带有数字数据。

所以,我想找到最后一列未使用(当前月份)并从 vlookup 中填充它下面的所有行。

谢谢。


工作代码!

With ActiveSheet.Range("IV42").End(xlToLeft)
    Dim iRow As Integer
    For iRow = 1 To 581

        Dim Lookup_Value, Table_Array, Col_Index, Range_Lookup As String
        Lookup_Value = "B42:B622"
        Table_Array = "'Current Rankings'!$B$2:$C$607"
        Col_Index = "2"
        Range_Lookup = "False"
        .Offset(iRow - 1, 0).Formula = "=VLOOKUP(" & Lookup_Value & "," & Table_Array & "," & Col_Index & "," & Range_Lookup & ")"

    Next iRow
End With
4

1 回答 1

2

要查找最后一列,请使用ActiveSheet.Range("IV1").End(xlToLeft). (假设你在第一行有值)

因此,要在使用的最后一列之后找到该列,您可以使用ActiveSheet.Range("IV1").End(xlToLeft).Range("B1"). 这将指向新月份列的第一个单元格。

然后你可以做一个简单的循环来填充列......你的代码应该看起来像:

With ActiveSheet.Range("IV1").End(xlToLeft).Range("B1")
    Dim iRow as Integer
    for iRow = 1 to 500

        Dim Lookup_Value, Table_Array, Col_Index, Range_Lookup
        Lookup_Value = ????
        Table_Array = ????
        Col_Index = ????
        Range_Lookup = ????

        .Offset(iRow-1,0).Formula = "=VLOOKUP(" & "," & _
            Lookup_Value & _"," & _
            Table_Array & "," & _
            Col_Index_Num & "," & _
            Range_Lookup & ")"

    next iRow
end with
于 2012-09-20T14:12:45.967 回答