我有几百个具有唯一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