excel 正在设置重复操作,如果它是Loop或Do或?语句,我将迷路

6rqinv9w  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(116)

'我需要此命令来跨列重复复制和粘贴过程,直到计数器'=关键页、“book”单元格或从第1行读取(如果第1行#=关键“book”#
'下一列(页)应该是上一个复制/粘贴列的3列

"PicPg" B2 copies to "PrntPg" B2
E2 to E2
etc

**这是我第一次在任何论坛上发帖寻求帮助。请原谅我的无知。我会尽我所能回答任何问题。提前感谢你的时间和帮助!

我可以分享工作簿,只是不知道怎么做。

Sub createPrintPage()
 
    With Worksheets("PicPg").Cells(2, 2)
        .Copy
        Sheets("PrntPg").Pictures.Paste(Link:=True).Select

        With Worksheets("PrntPg").Cells(2, 5)
            .Select
       
            Worksheets("PicPg").Cells(2, 5).Copy
            Sheets("PrntPg").Pictures.Paste(Link:=True).Select
         
        End With
    End With
        
 End Sub

“对于我”,我还没有弄清楚。我一直试图让它'继续重复....我已经尝试玩对于我的...我迷路了'这句话似乎是工作,现在让它继续跨越。'这是第三天我研究,尝试了许多方法...只能得到这么远(这'是漂亮得多(即:简单),然后我开始。

wqlqzqxt

wqlqzqxt1#

这两个方法都用于循环和迭代。如果你想使用For,只需给予它一个开头和一个结尾

Dim i As Integer
    
    For i = 0 To 3
        'put your code in here and it will loop 4x (i = 0, i = 1, i = 2 and i = 3)
    Next i
    
    'you can put your condition for the loop to exit either at the Do or Loop by using until
    i = 0
    
    Do Until i = 3
        'although start from 0, but it will loop 3x because when it hit i = 3 it will stop (i = 0, i = 1, i = 2 and i = 3)
        i = i + 1 'remember to increment your counter, before leaving the loop the counter had changed to 1 
    Loop
    
    i = 0
    
    Do
        'although start from 0, but it will loop 3x because when it hit i = 3 it will stop (i = 0, i = 1, i = 2 and i = 3)
        i = i + 1 'remember to increment your counter
    Loop Until i = 3

还有很多不同的写法。
你可以用单元格迭代来完成循环

Dim cell As Object 'late binding, early binding can write Dim cell as range
        
    For Each cell In ThisWorkbook.Range("A1:A20")
        'do something
    Next cell

你甚至可以用你自己的条件来设置止损

Do
    If x = 1 Then
        ThisWorkbook.Range("A1").Value = "True"
    End If
Loop Until ThisWorkbook.Range("A1") = "True"

如果您已经达到了想要的结果,您甚至可以退出Do或Exit For。

Dim i as Integer, temp as string

For i = 0 To 3
    If ThisWorkbook.Range("A" & i).Value2 = "True" Then
        temp = "hey, I found what I am looking for"
        Exit For
    End If
Next i

相关问题