2014-04-13 49 views
0

好吧,所以我想,如果可行的話,想出了一個更好的解決方案。宏觀情況如何能像這樣做評論中的事情?用變化的單元格範圍循環所需的代碼

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

希望這有助於更多。請幫我

+0

可insteresing:怎樣避免使用選擇/活動語句(http://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-選擇) –

+0

第三步: https://dl.dropboxusercontent.com/u/83126653/LP%20Combination%201.png Fouth步驟: https://dl.dropboxusercontent.com/u/83126653/Linear %20Programming%20Results.png –

回答

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)。

+0

+1 Monty那一列的評論肯定是一個錯誤 –

+0

我更新了由於列問題的問題。你認爲你可以再次幫助我嗎?我發佈了截圖,可能會幫助您查看我正在處理的內容。提前致謝。 –

0

這已解決得益於蒙蒂野外的一點幫助。我一直在尋找的代碼是這樣的:

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