Excel VBA-循环执行-移至下一行

问题描述

我试图根据不同的单元格值在报表输出添加注释。

我选择了“ do while ... Loop”,因为一旦B列中的Unq标识符不再出现在连续的行中而不是遍历整个工作表,就需要停止该条件。 Unq标识符可以在1-50行中找到。测试Col K值,并比较col N值和原始单元格(LastCell)。 这似乎在起作用,但是,我无法使Loop移至下一行。 我使用的是rng = rng + 1,但不是向下移动一行,而是向Unq标识符添加1,即:123变为124。

如何向下移动?另外,还有解决这个问题的更好方法吗?

仅供参考,我正在修改别人的代码,而不是从头开始。

Sub play()
 
Dim Unq As Long
Dim Cell As Range
Dim LastCell As Range
Dim rng As Range
 
For Each Cell In Range("U2:U" & ActiveSheet.UsedRange.Rows.Count)
        If Int(Range("B" & Cell.Row).Value) <> Unq And Len(Range("A" & Cell.Row).Value) > 0 Then
            Unq = Int(Range("B" & Cell.Row).Value)
            Cell.Value = Cell.Offset(0,-6).Value - Cell.Offset(0,-5).Value
        If Cell.Offset(0,-15) = "REG" Then
            GoTo Skip1
       
Skip1:
  
    'Bonus identified - No requirement to investigate
        If Cell.Offset(0,-2).Value <> 0 Or Cell.Offset(0,-1).Value <> 0 Then
       
        Set LastCell = Cell
        Set rng = Cell.Offset(0,-19)
                        
        do while rng.Value = Unq
           
            If rng.Offset(0,9).Value = "BONUS" And rng.Offset(0,12).Value = LastCell.Value Then
            LastCell.Activate
            Cell.Offset(0,1).Value = "Bonus Paid"
            ElseIf rng.Offset(0,12).Value <> LastCell.Value Then
            LastCell.Activate
            Cell.Offset(0,1).Value = "Retention Bonus Paid"
            Cell.Offset(0,2).Value = "Variation managed by macro"
            Cell.Offset(0,3).Value = "Variation managed by macro"
            Cell.Offset(0,4).Value = Date
            Cell.Offset(0,5).Value = "Yes"
        Else:
         rng = rng + 1
        
       
    End If
                  
    Loop
 

解决方法

显然不是昨天我最美好的一天之一!

Do Until rng.value <> Unq Or Cell.Offset(0,1 <> ""

修改的代码

已更改

Else:
rng = rng + 1

Else:
Set rng = rng.offset(1,0)

请客。