我正在A列中的范围内循环查找标题,找到后便需要选择下一个单元格,直到最后使用的单元格。
我一生无法使用Cells和End(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
,我将如何选择该范围。
以下应该工作:
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
但是,我建议您对代码进行更明确的编码以确保其有效:
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
在空/新文件上使用示例数据进行了测试:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句