2017-06-05 68 views
0

我有一個模板工作表。然後,根據另一個工作表上的輸入,模板表將被複制N次並重命名爲N.我現在要做的是創建一個循環,以將工作表中的數據傳輸到目標。循環N頁數 - Excel VBA

因此,例如,從「開始」工作表(其中輸入數字來自),假設我輸入了5,因爲我想從模板中選擇5張。這將更名爲1 ... 2 ... 3 ... 4 ... 5。

然後,之後,一旦使用了這些編號的工作表,我想將這些工作表中的數據複製到目標工作表。我怎麼做?

手中有工作表和以下代碼的複製和重命名代碼。

PS。如何簡化插入N個單元格到左側?非常感謝。 :)

Sub CreateLoaderBeta1() 

     Dim origin  As Worksheet 
     Dim destination As Worksheet 
     Dim desrow  As Long 
     Dim descol  As Long 
     Dim descolstart As Long 
     Dim origrow  As Long 
     Dim origcol  As Long 
     Dim rang  As Range 
     Dim C   As Range 
     Dim qual  As Integer 

     Set origin = Sheets("1") 
     Set destination = Sheets("OFFLIMITS") 
     desrow = 1 
     descol = 1 
     origrow = 18 
     origcol = 32 
     Set rng = origin.Range("AF18:af47") 
     total = WorksheetFunction.SUM(origin.Range("AF18:AF47")) 
     descolstart = destination.cells(desrow, Columns.Count).End(xlToLeft).column 
     descolnext = descolstart + 1 

     If total > 0 Then 

       For Each C In rng 
        If C = 14 Then 

        'No,Type,Amount,Distribution Account,Description,Product Type,VAT,Ewt,Net Purchases,Yes/No,Enter 

          destination.cells(desrow, descolstart).Value = origin.cells(origrow, 1).Value 'to copy sequence number 
          destination.cells(desrow, descolstart + 1).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 2).Value = origin.cells(origrow, 4).Value 'type 
          destination.cells(desrow, descolstart + 3).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 4).Value = origin.cells(origrow, 27).Value 'amount 
          destination.cells(desrow, descolstart + 5).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 6).Value = origin.cells(origrow, 6).Value 'distribution account 
          destination.cells(desrow, descolstart + 7).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 8).Value = origin.cells(origrow, 30).Value 'description 
          destination.cells(desrow, descolstart + 9).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 10).Value = origin.cells(origrow, 9).Value 'product type 
          destination.cells(desrow, descolstart + 11).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 12).Value = origin.cells(origrow, 10).Value 'VAT 
          destination.cells(desrow, descolstart + 13).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 14).Value = origin.cells(origrow, 11).Value 'wht 
          destination.cells(desrow, descolstart + 15).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 16).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 17).Value = "Net Purchases" 'to Net Purchases 
          destination.cells(desrow, descolstart + 18).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 19).Value = origin.cells(origrow, 13).Value 'wht 
          destination.cells(desrow, descolstart + 20).Value = "\{TAB}" 'to insert tab 
          destination.cells(desrow, descolstart + 21).Value = "\{ENTER}" 'to insert tab 
          destination.cells(desrow, descolstart + 22).Value = "\{DOWN}" 'to insert tab 

          descolstart = descolstart + 23 
          origrow = origrow + 1 

        End If 
       Next C 

       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, 1).insert Shift:=xlToRight 
       destination.cells(desrow, destination.cells(desrow, Columns.Count).End(xlToLeft).column).Value = "\%C" 
       destination.cells(desrow, destination.cells(desrow, Columns.Count).End(xlToLeft).column + 1).Value = "\%V" 
       destination.cells(desrow, destination.cells(desrow, Columns.Count).End(xlToLeft).column + 1).Value = "\%K" 

       'Call headers 

         Dim originWS As Worksheet 
         Dim desWS  As Worksheet 
         Dim rowNO  As Integer 

         Set originWS = origin 'CHANGE THIS TO SHEET NUMBER 
         Set desWS = destination 
         rowNO = desrow 

         desWS.Range("A" & rowNO).Value = originWS.Range("C1").Value 
         desWS.Range("c" & rowNO).Value = originWS.Range("C2").Value 
         desWS.Range("e" & rowNO).Value = Worksheets("Start").Range("C22").Value 
         desWS.Range("H" & rowNO).Value = originWS.Range("C3").Value 
         desWS.Range("J" & rowNO).Value = originWS.Range("C4").Value 
         desWS.Range("L" & rowNO).Value = originWS.Range("C4").Value 
         desWS.Range("N" & rowNO).Value = originWS.Range("C5").Value 
         desWS.Range("P" & rowNO).Value = originWS.Range("C6").Value 
         desWS.Range("R" & rowNO).Value = originWS.Range("C7").Value 
         desWS.Range("T" & rowNO).Value = originWS.Range("C8").Value 

         'to insert the keystrokes 
         desWS.Range("B" & rowNO).Value = "\{TAB}" 
         desWS.Range("D" & rowNO).Value = "\{TAB}" 
         desWS.Range("F" & rowNO).Value = "\{TAB}" 
         desWS.Range("G" & rowNO).Value = "\{TAB}" 
         desWS.Range("I" & rowNO).Value = "\{TAB}" 
         desWS.Range("K" & rowNO).Value = "\{TAB}" 
         desWS.Range("M" & rowNO).Value = "\{TAB}" 
         desWS.Range("O" & rowNO).Value = "\{TAB}" 
         desWS.Range("Q" & rowNO).Value = "\{TAB}" 
         desWS.Range("S" & rowNO).Value = "\{TAB}" 
         desWS.Range("U" & rowNO).Value = "\%2" 

       destination.Columns("J:J").NumberFormat = "dd-mmm-yy" 
       destination.Columns("L:L").NumberFormat = "dd-mmm-yy" 

     Else 'Do nothing 

     End If 

     End Sub 
