How to auto-center checkbox in cell in Excel?
While inserting check boxes in cells in Excel, you might notice that it is hard to arrange all checkboxes orderly as the left screenshot shown. Actually, you can move all check boxes to cell center to keep them in order neatly. Method in this article can help you.
Auto-center checkbox in cell with VBA code
Auto-center checkbox in cell with VBA code
To automatically center all checkboxes in cells in a current worksheet, please do as follows.
1. In the worksheet, you need to auto-center all checkboxes, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.
2. In the Microsoft Visual Basic for Applications window, please click Insert > Module. Then copy and paste VBA code into the code window.
VBA code: Automatically center all checkboxes in cells
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. Press the F5 key. Then all checkboxes are moved into the center of the cells immediately as below screenshot shown.
Note: This VBA code can be applied to both CheckBox (ActiveX Control) and CheckBox (Form Control).
Tip: If you want to insert multiple check boxes into a selected range in bulk, you can try the Batch Insert Check Boxes utility pf Kutools for Excel. Or batch insert multiple Option Buttons with the Batch Insert Option Buttons utility. Besides, you can delete all check boxes at once with the Batch Delete Check Boxes utility as below screenshots shown. You can go to free download the software with no limitation in 30 days.
Related articles:
- How to create a drop down list with multiple checkboxes in Excel?
- How to change a specified cell value or color when checkbox is checked in Excel?
- How to highlight cell or row with checkbox in Excel?
- How to make checkbox checked based on cell value in Excel?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!