我对VBA很陌生,并且很难理解一些语法。
例如,我有a3:c13
的范围,我想将其设置为变量,以便稍后将其作为表数组传递给vlookup。但是,范围是由用户输入根据其大小定义的。它总是从A3
开始,它将始终包含列A:C
,但我不知道它会走多远。在那种情况下,我认为我将其设置为:
With range("a3")
table_array = range(.cells(0,0), .End(xlDown).End(xlToRight)).Select
End With
然而,这似乎不起作用。我收到运行时错误:
Run-time Error '1004': Method '_Default' of object 'Range' failed.
答案 0 :(得分:1)
假设col,A,B和C具有相同的行数:
Sub Macro1()
Set r = Range("A3")
Set table_array = Range(r, r.End(xlDown)).Resize(, 3)
End Sub
答案 1 :(得分:0)
您可以在Col A:C中找到最后一行,然后构建您的范围?
Sub Sample()
Dim ws As Worksheet
Dim LastRow As Long
Dim Rng As Range
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Range("A:C").Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
If Not LastRow < 3 Then
Set Rng = .Range("A3:C" & LastRow)
Debug.Print Rng.Address
Else
MsgBox "No Data found beyond A3"
End If
End With
End Sub