2017-07-14 31 views
0

我有幾百個具有唯一ID號的接線盒,它們都包含相同的零件,我需要創建一個列表來顯示每個ID號下的零件列表。複製一系列單元格並插入多次

我今天早上剛剛開始使用VBA,並且從碎片拼湊了一個宏。

該宏執行以下操作: 提示用戶選擇要複製的單元格範圍(變量cell_rng)。 提示用戶選擇一個單元來啓動插入複製單元的過程(變量start_cell)。 提示用戶輸入應該輸入複製範圍的次數變量j)(總數爲 JB)。 提示用戶輸入包含在要複製的單元格範圍內的行數(變量k)。

宏應在原始列表的每行(接線盒ID)之間插入cell_rng。 它應該開始在start_cell下面插入cell_rng,然後在start_cell下面插入cell_rng k行(k表示複製的行數,因此在列表中下一個接線盒ID下面的單元的新位置),然後k在start_cell之下的行x 1,然後在start_cell之下的k行x 2,以此類推,直到達到數字j。

然而,宏所做的是在start_cell下面插入cell_rng,然後在第一個cell_rng插入點下面插入第**行的cell_rng行數(k + 1)。 因此,如果k = 5且start_cell = 1,則宏將從cell 2開始插入cell_rng,cell 7至11將沒有插入並且進程將在cell 9再次開始。 然後它繼續工作,因爲我希望它能夠在應該插入cell_rng的地方工作。

是否有人能夠幫助我獲得宏在原始列表中的每一行之後插入cell_rng? 道歉,如果解釋很難遵循,寫下你想要宏做什麼並不容易!

該宏的代碼如下。

Sub Insert_Copied_Cells() 
' 
'Insert_Copied_Cells 
'This Macro copies a range of cells and inserts x number of times below a    
specified cell. 
'The offset can be altered so that a copied range can be inserterd on 
multiple lines without changed data already present. 
' 
'This marco misses the first 6 row for some reason, this needs to be 
corrected somehow.......... 

' 
Dim i As Variant, j As Variant, k As Variant, l As Variant, cell_rng As 
Range, start_cell As Range 
'i = number of repeated entries required 
'j = number of repeated entries required 
'k = number of rows in the range of cells to be copied 
'l = number of repeated entries required 
'cell_rng = range of cells to be copied 
'start_cell = the cell below which the copied range should be inserted, this 
'is the reference cell for all the repetition of the range insertion 

Set cell_rng = Application.InputBox("Select Range to be Copied", "Obtain 
Range", Type:=8) 
'promts user to select a range to be copied 

Set start_cell = Application.InputBox("Select the First Cell Below Which 
Copied Range will be Entered", "Obtain Starting Cell", Type:=8) 
'promts user to select a cell to start the process 

    j = InputBox("Input Number of Entry Repetitions Required") 
'prompts user to enter number of repeated entries required 

    k = InputBox("Number of rows to be Copied") 
'prompts user to enter number of rows the selected range contains 

    l = k + 1 
'adds one onto number of rows to be copied to allow for next entry 

For i = 0 To j 
'run through the code below from i= 0 increasing by 1 until number j is reached, then stop. 

    cell_rng.Select 
'defines the range to select (range defined above at prompt) 
    Selection.Copy 
'copies the range of cells 
    start_cell.Offset((l * i), 0).Select 
'selects starting cell to paste range into 
    Selection.Insert Shift:=xlDown 
'inserts the selected range below the starting cell 

Next i 


End Sub 

回答

0

我相信這一切都在你開始的地方。 您應該在A2,A8,A14,A20,A26等插入 如果每個JBox包含相同的信息,爲什麼不只是設置k = 5,l = 6? start_cell應該是2.你是偏移量從0,0(6x0)開始,然後是6,0(6x1), 然後是12,0(6x2)等,這似乎是好的。我自己是一個業餘愛好者,我相信還有很多其他方法可以完成你正在做的事情,但我認爲只需要做一次就可以完成任務(只是猜測)。希望這會有所幫助,或讓你朝着正確的方向前進。

查看此答案,以瞭解@teylyn發佈的類似問題。非常簡單的編碼插入行。不錯,乾淨!

https://stackoverflow.com/a/36639509/7793894

相關問題