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
 

Some videos you may like

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

AlphaFrog

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

Watch MrExcel Video

Forum statistics

Threads
1,122,569
Messages
5,596,915
Members
414,110
Latest member
docops

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