centering checkboxes in non-adjacent ranges

pullmyefinger

Board Regular
Joined
Mar 9, 2011
Messages
69
this code was originally given by QuietRiot or someone, I don't remember.

I can't get my additions to work and don't understand how to setup or navigate all the cells in a range(s).

Can someone explain what is missing?? The example given in the forum does not work so something must be missing or I don't understand how to apply it to my sheet or any range(s).

My example ranges are G4:G6,K10:K11 in my practice sheet.




Sub CenterBox()

Dim rng As Range
Dim Box As Object

Worksheets("P").Activate

Set rng = Worksheets("P").Range("G4:G6,K10:K11")


For Each Box In ActiveSheet.CheckBoxes
Set rng = Box.TopLeftCell
Box.Left = rng.Left + (rng.Width - Box.Width) / 2
Box.Top = rng.Top + (rng.Height - Box.Height) / 2
Next
End Sub
 
Alright, in that case, I'll send you my email address via Private Message.
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The reason you're having problems is that some of the CheckBoxes extend beyond their cell and into the cell above it. In any case, after taking a look at your file, I've taken a different approach. Try the following instead...

Code:
Option Explicit

Sub CenterBoxes()

    Dim wks As Worksheet
    Dim rRng As Range
    Dim rCell As Range
    Dim i As Integer
    
    Set wks = Worksheets("this month")
        
    Set rRng = wks.Range("D9:D22,I8:I22,M8:M22")
    
    i = 561
    For Each rCell In rRng
        With wks.CheckBoxes("Check Box " & i)
            .Width = 27   ' ensures that every CheckBox is the same width - change the width if necessary
            .Height = 17  ' ensures that every CheckBox is the same height - change the height if necessary
            .Left = rCell.Left + (rCell.Width - .Width) / 2
            .Top = rCell.Top + (rCell.Height - .Height) / 2
        End With
        i = i + 1
    Next rCell
    
End Sub
 
Upvote 0
i didn't get any email so i thought you just dropped it.

thanks for the help, your solution looks like one of my old fortran programs.

i kinda thought there was a "border problem" (now i feel just like Texas and Arizona).

lolol, it took a Canadian to fix my border problem, so why don't y'all come down here and fix the rest of the states.. Implement your Vote of Confidence too for all office holders.
 
Upvote 0
I spoke too soon. The checkboxes are lined up under each other, but they are not vertically or horizontally centered in the cells.

Thanks for trying, I will learn this and figure it out so I will know how to do it.
 
Upvote 0
Try replacing...

Code:
.Width = 27
.Height = 17

with

Code:
.Width = 17
.Height = 18

Also, make sure that the row height for Rows 8 through 22 are the same.

Does this help?
 
Last edited:
Upvote 0
ok, I will do that, But is there a way to take any row and/or column and set its height and width to a variable??

then, no matter what you do the boxes will be centered horiz and vert and the calcs should not be that hard

lol, did you already do that and I don't know it?
 
Upvote 0
yes, I did try it, and it aligns the checkboxes one under the other just fine, but as for horiz and vert centering that still does not work.
 
Upvote 0
When I run the code, it seems to align the CheckBoxes both vertically and horizontally. If you send me your email address via Private Message I'll send you a sample file.
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top