跳到主要內容

如何在Excel中多次復制和插入行或重複x次復制行?

在日常工作中,您是否曾經嘗試過複製一行或每一行,然後在工作表中當前數據行的下方多次插入? 例如,我有一個單元格區域,現在,我想複製每一行並將它們粘貼3次到下一行,如下圖所示。 您如何在Excel中處理這項工作?


使用VBA代碼多次復制並插入特定行

如果您只想重複x特定的一行x,下面的VBA代碼可能會幫助您,請按照下列步驟操作:

1。 指定要復制並插入多次的行,然後按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:多次復制一個特定行:

Sub test()
'Updateby Extendoffice
    Dim xCount As Integer
LableNumber:
    xCount = Application.InputBox("Number of Rows", "Kutools for Excel", , , , , , 1)
    If xCount < 1 Then
        MsgBox "the entered number of rows is error, please enter again", vbInformation, "Kutools for Excel"
        GoTo LableNumber
    End If
    ActiveCell.EntireRow.Copy
    Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(xCount, 0)).EntireRow.Insert Shift:=xlDown
    Application.CutCopyMode = False
End Sub

3。 粘貼代碼後,請按 F5 鍵運行此代碼,並彈出一個提示框,提醒您輸入要重複的次數,請參見屏幕截圖:

4。 然後點擊 OK 按鈕,並且在所選行下方插入了三個新的複制行,請參見屏幕截圖:


輕鬆地根據特定數字多次復制並插入每一行

通常,除了手動處理複製和插入操作外,沒有很好的方法可以多次復制和插入行。 但是,隨著 Excel的Kutools's 根據單元格值複製行/列 功能,您可以輕鬆解決此問題。 點擊下載Kutools for Excel!

Excel的Kutools:具有300多個方便的Excel加載項,可以在60天內免費試用,沒有任何限制。 立即下載並免費試用!


使用VBA代碼多次復制並插入每一行

要在一個範圍內多次復制每一行,您可以應用以下VBA代碼,請執行以下操作:

1。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊 窗口。

VBA代碼:每行重複多次:

Sub insertrows()
'Updateby Extendoffice
Dim I As Long
Dim xCount As Integer
LableNumber:
xCount = Application.InputBox("Number of Rows", "Kutools for Excel", , , , , , 1)
If xCount < 1 Then
MsgBox "the entered number of rows is error ,please enter again", vbInformation, "Kutools for Excel"
GoTo LableNumber
End If
For I = Range("A" & Rows.CountLarge).End(xlUp).Row To 2 Step -1
Rows(I).Copy
Rows(I).Resize(xCount).Insert
Next
Application.CutCopyMode = False
End Sub

3。 然後按 F5 鍵以運行此代碼,並彈出一個提示框,提醒您輸入要為每個記錄重複的次數,請參見屏幕截圖:

4。 然後點擊 OK,並且每行已復制並插入到活動行下方3次,請參見屏幕截圖:

備註:在上面的代碼中, A 表示數據范圍從A列開始(如果您的數據從A列開始) K, 請更換 AK 作為你的需要。


具有令人敬畏的功能,可根據特定數字多次復制並插入每一行

也許您不熟悉VBA代碼,或者擔心該代碼會使您的數據崩潰。 在這裡,我將介紹一個有用的功能, Excel的Kutools's 根據單元格值複製行/列,使用此實用程序,您可以根據指定的編號快速復制並插入行。

提示:要應用此 根據單元格值複製行/列 功能,首先,您應該下載 Excel的Kutools,然後快速輕鬆地應用該功能。

安裝後 Excel的Kutools,請這樣做:

1。 在數據旁邊的單元格列表中輸入要復制行的重複數字,請參見屏幕截圖:

2。 點擊 庫工具 > 插入 > 根據單元格值複製行/列,請參見屏幕截圖:

3。 在 複製並插入行和列 對話框中選擇 複製並插入行 在選項 類別 部分,然後選擇要復制的數據范圍,然後指定重複時間以復制行,請參見屏幕截圖:

