試試這個
Set rng1 = Range(_
Range("A1:Z1").Find("Name").Offset(1), _
Range("A1:Z1").Find("Name").Offset(1).End(xlDown))
但是一個謹慎的詞。如果第二行以後沒有數據,xlDown
可以爲您提供意想不到的結果。如果找不到名字,你採取的方法也會給你一個錯誤。
話雖如此,這裏是另一種
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
Dim aCell As Range, rng1 As Range
'~~> Set this to the relevant worksheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find the cell which has the name
Set aCell = .Range("A1:Z1").Find("Name")
'~~> If the cell is found
If Not aCell Is Nothing Then
'~~> Get the last row in that column and check if the last row is > 1
lRow = .Range(Split(.Cells(, aCell.Column).Address, "$")(1) & .Rows.Count).End(xlUp).Row
If lRow > 1 Then
'~~> Set your Range
Set rng1 = .Range(aCell.Offset(1), .Cells(lRow, aCell.Column))
'~~> This will give you the address
Debug.Print rng1.Address
End If
End If
End With
End Sub
謝謝悉達思! –
歡迎:) –