简体   繁体   English

Excel VBA中的嵌套复制/粘贴循环

[英]Nested copy/paste loops in excel VBA

Very new to VBA and trying to write a macro that has a loop within a loop. VBA的新手,它试图编写一个在循环中具有循环的宏。 I'd like the whole code to run 30 times 我希望整个代码运行30次

For i = 1 To 30

Worksheets("Salary Extract (SPs Only)").Range("B" & (1 + i)).Copy
Worksheets("Pitcher Matchup Analysis").Paste
Destination = Worksheets("Pitcher Matchup Analysis").Range("B9")

For i = 1 To 9

Worksheets("Pitcher Matchup Analysis").Range("B" & (38 + i)).Copy
Worksheets("Batter Matchup Analysis").Paste
Destination = Worksheets("Batter Matchup Analysis").Range("B1")
Worksheets("Batter Matchup Analysis").Range("B88:AC88").Copy
Worksheets("Pitcher Matchup Analysis").Range("C" & (38 + i) & "AD" & (38 + i)).PasteSpecial xlPasteValues

Next i

Worksheets("Pitcher Matchup Analysis").Range("A71:R71").Copy
Worksheets("Pitcher Comparison").Range("A" & (1 + i) & "R" & (1 +  i)).PasteSpecial xlPasteValues

Next i

End Sub

Within each loop I'd like this block to loop 9 times 在每个循环中,我希望此块循环9次

For i = 1 To 9

Worksheets("Pitcher Matchup Analysis").Range("B" & (38 + i)).Copy
Worksheets("Batter Matchup Analysis").Paste
Destination = Worksheets("Batter Matchup Analysis").Range("B1")
Worksheets("Batter Matchup Analysis").Range("B88:AC88").Copy
Worksheets("Pitcher Matchup Analysis").Range("C" & (38 + i) & "AD" & (38 + i)).PasteSpecial xlPasteValues

Next i

My guess is this is a fairly simple fix but I'm not sure and couldn't figure it out looking at other questions. 我的猜测是,这是一个相当简单的修复程序,但我不确定,也无法通过查看其他问题来解决。

At first glance the issue is that you can't use the counter "i" for both loops. 乍一看,问题在于两个循环都不能使用计数器“ i”。 Change one of the counters to "j" and all references in the inner loop to "j" as well. 将其中一个计数器更改为“ j”,并将内部循环中的所有引用也更改为“ j”。 I'm not sure if there are other problems but that one jumps out at me. 我不确定是否还有其他问题,但是那会跳到我身上。

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

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