dynamic(整行数)插入复制的单元格宏

时间:2013-09-06 13:49:55

标签: excel-vba range rows vba excel

我有其他人制作的以下宏:


Sub test2()
Dim n As Integer, rng As Range
'n = InputBox("type the value of n")
Set rng = Range("a1")
rng.Select
line2:
n = InputBox("type no. of times you want to be repeated minus 1 for e.g if you wnat to be repeated 3 times type 2")
Range(rng.Offset(1, 0), rng.Offset(n, 0)).EntireRow.Insert
Range(rng, rng.End(xlToRight)).Copy
Range(rng, rng.Offset(n, 0)).PasteSpecial
Set rng = rng.Offset(n + 1, 0)
If rng = "" Then
GoTo line1
Else
GoTo line2
End If
line1:
Application.CutCopyMode = False
Range("a1").Select
MsgBox "macro over"
Stop


End Sub

  1. 我希望范围选择是动态的,即在上面的代码中,它硬编码为“a1”,但由于我想一次又一次地重复宏,我想通过选择它来每次选择不同的起点我的鼠标点击。

  2. 此外,当我完成复制它重启的单元格时,我想在我复制一次后停止宏。然后选择新的起点选择一行,然后将其复制x次

  3. 感谢您的提前帮助

1 个答案:

答案 0 :(得分:0)

您提供的代码有一些奇怪的逻辑。试图根据你的需要改变它我改变了一点,然后你要求。我希望这就是你现在所需要的。看看子内的一些评论。

Sub test2()
Dim n As Integer, rng As Range

    'new section >>
    On Error GoTo EH
    Set rng = Application.InputBox("Select any cell/cells within range to copy", Type:=8)
    '<<---

rng.Select

line2:
n = InputBox("type no. of times you want to be repeated minus 1 for e.g if you wnat to be repeated 3 times type 2")
Range(rng.Offset(1, 0), rng.Offset(n, 0)).EntireRow.Insert
Range(rng, rng.End(xlToRight)).Copy
Range(rng, rng.Offset(n, 0)).PasteSpecial

'Selection code:
Rng.offset(n,0).select

    'this section is not necessary>>
    'Set rng = rng.Offset(n + 1, 0)
    'If rng = "" Then
    'GoTo line1
    'Else
    'GoTo line2
    'End If

line1:
Application.CutCopyMode = False
    'range("a1").Select 'i don't think you need it
MsgBox "macro over"

    'Stop is not neede

Exit Sub
EH:
    MsgBox "Sub interrupted"

End Sub
相关问题