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

วิธีคัดลอกเฉพาะเส้นขอบของช่วงที่เลือกใน Excel

ในขณะที่ใช้ Microsoft Excel คุณสามารถคัดลอกเฉพาะค่าของเซลล์การจัดรูปแบบเซลล์สูตรเซลล์และอื่น ๆ จากช่วงที่มีฟังก์ชัน Paste Special ได้อย่างง่ายดาย แต่คุณเคยพยายามคัดลอกเฉพาะสไตล์เส้นขอบจากช่วงใน Excel หรือไม่? บทความนี้จะแสดงวิธีการคัดลอกเฉพาะขอบของช่วงที่เลือกไปยังช่วงใหม่ใน Excel

คัดลอกเฉพาะเส้นขอบของช่วงที่เลือกด้วยรหัส VBA


คัดลอกเฉพาะเส้นขอบของช่วงที่เลือกด้วยรหัส VBA

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

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

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคลิก สิ่งที่ใส่เข้าไป > โมดูลจากนั้นคัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่างรหัส

รหัส VBA: คัดลอกเฉพาะเส้นขอบของช่วงที่เลือกใน Excel

Sub CopyBorders()
'Updated by Extendoffice 20211203
    Dim xRg, yRg As Range
    On Error Resume Next
    
    Set xRg = Application.InputBox("Select Range with Borders to Copy...", "Kutools For Excel", , , , , , 8)
    Set yRg = Application.InputBox("Select Cell to Apply Borders to range..", "Kutools For Excel", , , , , , 8)
    
    
    With yRg.Borders(xlEdgeLeft)
        .LineStyle = xRg.Borders(xlEdgeLeft).LineStyle
        .ColorIndex = xRg.Borders(xlEdgeLeft).ColorIndex
        .TintAndShade = xRg.Borders(xlEdgeLeft).TintAndShade
        .Weight = xRg.Borders(xlEdgeLeft).Weight

    End With
    With yRg.Borders(xlEdgeTop)
        .LineStyle = xRg.Borders(xlEdgeTop).LineStyle
        .ColorIndex = xRg.Borders(xlEdgeTop).ColorIndex
        .TintAndShade = xRg.Borders(xlEdgeTop).TintAndShade
        .Weight = xRg.Borders(xlEdgeTop).Weight
    End With
    With yRg.Borders(xlEdgeBottom)
        .LineStyle = xRg.Borders(xlEdgeBottom).LineStyle
        .ColorIndex = xRg.Borders(xlEdgeBottom).ColorIndex
        .TintAndShade = xRg.Borders(xlEdgeBottom).TintAndShade
        .Weight = xRg.Borders(xlEdgeBottom).Weight
    End With
    With yRg.Borders(xlEdgeRight)
        .LineStyle = xRg.Borders(xlEdgeRight).LineStyle
        .ColorIndex = xRg.Borders(xlEdgeRight).ColorIndex
        .TintAndShade = xRg.Borders(xlEdgeRight).TintAndShade
        .Weight = xRg.Borders(xlEdgeRight).Weight
    End With

    With yRg.Borders(xlInsideHorizontal)
        .LineStyle = xRg.Borders(xlInsideHorizontal).LineStyle
        .ColorIndex = xRg.Borders(xlInsideHorizontal).ColorIndex
        .TintAndShade = xRg.Borders(xlInsideHorizontal).TintAndShade
        .Weight = xRg.Borders(xlInsideHorizontal).Weight
    End With
    With yRg.Borders(xlInsideVertical)
        .LineStyle = xRg.Borders(xlInsideVertical).LineStyle
        .ColorIndex = xRg.Borders(xlInsideVertical).ColorIndex
        .TintAndShade = xRg.Borders(xlInsideVertical).TintAndShade
        .Weight = xRg.Borders(xlInsideVertical).Weight
    End With
End Sub

3 กด F5 กุญแจสำคัญในการเรียกใช้รหัส ในครั้งแรกที่โผล่ขึ้นมา Kutools สำหรับ Excel โปรดเลือกช่วงที่มีเส้นขอบที่คุณต้องการคัดลอกจากนั้นคลิกที่ไฟล์ OK ปุ่ม

4. ในวินาทีที่ Kutools สำหรับ Excel โปรดเลือกเซลล์ที่จะใช้เส้นขอบที่คัดลอกแล้วคลิก OK ปุ่ม. ดูภาพหน้าจอ:

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


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

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

🤖 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 (6)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hola me gusto mucho al macro para copiar los Bordes, pero al copiar los bordes me borra el formato que tienen las celtas es decir borra colores y otros formatos que ya tiene la celda. me puedes ayudar con una macro que me permita copiar los bordes de otra celta a otra sin borrar el formato que tiene la celda de destino?
This comment was minimized by the moderator on the site
Dobrý den,

Děkuji za pomoc s kopírováním pouze ohraničení.
Šlo by
Děkuji.
Jirka
This comment was minimized by the moderator on the site
The code also copied my numeric formats and overwrote those in the target cells. Perhaps I did something wrong?
This comment was minimized by the moderator on the site
Hi David,
In my case, the code only copy the borders into the target cells. Can you tell me your Excel version? Thank you for your comment.
This comment was minimized by the moderator on the site
Hello crystal,
I know this is over a year later, but due to a rehosting of my e-mail system, your question rose to the top of my inbox.
My Excel version is 2016 16.0.4993.1001 as reported by Account>About
This comment was minimized by the moderator on the site
Hi David,
Glad to receive your reply. I have tested the code in my Excel 2016, but it still copy the border only. Do you mind sending a copy of your data to ? Sorry for the inconvenience.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations