簡體   English   中英

動態(整行數)插入復制的單元格宏

[英]dynamic (number of entire rows) insert copied cells macro

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


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次

謝謝您的幫助

您提供的代碼具有一些奇怪的邏輯。 嘗試根據您的需求進行更改,我的更改量比您要求的多。 我希望這是您現在需要的。 在子頁面中也可以看到一些注釋。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM