简体   繁体   English

Excel-Vba:根据下面的代码选择第一个没有值的单元格

[英]Excel-Vba: select first cell with no value based on the code below

With the macro below, I managed to select all of the cells in column B with a value. 通过下面的宏,我设法选择了带有值的B列中的所有单元格。 But now I need to change that code so that it selects the cell just underneath the cells with a value. 但是现在我需要更改该代码,以便它选择具有值的单元格正下方的单元格。

Example: Let's say the macro below selects cells B8:B15, then it should select cell B16 with the new code. 示例:假设下面的宏选择了单元格B8:B15,那么它应该选择具有新代码的单元格B16。 What should I add to this code to make this work? 我应该在此代码中添加什么才能使其正常工作?

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

The Offset method of the Range object should be what you are looking for. Range对象的Offset方法应该是您要寻找的。

On any range, you can do something like: 在任何范围内,您都可以执行以下操作:

newRang=rng.offset(1,0)

see these resources to learn more: 请参阅以下资源以了解更多信息:

I think this should work 我认为这应该工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM