使用下面的宏,我设法用一个值选择B列中的所有单元格。但现在我需要更改该代码,以便选择具有值的单元格下方的单元格。
示例: 假设下面的宏选择单元格B8:B15,那么它应该用新代码选择单元格B16。我应该在此代码中添加什么才能使其正常工作?
Sheets("sheet1").Select
Dim LR2 As Long, cell2 As Range, rng2 As Range
With Sheets("sheet1")
LR2 = .Range("B" & Rows.Count).End(xlUp).Row
For Each cell2 In .Range("B8:B" & LR2)
If cell2.Value <> "" Then
If rng2 Is Nothing Then
Set rng2 = cell2
Else
Set rng2 = Union(rng2, cell2)
End If
End If
Next cell2
rng2.Select
End With
答案 0 :(得分:0)
Range对象的Offset
方法应该是您要查找的内容。
在任何范围内,您都可以执行以下操作:
newRang=rng.offset(1,0)
请参阅这些资源以了解更多信息:
答案 1 :(得分:0)
我认为这应该有用
Sheets("sheet1").Select
Dim LR2 As Long, cell2 As Range, rng2 As Range
With Sheets("sheet1")
LR2 = .Range("B" & Rows.Count).End(xlUp).Row
For Each cell2 In .Range("B8:B" & LR2)
If cell2.Value <> "" Then
If rng2 Is Nothing Then
Set rng2 = Sheets("sheet1").cells(cell2.Row + 1, cell2.Column)
Else
Set rng2 = Union(rng2, cell2)
End If
End If
Next cell2
rng2.Select
End With