2016-08-23 86 views
-1

我想根據我的最後一行計算將範圍作爲數字,但我找不到一種方法。如何使範圍變量?

請參閱下面的雙星號。

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 

回答

2

您有lastRow在雙引號內。

錯誤

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

正確

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

+0

托馬斯謝謝。 – camel