【问题标题】:Copy a range of cells and insert multiple times复制一系列单元格并多次插入
【发布时间】:2017-12-19 17:22:21
【问题描述】:

我有数百个带有唯一 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 行x 1 在 start_cell 下方,然后 k 行 x 2 在 start_cell 下方,依此类推,直到达到数字 j。

然而,宏的作用是将 cell_rng 插入 start_cell 下方,然后在第一个 cell_rng 插入下方插入 cell_rng k+1 行**。 因此,如果 k = 5 且 start_cell = 1,宏将从单元 2 开始插入 cell_rng,单元 7 到 11 将没有插入,并且该过程将在单元 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我相信一切都在您的起点。 您应该在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-28
      相关资源
      最近更新 更多