Pass Userform Checkbox check as value into cell

marimar02

Board Regular
Joined
May 21, 2010
Messages
128
Office Version
  1. 365
Platform
  1. Windows
Hello,

I currently have this formula and need it to pass a value "X" into the Cell if it's checked within the Userform Checkbox. There are 50 checkboxes and I'd like it to pass "X" into the selected one. I currently get "False" inserted into the cell.

Code:
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim x As Long, j As Long


Set ws = Worksheets("Settings")


Unload Menu


    x = 1
    For j = 1 To 442 Step 9
        x = x + 1
        ws.Range("AJ" & x).Value = Menu_Controllables.Controls("Checkbox" & j).Value
    Next


Set ws = Nothing


Unload Me


End Sub

Thanks much...
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Change this...
Code:
ws.Range("AJ" & x).Value = Menu_Controllables.Controls("Checkbox" & j).Value
To this...
Code:
If Menu_Controllables.Controls("Checkbox" & j).Value Then ws.Range("AJ" & x).Value = "X" Else ws.Range("AJ" & x).Value = ""
 
Upvote 0
So, that worked. I was so close. My error was I was adding "xlOn" which didn't work.

here was the example that didn't work:

Code:
[COLOR=#333333]If Menu_Controllables.Controls("Checkbox" & j).Value = xlOn
Then ws.Range("AJ" & x).Value = "X" 
Else 
ws.Range("AJ" & x).Value = ""[/COLOR][COLOR=#574123][/COLOR]

Change this...
Code:
ws.Range("AJ" & x).Value = Menu_Controllables.Controls("Checkbox" & j).Value
To this...
Code:
If Menu_Controllables.Controls("Checkbox" & j).Value Then ws.Range("AJ" & x).Value = "X" Else ws.Range("AJ" & x).Value = ""

Thanks so much.
 
Upvote 0
Regarding the same formula, is there a good way to summarize this to reduce code length?

Code:
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim x As Long, k As Long, l As Long, m As Long, n As Long, o As Long
Dim p As Long, q As Long, r As Long, s As Long
Set ws = Worksheets("Settings")


Unload Menu


    x = 1
    For k = 1 To 442 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & k).Value Then
            ws.Range("AJ" & x).Value = "X"
        Else
            ws.Range("AJ" & x).Value = ""
        End If
    Next
    
    x = 1
    For l = 2 To 443 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & l).Value Then
            ws.Range("AK" & x).Value = "X"
        Else
            ws.Range("AK" & x).Value = ""
        End If
    Next
    
    x = 1
    For m = 3 To 444 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & m).Value Then
            ws.Range("AL" & x).Value = "X"
        Else
            ws.Range("AL" & x).Value = ""
        End If
    Next
    
    x = 1
    For n = 4 To 445 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & n).Value Then
            ws.Range("AM" & x).Value = "X"
        Else
            ws.Range("AM" & x).Value = ""
        End If
    Next


    x = 1
    For o = 5 To 446 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & o).Value Then
            ws.Range("AN" & x).Value = "X"
        Else
            ws.Range("AN" & x).Value = ""
        End If
    Next


    x = 1
    For p = 6 To 447 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & p).Value Then
            ws.Range("AO" & x).Value = "X"
        Else
            ws.Range("AO" & x).Value = ""
        End If
    Next


    x = 1
    For q = 7 To 448 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & q).Value Then
            ws.Range("AP" & x).Value = "X"
        Else
            ws.Range("AP" & x).Value = ""
        End If
    Next


    x = 1
    For r = 8 To 449 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & r).Value Then
            ws.Range("AQ" & x).Value = "X"
        Else
            ws.Range("AQ" & x).Value = ""
        End If
    Next
    
    x = 1
    For s = 9 To 450 Step 9
        x = x + 1
        If Menu_Controllables.Controls("Checkbox" & s).Value Then
            ws.Range("AR" & x).Value = "X"
        Else
            ws.Range("AR" & x).Value = ""
        End If
    Next


Set ws = Nothing


Unload Me


End Sub
 
Last edited:
Upvote 0
Regarding the same formula, is there a good way to summarize this to reduce code length?


Not tested

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] CommandButton1_Click()
    
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color], j [color=darkblue]As[/color] [color=darkblue]Long[/color], x [color=darkblue]As[/color] Long
    
    Unload Menu
    x = 1
    
    [color=darkblue]With[/color] Worksheets("Settings")
        [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] 442 [color=darkblue]Step[/color] 9
            x = x + 1
            [color=darkblue]For[/color] j = 0 [color=darkblue]To[/color] 8
                .Range("AJ" & x).Offset(0, j).Value = IIf(Menu_Controllables.Controls("Checkbox" & i + j).Value, "X", "")
            [color=darkblue]Next[/color] j
        [color=darkblue]Next[/color] i
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    Unload Me
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Worked beautifully. I'd like to know why the "If" is written as "IIf"?

Not tested

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
    
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], j [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], x [COLOR=darkblue]As[/COLOR] Long
    
    Unload Menu
    x = 1
    
    [COLOR=darkblue]With[/COLOR] Worksheets("Settings")
        [COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] 442 [COLOR=darkblue]Step[/COLOR] 9
            x = x + 1
            [COLOR=darkblue]For[/COLOR] j = 0 [COLOR=darkblue]To[/COLOR] 8
                .Range("AJ" & x).Offset(0, j).Value = IIf(Menu_Controllables.Controls("Checkbox" & i + j).Value, "X", "")
            [COLOR=darkblue]Next[/COLOR] j
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    Unload Me
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Thanks so much...
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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