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

วิธีทำเครื่องหมายในช่องตรงกลางอัตโนมัติในเซลล์ใน Excel

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

ช่องทำเครื่องหมายศูนย์อัตโนมัติในเซลล์ด้วยรหัส VBA


ช่องทำเครื่องหมายศูนย์อัตโนมัติในเซลล์ด้วยรหัส VBA

หากต้องการจัดให้ช่องทำเครื่องหมายทั้งหมดอยู่กึ่งกลางเซลล์ในแผ่นงานปัจจุบันโดยอัตโนมัติโปรดทำดังนี้

1. ในแผ่นงานคุณต้องวางช่องทำเครื่องหมายทั้งหมดไว้ตรงกลางโดยอัตโนมัติกดปุ่ม อื่น ๆ + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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

รหัส VBA: จัดกึ่งกลางช่องทำเครื่องหมายทั้งหมดในเซลล์โดยอัตโนมัติ

Sub CenterCheckbox ()
    Dim xRg As Range
    Dim chkBox As OLEObject
    Dim chkFBox As CheckBox
    On Error Resume Next
    Application.ScreenUpdating = False
    For Each chkBox In ActiveSheet.OLEObjects
        If TypeName(chkBox.Object) = "CheckBox" Then
            Set xRg = chkBox.TopLeftCell
            chkBox.Width = xRg.Width * 2 / 3
            chkBox.Height = xRg.Height
            chkBox.Left = xRg.Left + (xRg.Width - chkBox.Width) / 2
            chkBox.Top = xRg.Top + (xRg.Height - chkBox.Height) / 2
        End If
    Next
    For Each chkFBox In ActiveSheet.CheckBoxes
        Set xRg = chkFBox.TopLeftCell
        chkFBox.Width = xRg.Width * 2 / 3
        chkFBox.Height = xRg.Height
        chkFBox.Left = xRg.Left + (xRg.Width - chkFBox.Width) / 2
        chkFBox.Top = xRg.Top + (xRg.Height - chkFBox.Height) / 2
    Next
    Application.ScreenUpdating = True
End Sub

3 กด F5 สำคัญ. จากนั้นช่องทำเครื่องหมายทั้งหมดจะถูกย้ายไปที่กึ่งกลางของเซลล์ทันทีตามภาพด้านล่างที่แสดง

หมายเหตุ: รหัส VBA นี้สามารถใช้ได้กับทั้ง CheckBox (ActiveX Control) และ CheckBox (Form Control)

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


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

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

🤖 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 (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
The VBA does not work at all for me. When I press F5, it appears that the module runs, but the checkboxes did not move.
This comment was minimized by the moderator on the site
After some more experimenting, I figured out that the VBA does work for manually inserted checkboxes, but if I use Kutools batch insert, it does not. How do I center all the checkboxes inserted with Kutools?
This comment was minimized by the moderator on the site
Hi quadma,
The code also works for the checkboxes inserted by Kutools. Which Excel version are you using?
This comment was minimized by the moderator on the site
I'm using office 360. I think the issue that I'm having is that when I use the Kutools batch insert, the size of the the checkbox object is as wide as the column that it's inserted into (i.e. the checkbox object width is greater than it's height), with the visible checkbox itself on being left justified within the object. If I select all the checkbox objects and then resize them so that the height and length are equal, and then run the VBA, it does then center the checkboxes within the column.





This seems like an unnecessary step, given that checkboxes are square, why is Kultools not making the checkbox objects square?
This comment was minimized by the moderator on the site
Hi quadma,
The check boxes inserted by Kutools are the same as the Check Box (Form Control) which inserted by Excel.
I don't really understand you said "making the checkbox object square". Normally a check box include the box field and the value field. Kutools keeps the check boxes' value empty if the selected cells are blank. And if there are values in selected cells, the cell value will be taken as the check box value.
This comment was minimized by the moderator on the site
I've uploaded a picture to show what I mean. Hopefully that helps.
This comment was minimized by the moderator on the site
Hi quadma, I got your point. We will think about it and thank you for your patience.
This comment was minimized by the moderator on the site
This worked great for me except it doesn't align it with the text in the next cell which is bottom aligned. Is there a way to align the checkboxes for bottom aligned in order to get them to align with the text in the next cell? Thanks!
This comment was minimized by the moderator on the site
You VDA script is partly working. Because when i applied it, linked cell next to Checkbox get changed and got linked with a cell under it.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations