ไปยังเนื้อหาหลัก

วิธีคัดลอกและแทรกแถวหลาย ๆ ครั้งหรือทำซ้ำแถว x ครั้งใน Excel

ในการทำงานประจำวันของคุณคุณเคยพยายามคัดลอกแถวหรือแต่ละแถวแล้วแทรกหลาย ๆ ครั้งด้านล่างแถวข้อมูลปัจจุบันในแผ่นงานหรือไม่? ตัวอย่างเช่นฉันมีช่วงของเซลล์ตอนนี้ฉันต้องการคัดลอกแต่ละแถวและวาง 3 ครั้งในแถวถัดไปตามภาพหน้าจอต่อไปนี้ คุณจะจัดการกับงานนี้ใน Excel ได้อย่างไร?


คัดลอกและแทรกแถวเฉพาะหลาย ๆ ครั้งด้วยรหัส VBA

หากคุณต้องการทำซ้ำเพียงแถวเดียว x ครั้งโค้ด VBA ต่อไปนี้อาจช่วยคุณได้โปรดดำเนินการดังนี้:

1. ระบุแถวที่คุณต้องการคัดลอกและแทรกหลาย ๆ ครั้งจากนั้นกดไฟล์ ALT + F11 จากนั้นจะเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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 และแทรกแถวที่คัดลอกใหม่สามแถวใต้แถวที่เลือกดูภาพหน้าจอ:


คัดลอกและแทรกแต่ละแถวหลาย ๆ ครั้งตามจำนวนที่ระบุได้อย่างง่ายดาย

โดยปกติไม่มีวิธีการที่ดีในการคัดลอกและแทรกแถวหลาย ๆ ครั้งยกเว้นการคัดลอกและแทรกด้วยตนเอง แต่ด้วย Kutools สำหรับ Excel's ทำซ้ำแถว / คอลัมน์ตามค่าของเซลล์ คุณลักษณะนี้คุณสามารถแก้ปัญหานี้ได้อย่างง่ายดาย คลิกเพื่อดาวน์โหลด Kutools for Excel!

Kutools สำหรับ Excel: ด้วย Add-in ของ Excel ที่มีประโยชน์มากกว่า 300 รายการให้ทดลองใช้ฟรีโดยไม่มีข้อ จำกัด ใน 60 วัน ดาวน์โหลดและทดลองใช้ฟรีทันที!


คัดลอกและแทรกแต่ละแถวหลาย ๆ ครั้งด้วยรหัส VBA

หากต้องการทำซ้ำแต่ละแถวหลาย ๆ ครั้งในช่วงคุณสามารถใช้รหัส VBA ต่อไปนี้ได้โปรดทำตามด้านล่าง:

1. กด ALT + F11 จากนั้นจะเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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 หากข้อมูลของคุณเริ่มต้นที่คอลัมน์ Kโปรดเปลี่ยน A ไปยัง K ตามความต้องการของคุณ


คัดลอกและแทรกแต่ละแถวหลาย ๆ ครั้งตามจำนวนเฉพาะพร้อมคุณสมบัติที่ยอดเยี่ยม

บางทีคุณอาจไม่คุ้นเคยกับรหัส VBA หรือกังวลว่าโค้ดจะทำให้ข้อมูลของคุณผิดพลาด ที่นี่ฉันจะแนะนำคุณสมบัติที่มีประโยชน์ Kutools สำหรับ Excel's แถว / คอลัมน์ซ้ำกันตามค่าของเซลล์ด้วยยูทิลิตี้นี้คุณสามารถคัดลอกและแทรกแถวตามจำนวนที่คุณระบุได้อย่างรวดเร็ว

ทิปส์:ที่จะใช้สิ่งนี้ แถว / คอลัมน์ซ้ำกันตามค่าของเซลล์ ประการแรกคุณควรดาวน์โหลดไฟล์ Kutools สำหรับ Excelแล้วใช้คุณสมบัตินี้อย่างรวดเร็วและง่ายดาย

หลังจากการติดตั้ง Kutools สำหรับ Excelโปรดทำตามนี้:

1. ป้อนตัวเลขซ้ำที่คุณต้องการทำซ้ำแถวในรายการเซลล์ข้างข้อมูลของคุณดูภาพหน้าจอ:

2. คลิก Kutools > สิ่งที่ใส่เข้าไป > แถว / คอลัมน์ซ้ำกันตามค่าของเซลล์ดูภาพหน้าจอ:

3. ใน คัดลอกและแทรกแถวและคอลัมน์ ใหเลือก คัดลอกและแทรกแถว ตัวเลือกใน ชนิดภาพเขียน จากนั้นเลือกช่วงข้อมูลที่คุณต้องการทำซ้ำจากนั้นระบุเวลาทำซ้ำเพื่อทำซ้ำแถวดูภาพหน้าจอ:

4. จากนั้นคลิก Ok or ใช้ คุณจะได้รับผลลัพธ์ต่อไปนี้ตามที่คุณต้องการ:

คลิกเพื่อดาวน์โหลด Kutools สำหรับ Excel และทดลองใช้ฟรีทันที!

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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