如何使范围变量?

时间:2016-08-23 04:03:02

标签: excel-vba vba excel

我想根据我的上一行计算将范围作为数字,但我找不到办法。

请参阅下面的双星号。

Sub TestVBA()
    OptimizeVBA True
    Dim startTime As Single, endTime As Single
    startTime = Timer

    Dim testnames As Range, testvalues As Range
    Dim lookupTestNames As Range, lookupTestValues As Range
    Dim vlookupCol As Object

    Dim lastRow As Long
    Dim lCol As Long

    'Dim LastLine As Long
    'LastLine = Range("A" & Rows.Count).End(xlUp).Row

    'Find the last non-blank cell in column A(1)
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row

    'Find the last non-blank cell in row 1
    lCol = Cells(1, Columns.Count).End(xlToLeft).Column


    'Set testnames = Worksheets("Data Analysis").Range("C8:C399")
    **Set testnames = Worksheets("Data Analysis").Range("C8:C&lastRow")**
    Set testvalues = Worksheets("Data Analysis").Range("O8:O399")
    Set lookupTestNames = Worksheets("Graph").Range("A5:A172")
    Set lookupTestValues = Worksheets("Graph").Range("T5:T172")

    'Build Collection
    Set vlookupCol = BuildLookupCollection(testnames, testvalues)

    'Lookup the values
    VLookupValues lookupTestNames, lookupTestValues, vlookupCol
    endTime = Timer
    Debug.Print (endTime - startTime) & " seconds have passed [VBA]"
    OptimizeVBA False
    Set vlookupCol = Nothing
End Sub

1 个答案:

答案 0 :(得分:2)

你有双引号内的lastRow

<强>不正确

Set testnames = Worksheets("Data Analysis").Range("C8:C&lastRow")

<强>正确

Set testnames = Worksheets("Data Analysis").Range("C8:C" & lastRow)