0

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


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 # 次

提前感谢您的帮助

4

1 回答 1

0

您提供的代码有一些奇怪的逻辑。尝试根据您的需要进行更改,我更改的内容比您要求的要多。我希望这是你现在需要的。也可以在 sub 中查看一些评论。

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
于 2013-09-06T14:55:28.923 回答