使用不断变化的单元格范围循环所需的代码

时间:2014-04-13 22:50:45

标签: vba excel-vba excel

好的,所以我在思考并提出了一个更好的解决方案,如果可行的话。那个宏怎么样会在评论中做这个呢?

    Sub CopyCombinationForLP()
    ' CopyCombinationForLP Macro
    ' Copy Combinations For Linear Programming Working And Copy Results in New Sheet
    ' Need this part to loop for a number of times depending on "cell value P1":

Sheets("Combinations Prior LP").Select
Range("P6:Z6").Select 'need this range to change +1 row every loop for a total of "cell value in P1" in sheet "Combination Prior LP
Selection.Copy
Sheets("Linear Programming Combination ").Select
Range("A1").Select 'Need this range stays the same for each loop
ActiveSheet.Paste Link:=True
Sheets("LP Combination 1 ").Select
Range("A2:G32").Select 'need this range to stay the same for each loop
Application.CutCopyMode = False
Selection.Copy
Sheets("Linear Programming Combination ").Select
Range("A2").Select 'Need this range to stay the same for each loop
ActiveSheet.Paste
Range("I10").Select 'This one stays the same foe every loop
Range("B32:E32").Select 'Need this range to stay the same for each loop
Application.CutCopyMode = False
Selection.Copy
Sheets("Linear Programming Results").Select
Range("A2").Select 'need this one to shift +1 row every loop
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Combinations Prior LP").Select

End Sub

我认为这是一个可行的解决方案,因为我只需要线性编程组合的结果表。我可以达到的最大组合是10000。 以下链接到我试图复制和粘贴的截图:

第一步: https://dl.dropboxusercontent.com/u/83126653/Combinations%20Prior%20LP.png

第二步: https://dl.dropboxusercontent.com/u/83126653/Linear%20Programming%20Combination.png

希望这更有帮助。请帮帮我

2 个答案:

答案 0 :(得分:1)

尝试类似:

Sub CopyCombinationForLP()
    ' CopyCombinationForLP Macro
    ' Copy Combinations For Linear Programming Working And Copy Results in New Sheet
    ' Need this part to loop for 10000 times with the ranges changing accordingly:
    Dim Ctr As Long
    For Ctr = 0 To 99999
        Sheets("Combinations Prior LP").Select
        Range("P6:Z6").Offset(Ctr, 0).Select    'need this range to change +1 row every loop for 10000 rows (next one would be P7:Z7)
        Selection.Copy
        Sheets("Linear Programming Combination ").Select
        Range("A1").Offset(0, Ctr * 12).Select  'Need this range to change +12 columns every loop (next would be cell M1)
        ActiveSheet.Paste Link:=True
        Sheets("LP Combination 1 ").Select
        Range("A2:G32").Select     'need this range to stay the same for each loop
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Linear Programming Combination ").Select
        Range("A2").Offset(0, Ctr * 12).Select     'Need this range to change +12 columns every loop (next would be cell M2)
        ActiveSheet.Paste
        Range("I10").Select     'This one stays the same foe every loop
        Range("A32:G32").Offset(0, Ctr * 12).Select     'Need this one to change +12 from first cell of range (next would be M32:S32)
        Application.CutCopyMode = False
        Selection.Copy
        Sheets("Linear Programming Results").Select
        Range("A2").Offset(Ctr, 0).Select     'need this one to shift +1 row every loop
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                      :=False, Transpose:=False
        Sheets("Combinations Prior LP").Select
    Next
End Sub

然而这不起作用,因为早期版本的Excel有255列,甚至最新版本的Excel也只有16384列;您的'Need this range to change +12 columns every loop (next would be cell M2)类型注释会导致大约1366个周期出现溢出。所以,要么你没有告诉我们你的数据,要么你得到错误的数量(10000而不是1000)。

答案 1 :(得分:0)

这已经得到解决感谢Monty wild的一点帮助。我正在寻找的代码是:

  Sub ResultsOfCombinations()
' CopyCombinationForLP Macro
' Copy Combinations For Linear Programming Working And Copy Results in New Sheet
' Need this part to loop for a number of times depending on "cell value P1":
       Dim Ctr As Long
       For Ctr = 0 To Sheets("Combinations Prior LP").Range("P1").Value
             Sheets("Combinations Prior LP").Select
             Range("P6:Z6").Offset(Ctr, 0).Select    'need this range to change +1 row every loop for Cell Value in P1 in sheet Combination Prior LP rows (next one would be P7:Z7)
             Selection.Copy
             Sheets("Linear Programming Combination ").Select
             Range("A1").Select
             ActiveSheet.Paste Link:=True
             Sheets("LP Combination 1 ").Select
             Range("A2:G32").Select     'need this range to stay the same for each loop
             Application.CutCopyMode = False
             Selection.Copy
             Sheets("Linear Programming Combination ").Select
             Range("A2").Select  'Need this range to stay the same for every loop
             ActiveSheet.Paste
             Range("I10").Select     'This one stays the same foe every loop
             Range("B32:E32").Select  'Need this range to stay the same for every loop
             Application.CutCopyMode = False
             Selection.Copy
             Sheets("Linear Programming Results").Select
             Range("A2").Offset(Ctr, 0).Select     'need this one to shift +1 row every loop
             Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                                                  :=False, Transpose:=False
             Sheets("Combinations Prior LP").Select
       Next
End Sub