excel 学习如何设置跨列循环以复制/粘贴所选内容,执行操作,然后移动,再次执行

q0qdq0h2  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(378)

我已经能够处理这个问题了。
我肯定有无用的东西在这,我移动的nexts,循环,与等左右,看到不同的结果,所以我知道笑话...
我最大的障碍是让循环1中相同的动作发生在循环2中,但在下一个选择的单元格中。
我差不多凌晨一点就走了...
谢谢大家抽出时间!!
更新代码:
子循环复制粘贴()将范围变小为范围将范围变小为范围将范围2变小为范围

counter = 0
bkpages = Worksheets("Key").Range("B3").Value

当bkpages〉= bkpages '当bkpages〉1时执行

Set Rng = shPicPg.Cells(2, 2)
Set pstrng = shPrintPg.Cells(2, 2)
Set Rng2 = Rng.Offset(0, i)
Set pstrng2 = pstrng.Offset(0, i)

对于i = 2至bkpages步骤(3)
带有工作表(“PicPg”)。选择

Rng2.Select
    Rng2.CopyPicture
    
With Worksheets("PrntPg").Select

    pstrng.Select
    Sheets("PrntPg").Pictures.Paste
      
End With

结束于

pstrng2 = pstrng2
    Rng2 = Rng2
   
Next i

回路
末端子组件

qacovj5a

qacovj5a1#

这两个方法都用于循环和迭代。如果你想使用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

相关问题