How to fix run time error 424 when accessing the value of a CheckBox in Excel VBA? -


okay here problem: in user form, have 2 checkboxes. want able use value of checkbox perform job.


example:

sub main()     userform1.show     if userform1.checkbox1.value=true         msgbox("awesome")     end if end sub 

now problem keeps giving me run time error 424. can me this? appreciated. thank you.


update:

sub main()     userform1.show     if userform1.checkbox1.value=true         worksheets(1).activate         if userform1.checkbox1.value=true             msgbox("awesome")         end if     end if end sub 

okay stops after worksheets(1).activate. appreciated. thank you.

solution: code works me:

sub main()     if userform1.checkbox1.value = true         msgbox "checkbox checked"     end if end sub 

explanation: error appears because did not specify object (in case: form) checkbox1 belongs to. hence added userform1. in if statement. secondly, checkbox1.value boolean property, i.e. value true when checked, not 1.

enter image description here

additional information: please note running if clause after userform1.show (like did in example) never work in case intend select checkboxes after .show command. form shown , if clause run before had time select checkbox. code in answer should go sub, e.g. 1 run when click button in form (do have sort of "ok" or "close" button on it? if yes, double click button in macro editor , add code there). let me know if need more context.

update (as requested in comments): here's have:

sub a()     ' launches form     ' added normal module in     ' vba editor     userform1.show end sub  private sub commandbutton1_click()     ' executed when clicking     ' "ok" button     ' add code, add button     ' form, double click , paste code     if userform1.checkbox1.value = true         worksheets(1).activate         msgbox "awesome"     end if     ' update 2: close form keep     ' checkbox1.value available     userform1.hide end sub 

running a (from "macros" dialogue on "developer" tab) gives me:

enter image description here

selecting checkbox , clicking ok returns this:

enter image description here


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -