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

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,451
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...
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
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
 

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74

ADVERTISEMENT

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
 

CEG

Board Regular
Joined
Jan 3, 2010
Messages
74
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
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,451
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,652
Messages
5,854,960
Members
431,689
Latest member
jacker01

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
Top