+0

如果你已經有了一定的代碼在你的問題中,哪一點你特別需要幫助?在回答你的「p.s.」時,你可以做「Dim i As Long」; '對於我= 1到21'; 'destination.cells(desrow,1).insert Shift:= xlToRight'; '接下來我',每個分號是一個新的行。 – Wolfie

+0

@Wolfie有效,但效率非常低。調整您的範圍對象並僅執行1次插入會更好。這樣,您只能與工作表交互一次,而用一個循環(在本例中)執行21個I/O操作來引用單元格和21個I/O操作以插入。 –

回答

1

這個問題實際上是三重的。第一部分:使用名稱「1」,「2」等生成n張表,最多爲「n」。讓我們說在範圍A1的工作表(「Sheet1」)中,您設置了您希望生成的工作表數量。然後,該腳本將是:

Sub GenerateSheets() 
Dim i as Integer 
Dim numberOfSheets as Integer 
Dim ws as Worksheet 

numberOfSheets = Worksheets("Sheet1").Range("A1").value 

For i = 1 to numberOfSheets 
    Set ws = Worksheets.add() 
    With ws 
     .name = i 
     'Do other stuff with the new sheet 
    End With 
Next i 
End Sub 

如果這些新的牀單需要一個模板表的副本,你可以這樣做:

Sub GenerateSheets() 
Dim i As Integer 
Dim numberOfSheets As Integer 
Dim ws As Worksheet 

numberOfSheets = Worksheets("Sheet1").Range("A1").Value 

For i = 1 To numberOfSheets 
    Worksheets("Template").Copy After:=Worksheets("Template") 
    Set ws = Worksheets(Worksheets("Template").Index + 1) 
    With ws 
     .Name = i 
     'Do other stuff with the new sheet 
    End With 
Next i 
End Sub 

第二個問題是:我如何從這個表中獲取數據回到我的目的地表單?您可以將「目標」中的值設置爲等於工作表中的值,或者複製整個單元格。根據你的示例腳本,我會說第一個有偏好。 比方說,您希望將新工作表中範圍A1的值複製到目標範圍A1。然後,您可以修改上述如下:

Sub GenerateSheets() 
Dim i as Integer 
Dim numberOfSheets as Integer 
Dim ws as Worksheet 
Dim destination as Worksheet 

numberOfSheets = Worksheets("Sheet1").Range("A1").value 

Set destination = Worksheets("Destination") 

For i = 1 to numberOfSheets 
    Set ws = Worksheets.add() 
    With ws 
     .name = i 
     .Range("A1") = "Some value" 
     destination.Range("A1").value = .Range("A1").value ' = "Some value" 
     'Do other stuff with the new sheet 
    End With 
Next i 
End Sub 

問題的第三部分:「我如何簡化插入N個單元,左邊是」 這取決於你有多少細胞要插入,但讓我們說這是X單元,根據您現有的代碼將是調整的範圍內插入簡單的方法:

destination.cells(desrow, 1).Resize(1, X).insert Shift:=xlToRight