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

จะทำซ้ำแถวตามค่าของเซลล์ในคอลัมน์ได้อย่างไร

ตัวอย่างเช่นฉันมีช่วงของข้อมูลซึ่งมีรายการตัวเลขในคอลัมน์ D และตอนนี้ฉันต้องการทำซ้ำทั้งแถวหลายครั้งโดยยึดตามค่าตัวเลขในคอลัมน์ D เพื่อให้ได้ผลลัพธ์ต่อไปนี้ ฉันจะคัดลอกแถวหลาย ๆ ครั้งตามค่าของเซลล์ใน Excel ได้อย่างไร

ทำซ้ำแถวหลายครั้งตามค่าของเซลล์ด้วยรหัส VBA

คัดลอกและแทรกแถวตามจำนวนครั้งที่ระบุด้วยเครื่องมือที่มีประโยชน์ - Kutools for Excel


ทำซ้ำแถวหลายครั้งตามค่าของเซลล์ด้วยรหัส VBA

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

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

2. คลิก สิ่งที่ใส่เข้าไป > โมดูลและวางรหัสต่อไปนี้ในไฟล์ โมดูล หน้าต่าง.

รหัส VBA: ทำซ้ำแถวหลายครั้งตามค่าของเซลล์:

Sub CopyData()
'Updateby Extendoffice
    Dim xRow As Long
    Dim VInSertNum As Variant
    xRow = 1
    Application.ScreenUpdating = False
    Do While (Cells(xRow, "A") <> "")
        VInSertNum = Cells(xRow, "D")
        If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
           Range(Cells(xRow, "A"), Cells(xRow, "D")).Copy
           Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "D")).Select
           Selection.Insert Shift:=xlDown
           xRow = xRow + VInSertNum - 1
        End If
        xRow = xRow + 1
    Loop
    Application.ScreenUpdating = False
End Sub

3. จากนั้นกด F5 คีย์ในการเรียกใช้รหัสนี้ทั้งแถวซ้ำกันหลายครั้งตามค่าเซลล์ในคอลัมน์ D ตามที่คุณต้องการ

หมายเหตุ: ในรหัสด้านบนตัวอักษร A ระบุคอลัมน์เริ่มต้นของช่วงข้อมูลของคุณและตัวอักษร D คือตัวอักษรคอลัมน์ที่คุณต้องการทำซ้ำแถวตาม โปรดเปลี่ยนตามความต้องการของคุณ

คัดลอกและแทรกแถวตามจำนวนครั้งที่ระบุด้วยเครื่องมือที่มีประโยชน์ - Kutools for Excel

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

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

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

🤖 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 (41)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi All,
Can anyone give me the code to copy whole table at the same time?.
This comment was minimized by the moderator on the site
Hello, Aparna,
Maybe the following article can help you.
https://www.extendoffice.com/documents/excel/3682-excel-copy-and-insert-row-multiple-times.html#a2
Please view it, if you have any other problem, please comment here.
This comment was minimized by the moderator on the site
Is there any way to get this to work on a shared workbook? it works perfectly until I share the workbook then i get "insert method of range class failed"
This comment was minimized by the moderator on the site
Bonjour,
Merci pour ce code qui fonctionne bien.
Par contre dans mon tableau j'ai une date pour chaque ligne:
J'aimerai qu'elle s'incrémente au fur et à mesure des duplications de lignes et en automatique, car il y a plus de 1000 dossiers différents.

N° dossier Date Nb de jours
2101007 29/01/2021 49
2110002 11/10/2021 22
2008006 31/08/2020 132

pour donner:
N° dossier Date Nb de jours
2101007 29/01/2021 49
2101007 30/01/2021 49
...

Est-ce possible ?
Merci par avance.
This comment was minimized by the moderator on the site
Thank you so much for this!
This comment was minimized by the moderator on the site
What if I wanted to do the above (nice job btw) but what if I wanted to change the dates by “X” days when I add the rows? Like a reoccurring event in a calendar. 
This comment was minimized by the moderator on the site
This is PERFECTION! Short Sweet and to the point as well as easily adaptable!
THANK YOU!
This comment was minimized by the moderator on the site

this is wondeful thank you so much
This comment was minimized by the moderator on the site
I tried running it by pressing F5 and a pop up message below:
"Compile Error:Sub or function not defined."
What am I doing wrong? I adjusted column A and changed A & D as needed.
This comment was minimized by the moderator on the site
Hi, this does not work for me. I copy the code, change the column letter D to the column letter that I want to duplicate rows based upon, and... nothing happens when I run the code. I have enabled macros and tried on two different computers. What am I doing wrong?
This comment was minimized by the moderator on the site
Hi, Sean,
Note: In the above code, the letter A indicates the start column of your data range, and the letter D is the column letter that you want to duplicate the rows based on. Please change them to your need.
Have you adjust the column A of your data? please check it, thank you!

This comment was minimized by the moderator on the site
Is it possible to adjust the code to create a new sheet, keeping the original sheet untouched?
This comment was minimized by the moderator on the site
Hello,If you want to keep your original data, please copy and paste the data into another new sheet, and then apply the code as you need.
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