简体   繁体   English

使用FOR LOOP的EXCEL VBA将忽略步骤+1 LOOP COUNTER

[英]EXCEL VBA using FOR LOOP that will disregard the step +1 LOOP COUNTER

I am in search of a solution that will disregard or take one step back from the loop counter. 我正在寻找一种解决方案,该解决方案将忽略或从循环计数器中退一步。 Here is my code for better understanding. 这是我的代码,可以更好地理解。

 Sub DownArrow5_Click()
Dim c As Integer
Dim copyFromRow As Integer

copyFromRow = 1

For c = 1 To 20
   If ActiveSheet.Rows(copyFromRow).Hidden = False And Range("A" & c & "").Value <> "" Then
   'start copy paste
    ActiveSheet.Range("A" & copyFromRow & ", C" & copyFromRow & "").Copy
    Sheets("Sheet2").Select
    ActiveSheet.Range("A1").Select
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Select
    Sheets("Sheet1").Select
    Application.CutCopyMode = False

    Else
        c = 'disregard the +1 for the loop

   End If
 Next c   
End Sub

I can't decrement the counter since it will have a negative (-) result thus returning an unending loop. 我不能递减计数器,因为它将产生负数(-),从而返回无休止的循环。

NOTE: I need to copy and paste 20 UNHIDDEN rows to sheet2. 注意:我需要将20个未隐藏的行复制并粘贴到sheet2。 That is why I need to get the Max counter (20). 这就是为什么我需要获得Max计数器(20)的原因。 This is just a brief code from what I am working on. 这只是我正在研究的简短代码。

Indeed, manipulating your loop counter in a For...Next loop probably isn't a good idea. 确实,在For...Next循环中操作循环计数器可能不是一个好主意。 Your code snippet isn't clear about why you would need to do this, but if you need to use the value of c - 1 somewhere in your Else block, either use c - 1 (without assigning c ), or assign c - 1 to another variable: 您的代码段并不清楚为什么你需要做的,但如果你需要使用的值c - 1在某处Else块,要么使用c - 1 (不分配c ),或指定c - 1到另一个变量:

Sub DownArrow5_Click()
    Dim c As Integer, d As Integer

    For c = 1 To 20
        If (condition) then
            'do stuff here

        Else
            d = c - 1
            'some more stuff here using d

        End If
    Next

End Sub

UPDATE UPDATE

Now that you have edited your code with more details, I think you're looking for something like this: 既然您已经编辑了更多详细信息的代码,我想您正在寻找的是这样的东西:

While c <= 20

    If Not ActiveSheet.Rows(copyFromRow).Hidden _
       And Range("A" & c).Value <> vbNullString Then

        'do your stuff
        c = c + 1

    End If

Wend

Note that VB has several loop constructs that can work just as well - and any condition that evaluates to False at 20 iterations will do, so just use whatever you find more readable: 请注意,VB有几个循环结构也可以很好地起作用-并且任何条件在20次迭代后得出False条件都可以,因此,只要使用您发现更具可读性的内容即可:

Do Until c = 21 ' until c = 20 would make it 19 iterations since c starts at 1
   ...
   c = c + 1
Loop

Do While Not c > 20
   ...
   c = c + 1
Loop

Use another type of loop: 使用另一种类型的循环:

c = 1
Do
    If (condition) Then
        'do stuff

         c = c+1  'increment your counter variable
    Else:
        'presumably do nothing, i.e., "Disregard" the loop.
        ' do NOT increment the counter variable in the Else block
    End If

Loop While Not c > 20

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

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