Clearing values for invisible controls

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Is there a way of clearing the value for all controls on a userform where visible = false?

I have a lot of textboxes, comboboxes, etc on my userform which only show up dependant on values elsewhere in the form, however if I input in 1 and then change an input in another textbox which influences thus making it invisible the value still posts to my worksheet when I submit.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Something along these lines:-
Code:
dim ctl as control
 
for each ctl in userform1.controls
  if typename(ctl)="TextBox" or typename(ctl)="ListBox" or typename(ctl)="ComboBox" then
    if ctl.visible=false then ctl.value=""
  endif
next ctl
(Not tested.)
 
Upvote 0
Thank you, that works.

Problem I have now is that I have image controls which call on the 'change' events of my textboxes so when the textbox is cleared the image appears when it should be hidden.

Will work out a way around this, thank you for your help
 
Upvote 0
Disable events before clearing each control:-
Code:
    if ctl.visible=false then
      [COLOR=red]application.enableevents=false[/COLOR]
      ctl.value=""
      [COLOR=red]application.enableevents=true[/COLOR]
    endif
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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