Excel VBA使用单元格和xlDownselect范围

我正在循环A列的范围find一个标题,一旦发现我需要select下一个单元格到最后使用的单元格。

不能为我的生活select使用单元格和结束(xlDown)这个范围

For k = 1 To lastCell If Cells(k, 1).Value = rangeName Then Range(Range(Cells(k + 1, 1)), Range(Cells(k + 1, 1)).End(xlDown)).Select End If Next k 

我试过Range(Cells(k + 1, 1), Cells(k + 1, 1).End(xlDown)) ,但是没有任何组合可以工作。

A列中有空白单元格,数据的例子如下:

 MONTH Jan Feb AGE 18-21 22+ GENDER Male Female Horse 

例如,如果rangeName等于GENDER ,我将如何去select这个范围。

以下应该工作:

 For k = 1 To lastCell If Cells(k, 1).Value = rangeName Then Range(Cells(k + 1, 1), Cells(k + 1, 1).End(xlDown)).Select End If Next k 

不过,我build议你更明确一点,以确保它的工作原理:

 With Worksheets("SheetYouAreWorkingOn") For k = 1 To lastCell If .Cells(k, 1).Value = rangeName Then .Range(.Cells(k + 1, 1), .Cells(k + 1, 1).End(xlDown)).Select End If Next k End With 

用空白/新文件testing您的示例数据:

 Public Sub tmpSO() Dim lastCell As Long Dim rangeName As String rangeName = "AGE" With Worksheets("Sheet1") lastCell = .Cells(.Rows.Count, 1).End(xlUp).Row For k = 1 To lastCell If .Cells(k, 1).Value = rangeName Then .Range(.Cells(k + 1, 1), .Cells(k + 1, 1).End(xlDown)).Select End If Next k End With End Sub