4。 然後,點擊 Ok or 登記 按鈕,您將根據需要獲得以下結果:

點擊下載Kutools for Excel並立即免費試用!

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (32)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Halo

Ik heb een zoek programma gemaakt met VBA die de cel van het gezocht item groen maakt
Bijv. saldo 1 euro komt 3x voor in een blad ( B2 t/m F16 )
Dan maakt hij 3x de cel Saldo 1 euro groen
Nou zou ik graag willen dat hij deze 3 items kopieert op hetzelfde blad in kolom H
Wie weet hoe je dit moet schrijven in een Macro

B.v.d. Michel
This comment was minimized by the moderator on the site
Bonjour,
J'ai pu tester ce code (celui en haut du post) pour dupliquer plusieurs lignes autant de fois que le nombre présent dans la dernière colonne. Cela fonctionne très bien, Merci.
Par contre, dans une des colonnes, il y a une date de début. Je voudrais que cette date s'incrémente automatiquement lors de la duplication.
Pouvez vous m'aider s'il vous plait, je ne trouve pas la réponse sur internet ?
Merci par avance.
This comment was minimized by the moderator on the site
It took more time to make google understand what I want, But after I found this page, it takes a few minutes to get what I was searching for. Many thanks 👍
This comment was minimized by the moderator on the site
I desire to have the VBA Code for Copy And Insert Each Row Multiple Times Based On A Specific Number , please assist, Thank you
This comment was minimized by the moderator on the site
Hi, Carl,To copy and insert the rows multiple times based on specific numbers, please apply the below code:<div data-tag="code">Sub CopyRow()
'UpdatebyExtendoffice
Dim xRg As Range
Dim xCRg As Range
Dim xFNum As Integer
Dim xRN As Integer
On Error Resume Next
SelectRange:
xTxt = ActiveWindow.RangeSelection.Address
Set xRg = Application.InputBox("Select the list of numbers to copy the rows based on: ", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub

If xRg.Columns.Count > 1 Then
MsgBox "Please select single column!"
GoTo SelectRange
End If
Application.ScreenUpdating = False
For xFNum = xRg.Count To 1 Step -1
Set xCRg = xRg.Item(xFNum)
xRN = CInt(xCRg.Value)
With Rows(xCRg.Row)
.Copy
.Resize(xRN).Insert
End With
Next
Application.ScreenUpdating = True
End SubPlease try, hope it can help you!
This comment was minimized by the moderator on the site
skyyang not work!
This comment was minimized by the moderator on the site
This code works great. I have a situation where I am using Excel as a quote form. Worksheet one is the actual bid, and worksheet two is our cost page. When I insert the copied rows, I need it to do so on both pages. I have tried adding some code to select both pages, but it does not seem to work. Any help is greatly appreciated.
This comment was minimized by the moderator on the site
For the second VBA code (VBA code: Duplicate each row multiple times) I keep getting as run time error: 1004
This comment was minimized by the moderator on the site
Hello, is it possible to use different sizes(values) for each row? I am trying using vector, but it is not working.
For instance :
Rows(I).Resize(xCount(y) ).Insert

Where the values saved in xCount(y) are read from a table.
This comment was minimized by the moderator on the site
Hello, Guss,
May be the below article can help you, please check it:
https://www.extendoffice.com/documents/excel/4054-excel-duplicate-rows-based-on-cell-value.html
This comment was minimized by the moderator on the site
hi everyone.. Thank you in advance for you help!!

The VBA code to duplicate one specific row multiple times work perfectly until you use a filter. I wonder if someone can help me to solve this issue. I need a code that work even if you have some values filtered. I am using a large amount of data that is sort by locations. The code partially work, it pastes the number of row desire but no data or format when I have applied a filter.
This comment was minimized by the moderator on the site
No funciona con Windows 2019, deseo usar la función en mención pero simplemente no hace absolutamente nada
This comment was minimized by the moderator on the site
Copy And Insert Each Row Multiple Times With VBA Code

In VBA code how can I select the starting row
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations