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

วิธีแทรกตัวเลขหรือแถวสำหรับหมายเลขลำดับที่ขาดหายไปใน Excel

สมมติว่าคุณมีรายการหมายเลขลำดับในแผ่นงาน แต่มีตัวเลขที่ขาดหายไปในลำดับและตอนนี้คุณต้องแทรกตัวเลขที่ขาดหายไปหรือแถวว่างเพื่อให้แน่ใจว่าลำดับนั้นสมบูรณ์ (แสดงเป็นภาพหน้าจอต่อไปนี้) คุณจะแก้ปัญหานี้อย่างรวดเร็วใน Excel ได้อย่างไร

doc-insert-missing-number1 -2 doc-insert-missing-number2

แทรกตัวเลขที่ขาดหายไปสำหรับลำดับด้วยคุณสมบัติเรียงลำดับและลบรายการที่ซ้ำกัน

ใส่หมายเลขที่ขาดหายไปสำหรับลำดับด้วยรหัส VBA

แทรกแถวว่างสำหรับลำดับที่ขาดหายไปด้วยรหัส VBA

แทรกตัวเลขที่ขาดหายไปหรือแถวว่างสำหรับลำดับด้วย Kutools for Excel


ลูกศรสีฟ้าฟองขวา แทรกตัวเลขที่ขาดหายไปสำหรับลำดับด้วยคุณสมบัติเรียงลำดับและลบรายการที่ซ้ำกัน

อาจเป็นไปได้ว่าคุณสามารถค้นหาหมายเลขที่หายไปทีละหมายเลขแล้วใส่เข้าไป แต่เป็นการยากสำหรับคุณที่จะระบุตำแหน่งของหมายเลขที่หายไปหากมีตัวเลขเรียงตามลำดับหลายร้อยหมายเลข ใน Excel ฉันสามารถใช้คุณลักษณะเรียงลำดับและลบรายการที่ซ้ำกันเพื่อจัดการกับงานนี้

1. หลังจากสิ้นสุดรายการลำดับแล้วให้กรอกหมายเลขลำดับอื่นจาก 2005023001 ถึง 2005023011 ดูภาพหน้าจอ:

doc-insert-missing-number3

2. จากนั้นเลือกช่วงของตัวเลขสองลำดับแล้วคลิก ข้อมูล > เรียงลำดับ A ถึง Zดูภาพหน้าจอ:

doc-insert-missing-number4

3. และข้อมูลที่เลือกได้รับการจัดเรียงเป็นภาพหน้าจอต่อไปนี้:

doc-insert-missing-number5

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

doc-insert-missing-number6 -2 doc-insert-missing-number7

5. จากนั้นคลิก OKที่ซ้ำกันใน คอลัมน์ก ถูกลบและใส่หมายเลขที่ขาดหายไปในรายการลำดับดูภาพหน้าจอ:

doc-insert-missing-number8


ลูกศรสีฟ้าฟองขวา ใส่หมายเลขที่ขาดหายไปสำหรับลำดับด้วยรหัส VBA

หากคุณรู้สึกว่ามีหลายขั้นตอนด้วยวิธีการข้างต้นที่นี่ยังมีรหัส VBA สามารถช่วยคุณแก้ปัญหานี้ได้ โปรดดำเนินการดังนี้:

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

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

VBA: ใส่ตัวเลขที่ขาดหายไปสำหรับลำดับ

Sub InsertValueBetween()
'Updateby Extendoffice
Dim WorkRng As Range
Dim Rng As Range
Dim outArr As Variant
Dim dic As Variant
Set dic = CreateObject("Scripting.Dictionary")
'On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
num1 = WorkRng.Range("A1").Value
num2 = WorkRng.Range("A" & WorkRng.Rows.Count).Value
interval = num2 - num1
ReDim outArr(1 To interval + 1, 1 To 2)
For Each Rng In WorkRng
    dic(Rng.Value) = Rng.Offset(0, 1).Value
Next
For i = 0 To interval
    outArr(i + 1, 1) = i + num1
    If dic.Exists(i + num1) Then
        outArr(i + 1, 2) = dic(i + num1)
    Else
        outArr(i + 1, 2) = ""
    End If
Next
With WorkRng.Range("A1").Resize(UBound(outArr, 1), UBound(outArr, 2))
    .Value = outArr
    .Select
End With
End Sub

3. จากนั้นกด F5 คีย์เพื่อเรียกใช้รหัสนี้และกล่องพร้อมต์จะปรากฏขึ้นโปรดเลือกช่วงข้อมูลที่คุณต้องการแทรกตัวเลขที่ขาดหายไป (อย่าเลือกช่วงหัวเรื่อง) ดูภาพหน้าจอ:

doc-insert-missing-number9

4. จากนั้นคลิก OKหมายเลขที่หายไปได้ถูกแทรกลงในรายการลำดับ ดูภาพหน้าจอ:

doc-insert-missing-number1 -2 doc-insert-missing-number2

ลูกศรสีฟ้าฟองขวา แทรกแถวว่างสำหรับลำดับที่ขาดหายไปด้วยรหัส VBA

บางครั้งคุณเพียงแค่ต้องค้นหาตำแหน่งของตัวเลขที่ขาดหายไปและแทรกแถวว่างระหว่างข้อมูลเพื่อให้คุณสามารถป้อนข้อมูลได้ตามที่คุณต้องการ แน่นอนรหัส VBA ต่อไปนี้สามารถช่วยคุณแก้ปัญหานี้ได้

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

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

VBA: แทรกแถวว่างสำหรับลำดับที่ขาดหายไป

Sub InsertNullBetween()
'Updateby Extendoffice
Dim WorkRng As Range
Dim Rng As Range
Dim outArr As Variant
Dim dic As Variant
Set dic = CreateObject("Scripting.Dictionary")
'On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
num1 = WorkRng.Range("A1").Value
num2 = WorkRng.Range("A" & WorkRng.Rows.Count).Value
interval = num2 - num1
ReDim outArr(1 To interval + 1, 1 To 2)
For Each Rng In WorkRng
    dic(Rng.Value) = Rng.Offset(0, 1).Value
Next
For i = 0 To interval
    If dic.Exists(i + num1) Then
        outArr(i + 1, 1) = i + num1
        outArr(i + 1, 2) = dic(i + num1)
    Else
        outArr(i + 1, 1) = ""
        outArr(i + 1, 2) = ""
    End If
Next
With WorkRng.Range("A1").Resize(UBound(outArr, 1), UBound(outArr, 2))
    .Value = outArr
    .Select
End With
End Sub

3. จากนั้นกด F5 คีย์เพื่อเรียกใช้รหัสนี้และกล่องพร้อมต์จะปรากฏขึ้นจากนั้นเลือกช่วงข้อมูลที่คุณต้องการแทรกแถวว่างสำหรับลำดับที่ขาดหายไป (อย่าเลือกช่วงหัวเรื่อง) ดูภาพหน้าจอ:

doc-insert-missing-number9

4. จากนั้นคลิก OKมีการแทรกแถวว่างสำหรับรายการลำดับที่ขาดหายไป ดูภาพหน้าจอ:

doc-insert-missing-number1 -2 doc-insert-missing-number10

ลูกศรสีฟ้าฟองขวา แทรกตัวเลขที่ขาดหายไปหรือแถวว่างสำหรับลำดับด้วย Kutools for Excel

ที่นี่ฉันจะแนะนำเครื่องมือที่ง่ายและสะดวก - Kutools สำหรับ Excelเดียวกันกับที่ ค้นหาหมายเลขลำดับที่ขาดหายไป คุณสามารถแทรกหมายเลขลำดับที่ขาดหายไปหรือแถวว่างระหว่างลำดับข้อมูลที่มีอยู่ได้อย่างรวดเร็ว

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

ถ้าคุณได้ติดตั้ง Kutools สำหรับ Excelโปรดดำเนินการดังนี้:

1. เลือกลำดับข้อมูลที่คุณต้องการแทรกตัวเลขที่ขาดหายไป

2. คลิก Kutools > สิ่งที่ใส่เข้าไป > ค้นหาหมายเลขลำดับที่ขาดหายไปดูภาพหน้าจอ:

3. ใน ค้นหาหมายเลขลำดับที่ขาดหายไป กล่องโต้ตอบตรวจสอบ การใส่หมายเลขลำดับที่ขาดหายไป เพื่อแทรกตัวเลขที่ขาดหายไปหรือ Inserting แถวว่างเมื่อพบหมายเลขลำดับที่ขาดหายไป เพื่อแทรกแถวว่างตามที่คุณต้องการ ดูภาพหน้าจอ:

doc-insert-missing-number10

4. จากนั้นคลิก OK และมีการแทรกหมายเลขลำดับที่ขาดหายไปหรือแถวว่างลงในข้อมูลดูภาพหน้าจอ:

doc-insert-missing-number10 2 doc-insert-missing-number10 2 doc-insert-missing-number10

ดาวน์โหลดและทดลองใช้ Kutools for Excel ฟรีทันที!


ลูกศรสีฟ้าฟองขวา  Demo: แทรกตัวเลขที่ขาดหายไปหรือแถวว่างสำหรับลำดับด้วย Kutools for Excel

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

บทความที่เกี่ยวข้อง:

วิธีระบุลำดับตัวเลขที่ขาดหายไปใน 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 (12)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have used the code for "VBA: insert blank rows for missing sequence" as listed above and works great - but i need it to insert rows across the all columns it only adds rows to the first 2 columns of my selection - not my entire table.
This comment was minimized by the moderator on the site
Hello, Melanie,

To solve your problem, maybe the following code can help you: (Note: A indicates the column contains the missing sequence, please change it to your need.)
Sub InsertBlankRowsForMissingSequence()
    Dim i As Long
    On Error Resume Next
    Application.ScreenUpdating = False
    For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
        If IsNumeric(Cells(i, "A").Value) And IsNumeric(Cells(i - 1, "A").Value) And Cells(i, "A").Value <> "" And Cells(i - 1, "A").Value <> "" Then
            If Cells(i, "A").Value - Cells(i - 1, "A").Value > 1 Then
                Debug.Print Cells(i, "A").Value - Cells(i - 1, "A").Value - 1
                Rows(i).Resize(Cells(i, "A").Value - Cells(i - 1, "A").Value - 1).Insert
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
I am trying to use the VBA for sequential numbers. I have several columns next to the numbers of which numbers too. I.e.
1. HL Meter 34
2. HL Watermeter 40
4. HL CO2meter 24

When I use the code it works for the first 3 columns but it gets mixed up if I include the 4th column since it includes numbers too.
How can I change the code to make sure the numbers in column 4 stay the same?
This comment was minimized by the moderator on the site
Thank you amazing
This comment was minimized by the moderator on the site
What if i want to select 6 columns and then check 1st column for dates and if dates are missing add a row(blank cells) for all 6 columns
This comment was minimized by the moderator on the site
I want to use "Inserting missing sequence Number" feature but it's not supporting for digits more than 12 ? there are many sets in which I want to insert the sequence between (it's a alpha-numeric digit) can you help
This comment was minimized by the moderator on the site
Hi, I want to use "Inserting Missing Sequence Number", but it's not supporting if the no. of digits are more than 12 can you help ?
This comment was minimized by the moderator on the site
What if i want to select 6 columns and then check 1st column for dates and if dates are missing add a row(blank cells) for all 6 columns
This comment was minimized by the moderator on the site
Thank you very much. How do i change the script if the increments is only 0.02 and not 1 This is for the script InsertNullBetween()
This comment was minimized by the moderator on the site
this worked and was very easy to complete the task. Thank you.
This comment was minimized by the moderator on the site
Thanks ! Great script ! How i can modify this script if i say we need to process not only ID column + NAME column, but ID column + NAME column + NEW column ? How i can add new columns in this script?
This comment was minimized by the moderator on the site
The following is the modified macro to include an added column - Another important point is that when prompted to select the range, you should only select the first column - these took me a few hours! hope to save others' time

Sub InsertValueBetween()
'Updateby Extendoffice
Dim WorkRng As Range
Dim Rng As Range
Dim outArr As Variant
Dim dic As Variant
Set dic = CreateObject("Scripting.Dictionary")
Dim dic2 As Variant
Set dic2 = CreateObject("Scripting.Dictionary")

'On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
num1 = WorkRng.Range("A1").Value
num2 = WorkRng.Range("A" & WorkRng.Rows.Count).Value
interval = num2 - num1
ReDim outArr(1 To interval + 1, 1 To 3)
For Each Rng In WorkRng
dic(Rng.Value) = Rng.Offset(0, 1).Value
dic2(Rng.Value) = Rng.Offset(0, 2).Value
Next
For i = 0 To interval
outArr(i + 1, 1) = i + num1
If dic.Exists(i + num1) Then
outArr(i + 1, 2) = dic(i + num1)
outArr(i + 1, 3) = dic2(i + num1)
Else
outArr(i + 1, 2) = ""
outArr(i + 1, 3) = ""

End If
Next
With WorkRng.Range("A1").Resize(UBound(outArr, 1), UBound(outArr, 2))
.Value = outArr
.Select
End With
End Sub
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations