【问题标题】:Cell Address in a loop循环中的单元格地址
【发布时间】:2018-05-10 22:33:47
【问题描述】:

我正在遍历一系列单元格以检查值是否为 0。我的逻辑是如果单元格值为零,则单元格值是前一个单元格值。如果前一个单元格也为零,那么它就是下一个单元格的值。但我说如果最后一个单元格或第一个单元格为零怎么办?我也需要检查它,因为如果它是第一个单元格或最后一个单元格,则循环失败。我的问题是,通过最后一行作为地址的代码应该是什么。我知道最后一行,但我不知道如何将其写为具有已知列的地址。数据从第 2 行开始,然后到第 X 行。

For each Cell In rng
    If Cell.Address="A2" Then
        If Cell.Value=0 Then
           Cell.Value=Cell.Offset(1,0).Value
        End if

     Elseif Cell.Address="AX" Then 'X is the last row
        If Cell.Value=0 Then
           Cell.Value=Cell.Offset(-1,0).Value
        End If

    Elseif Cell.Value=0 and Cell.Offset(1,0).Value=0 Then 
        Cell.Value=Cell.Offset(-1,0).Value

    Elseif Cell.Value=0 Then
        Cell.Value=Cell.Offset(1,0).Value

    Else
        Do Nothing

    End If
Next

【问题讨论】:

  • RANGE 是如何定义的?如果 RANGE 真的是(命名错误的)变量,那么 RANGE(RANGE.Rows.Count, RANGE.Columns.Count).Address 会给你该范围内的最后一个单元格。
  • 澄清了这个问题。 RANGE 是一个范围,我的实际名称不同。将范围更改为 rng
  • 我的评论仍然支持rng - rng(rng.cells.count).addressrng(1).address 是返回第一个和最后一个单元格地址的另一种方式。如果你想要这个专栏,你可以使用rng(1).Column

标签: excel if-statement dynamic vba


【解决方案1】:

我添加了三行来定义变量和范围。
除此之外,我只对IF 语句和第一个ELSEIF 语句进行了更改。

Sub Test()

    Dim rng As Range
    Dim Cell As Range

    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A20")

    For Each Cell In rng
        If Cell.Address = rng(1).Address Then
            If Cell.Value = 0 Then
               Cell.Value = Cell.Offset(1, 0).Value
            End If

         ElseIf Cell.Address = rng(rng.Cells.Count).Address Then
            If Cell.Value = 0 Then
               Cell.Value = Cell.Offset(-1, 0).Value
            End If

        ElseIf Cell.Value = 0 And Cell.Offset(1, 0).Value = 0 Then
            Cell.Value = Cell.Offset(-1, 0).Value

        ElseIf Cell.Value = 0 Then
            Cell.Value = Cell.Offset(1, 0).Value

        Else
            'Do Nothing

        End If
    Next

End Sub  

编辑:(接受答案后)。

要遍历每列中的每个单元格,您需要一个循环来查看每一列,然后再循环查看列中的每个单元格。

在下面的代码中,我将col 定义为一个范围。
然后在第一个循环中使用它 (For Each col in rng.Columns)。
然后第二个循环查看col (For Each Cell in col.Cells) 中的每个单元格。

Sub Test()

    Dim rng As Range
    Dim Cell As Range
    Dim col As Range

    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:Z20")

    For Each col In rng.Columns
        For Each Cell In col.Cells
            If Cell.Address = rng(1).Address Then
                If Cell.Value = 0 Then
                   Cell.Value = Cell.Offset(1, 0).Value
                End If

             ElseIf Cell.Address = rng(rng.Cells.Count).Address Then
                If Cell.Value = 0 Then
                   Cell.Value = Cell.Offset(-1, 0).Value
                End If

            ElseIf Cell.Value = 0 And Cell.Offset(1, 0).Value = 0 Then
                Cell.Value = Cell.Offset(-1, 0).Value

            ElseIf Cell.Value = 0 Then
                Cell.Value = Cell.Offset(1, 0).Value

            Else
                'Do Nothing

            End If
        Next
    Next col

End Sub

【讨论】:

  • 如果我的范围扩展到多列怎么办?我想我会得到一个类似于我在问题中定义我的范围的答案。如果不是,那么每一列都是一个范围,我可以有多个循环。
  • 如果rng 设置为A1:Z20,那么rng(rng.Cells.Count).Address 将返回Z20。我将在我的答案中添加另一部分,以依次查看每一列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多