VBA是增量循环的最佳方式

时间:2012-01-09 19:10:15

标签: excel vba loops excel-vba auto-increment

增加每个范围的好方法例如我需要x来复制范围B11:到G20并将其粘贴到C10以进行第二次循环。每个变量都有不同的增量。

我正在使用excel 2010

感谢任何帮助,因为我对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

1 个答案:

答案 0 :(得分:3)

您可以使用偏移()。例如:

Set x = x.Offset(rowOffset, colOffset)

此外 - 您无需选择复制范围:

p.Copy i

与:

相同
p.Select     
Selection.Copy     
i.Select     
ActiveSheet.Paste