VBA 循环增量的最佳方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8793782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA whats the best way to Increment in loop
提问by user1139384
What would be a good way to increment each of my ranges for example I need x to copy Range B11:to G20 and paste it to C10 for the second loop. Each Variable has different increments.
什么是增加我的每个范围的好方法,例如我需要 x 将范围 B11: 复制到 G20 并将其粘贴到 C10 以进行第二次循环。每个变量都有不同的增量。
Im using excel 2010
我使用的是 excel 2010
Any Help is appreciated cause I'm horrible with Visual Basic.
感谢任何帮助,因为我对 Visual Basic 很糟糕。
Dim i As Range, j As Range, k As Range
Dim x As Range, y As Range
Dim Num As Integer
Num = 94
Set x = Sheets("Sum Data").Range("B1:G10")
Set j = Sheets("PNA Physical Needs Summary Data").Range("C4")
Set i = Sheets("PNA Physical Needs Summary Data").Range("B4:B9")
Set k = Sheets("Sum Data").Range("A1")
Set p = Sheets("PNA Physical Needs Summary Data").Range("P3:P8")
Set e = Sheets("PNA Physical Needs Summary Data").Range("A4:A9")
Do
Sheets("Sum Data").Select
x.Copy
Sheets("PNA Physical Needs Summary Data").Select
j.Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=True
p.Select
Selection.Copy
i.Select
ActiveSheet.Paste
Sheets("Sum Data").Select
k.Select
Application.CutCopyMode = False
Selection.Copy
Sheets("PNA Physical Needs Summary Data").Select
e.Activate
ActiveSheet.Paste
Num = Num - 1
Loop Until Num = 0
回答by Tim Williams
You can use Offset(). Eg:
您可以使用 Offset()。例如:
Set x = x.Offset(rowOffset, colOffset)
Also - you don't need to select to copy ranges:
此外 - 您不需要选择复制范围:
p.Copy i
is the same as:
是相同的:
p.Select
Selection.Copy
i.Select
ActiveSheet.Paste