VBA CheckBox If True, Display Date / Time

melq64

New Member
Joined
Aug 21, 2017
Messages
49
Hi newbie question please. I'm trying to create vba for checkbox when checkbox = true, display date/time else display blank/null

Thanks in advance:)
 
Thanks. I decided to enter password and add the necessary code as you pointed out but I get the following error: "Run-time error '9' Subscript out of range"

Private Sub OBApprove_Change()
Sheets("Sheet1").Unprotect Password:="test"


If OBApprove.Value = True Then
Sheets("Template").Range("I34").Value = Environ("Username")
Sheets("Template").Range("J34").Value = Now
Else
Sheets("Template").Range("I34").Value = vbNullString
Sheets("Template").Range("J34").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"

End Sub


Private Sub OBApprove_ClicJ()
Sheets("Sheet1").Unprotect Password:="test"


If OBApprove.Value = True Then
Sheets("Template").Range("I34").Value = Environ("Username")
Sheets("Template").Range("J34").Value = Now
Else
Sheets("Template").Range("I34").Value = vbNullString
Sheets("Template").Range("J34").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"


End Sub


Private Sub OBDecline_Change()
Sheets("Sheet1").Unprotect Password:="test"


If OBDecline.Value = True Then
Sheets("Template").Range("I36").Value = Environ("Username")
Sheets("Template").Range("J36").Value = Now
Else
Sheets("Template").Range("I36").Value = vbNullString
Sheets("Template").Range("J36").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"

End Sub


Private Sub OBDecline_ClicJ()
Sheets("Sheet1").Unprotect Password:="test"


If OBDecline.Value = True Then
Sheets("Template").Range("I36").Value = Environ("Username")
Sheets("Template").Range("J36").Value = Now
Else
Sheets("Template").Range("I36").Value = vbNullString
Sheets("Template").Range("J36").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"


End Sub


Private Sub OBDefer_Change()
Sheets("Sheet1").Unprotect Password:="test"


If OBDefer.Value = True Then
Sheets("Template").Range("I35").Value = Environ("Username")
Sheets("Template").Range("J35").Value = Now
Else
Sheets("Template").Range("I35").Value = vbNullString
Sheets("Template").Range("J35").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"


End Sub


Private Sub OBDefer_ClicJ()
Sheets("Sheet1").Unprotect Password:="test"
If OBDefer.Value = True Then
Sheets("Template").Range("I35").Value = Environ("Username")
Sheets("Template").Range("J35").Value = Now
Else
Sheets("Template").Range("I35").Value = vbNullString
Sheets("Template").Range("J35").Value = vbNullString

End If

Sheets("Sheet1").Protect Password:="test"


End Sub
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I found issue and fixed it. Changed "Sheet1" to name of my sheet.

Thanks again for your help, much appreciated:)
 
Upvote 0
I found issue and fixed it. Changed "Sheet1" to name of my sheet.

Thanks again for your help, much appreciated:)
:oops: Sorry, I had forgotten to change the sheet name in my copy/paste... glad you figured it out on your own.
 
Upvote 0
Hi Rick

For info, I'd looked at OptionButtons, but decided that the OP may well want the option to have none of them selected, which I don't think is an option for OptionButtons, ironically!
 
Upvote 0
Hi Sykes, just wanted to say thank you as well for your help initially. I couldn't get the checkbox code to work in 'Module' so I decided to go with the Options buttons.

By the way I have a last question (hopefully) if its possible to clear selection of the option buttons (i.e. no options selected) so that when file is reopened, there won't be any options pre-selected. Hope that made sense.
 
Last edited:
Upvote 0
I'll let Rick finish off the thread, now he's dealing with your request.
BTW, I've since had a play, and found that programmatically, you CAN have all option buttons deselected at the same time - by just setting their values to false.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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