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

วิธีการเปลี่ยนคอลัมน์เป็นแถวและแทรกแถวใหม่เพื่อเติมข้อมูลใน Excel

ในบทความนี้ฉันแนะนำรหัส VBA เพื่อเปลี่ยนคอลัมน์เป็นแถวและแทรกแถวใหม่เพื่อกรอกข้อมูลตามภาพด้านล่างที่แสดงหากคุณสนใจในการดำเนินการนี้โปรดไปที่ด้านล่างเพื่อดูรายละเอียดเพิ่มเติม
doc transpose แทรกแถว 1

เปลี่ยนและแทรกแถวด้วย VBA


ลูกศรสีฟ้าฟองขวา เปลี่ยนและแทรกแถวด้วย VBA

ไม่มีวิธีโดยตรงในการเปลี่ยนช่วงและแทรกแถวเพื่อเติมข้อมูลเดียวกัน แต่ที่นี่ฉันสามารถแนะนำรหัส VBA เพื่อแก้ปัญหานี้ได้อย่างรวดเร็ว

1 กด Alt + F11 กุญแจเพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 คลิก สิ่งที่ใส่เข้าไป > โมดูลแล้ววางโค้ด VBA ด้านล่างลงในหน้าต่างโมดูล ดูภาพหน้าจอ:

VBA: เปลี่ยนและแทรกแถว

 Sub TransposeInsertRows()
'UpdatebyExtendoffice20161125
    Dim xRg As Range
    Dim i As Long, j As Long, k As Long
    Dim x As Long, y As Long
    Set xRg = Application.InputBox _
    (Prompt:="Range Selection...", _
    Title:="Kutools For Excel", Type:=8)
    Application.ScreenUpdating = False
    x = xRg(1, 1).Column + 2
    y = xRg(1, xRg.Columns.Count).Column
    For i = xRg(xRg.Rows.Count, 1).Row To xRg(1, 1).Row Step -1
        If Cells(i, x) <> "" And Cells(i, x + 1) <> "" Then
            k = Cells(i, x - 2).End(xlToRight).Column
            If k > y Then k = y
            For j = k To x + 1 Step -1
                Cells(i + 1, 1).EntireRow.Insert
                With Cells(i + 1, x - 2)
                    .Value = .Offset(-1, 0)
                    .Offset(0, 1) = .Offset(-1, 1)
                    .Offset(0, 2) = Cells(i, j)
                End With
                Cells(i, j).ClearContents
            Next j
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

 

doc transpose แทรกแถว 2

3 กด F5 เพื่อเรียกใช้รหัสจากนั้นเลือกช่วงข้อมูลที่คุณต้องการเปลี่ยนและแทรกแถวในกล่องโต้ตอบ popping ดูภาพหน้าจอ:
doc transpose แทรกแถว 3

4 คลิก OK. และช่วงที่เลือกได้ถูกแปลงตามภาพด้านล่างที่แสดง
doc transpose แทรกแถว 4

หมายเหตุ: ก่อนที่จะเรียกใช้รหัสคุณควรบันทึกสำเนาข้อมูลต้นฉบับไว้ก่อน

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

doc เปลี่ยนข้ามไปที่รายการ

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

🤖 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 (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Compiler errorSyntax Error
Dim xRg As Range
Dim i As Long, j As Long, k As Long
Dim x As Long, y As Long

This comment was minimized by the moderator on the site
The above code works great. It keeps the first two columns. How would I change the code so that it only keeps the first column?
This comment was minimized by the moderator on the site
How can I transpose the data every two cells

The initial exercise is:


Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

and I need it to look like this:

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how can I make data 3 and 4 remain in the same column:


the initial exercise is:

Class1 | Name | Nancy | Judy | Lily | Daniel
Class2 | Name | Bruce | Uee | Perter | Daniel
Class3 | Name | kate | Kelly | Wenddy | Daniel

I need to transpose it like this

Class1 | Name | Nancy | Judy
Class1 | Name | Lily | Daniel
Class 2 | Name | Bruce | Uee
Class 2 | Name | Perter | Daniel
Class3 | Name | kate | Kelly
Class3 | Name | Wenddy | Daniel
This comment was minimized by the moderator on the site
how would i change the VBA code so that it will keep more of the columns the same? for example instead of the first two columns staying the same it will be the first 4?
This comment was minimized by the moderator on the site
I am not sure, may be you can try to change x = xRg(1, 1).Column + 2 to x = xRg(1, 1).Column + 4.
This comment was minimized by the moderator on the site
If my cross table's informations come from formulas, those formulas don't always follow when the macro is applied. How could I fix this to make sure that the formulas are kept in the cells?
This comment was minimized by the moderator on the site
Hi, thank u for ur message. For solving your problem, you can apply Kutools' Convert Formula to Text before you running the macro, then after transposing thetable, apply Kutools' Convert Text to Formula. Please Let me know if it works. Thank u.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations