Userform checkbox code

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74
I have a userform with checkboxes on it and the following code. I am thinking there has to be an easier way to write this, as I have about 15 of them I have to write. Anyone...?

Code:
If chk1 = True Then
        Cells(ActiveCell.Row, "DT").Value = "1"
    Else
        Cells(ActiveCell.Row, "DT").Value = "0"
    End If
    If chk2 = True Then
        Cells(ActiveCell.Row, "DU").Value = "1"
    Else
        Cells(ActiveCell.Row, "DU").Value = "0"
    End If
        If chk3 = True Then
        Cells(ActiveCell.Row, "DV").Value = "1"
    Else
        Cells(ActiveCell.Row, "DV").Value = "0"
    End If
    ect...

Thanks
CEG
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
    Dim r As Long
    r = ActiveCell.Row
    Cells(r, "DT").Value = Abs(chk1)
    Cells(r, "DU").Value = Abs(chk2)
    Cells(r, "DV").Value = Abs(chk3)
    'ect...
 
Upvote 0
If the columns move to the right for each checkbox and the checkboxes are all prefixed with chk you could try this.
Code:
Dim I As Long
    For I = 1 To 40
        Range("DT" & ActiveCell.Row).Offset(, I - 1) = Me.Controls("chk" & I).Value * -1
    Next I
 
Upvote 0
Ok Norie...

I was just gonna ask another question, which your answer may have taken care of. I need to reverse it for the Private Sub UserForm_Initialize
 
Upvote 0
This worked for the UserForm_Initialize...

Code:
Dim I As Long
    For I = 1 To 20
    If Cells(r, "DT").Offset(, I - 1) = "1" Then
        Me.Controls("chk" & I) = True
    Else: Me.Controls("chk" & I) = False
    End If
    Next I
 
Upvote 0
Or maybe this...
Code:
Dim I As Long
    For I = 1 To 20
        Me.Controls("chk" & I).Value = Cells(r, 123 + I).Value = 1
    Next I
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

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