VBA Userform: Checkbox & IF statement

MrRajKumar

Active Member
Joined
Jan 29, 2008
Messages
291
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a checkbox in an userform named as chkList. If it is checked, 3 other controls will be disabled, so the users wouldn't need to fill those fields. If not, I have ensure the users filled those fields. here is I have tried, but doesn't work.

Cotrols:
txtYear - text box
cboMonth: combo box
cboDay: combo box

Code:
If Me.chkList.Value = False And Me.txtYear & "" = "" Then
     Me.lblErrYear.Caption = "Required"
     Me.txtYear.SetFocus
     Exit Sub
End If

With this I got runtime error: 2110 (Can't focus to the control because it is invisible, not enabled...)

it is disabled, because chkList is checked TRUE. I want to bypass this section, if it is checked.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,
not really understand what you want!
but try this
VBA Code:
Private Sub chkList_Click()
If Me.chkList.Value = True Then
txtYear.Enabled = False
cboMonth.Enabled = False
cboDay.Enabled = False
Else
txtYear.Enabled = True
cboMonth.Enabled = True
cboDay.Enabled = True
     Me.lblErrYear.Caption = "Required"
     Me.txtYear.SetFocus
     Exit Sub
End If
End Sub
 
Upvote 0
Thank you for the reply.

Sorry for the confusion. I am not trying to add the code for checkbox. Instead, I am looking for a code to add to a command button.

When I clicked on a checkbox, It successfully disabled the controls (txtYear, cboMonth & cboday). No issue here.
When I click on cmdSave I have to check these three controls. If these are not disabled (because chkList not clicked) then users must enter values in those three controls. if it is disabled, then skip to checking these three fields.

I hope my reply avoid the confusion. Thank you & sorry once again for the confusion.
 
Upvote 0
What should do when click commandbutton1 ?
the tools should be enabled and not clicked chkList ?
 
Upvote 0
Untested:
VBA Code:
If Me.chkList.Value = False Then
     If Me.txtYear = "" Then
        Me.lblErrYear.Caption = "Required"
        Me.txtYear.SetFocus
        Exit Sub
     End If
End If
 
Upvote 0
Solution
What should do when click commandbutton1 ?
the tools should be enabled and not clicked chkList ?

The commad button, just adding control's values to a sheet. if the check box is clicked, then skip those three control's values & take values from other controls, otherwise include those three control's values as well.
 
Upvote 0
Untested:
VBA Code:
If Me.chkList.Value = False Then
     If Me.txtYear = "" Then
        Me.lblErrYear.Caption = "Required"
        Me.txtYear.SetFocus
        Exit Sub
     End If
End If

Looks like this works. Thank you.
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
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