Clearing values for invisible controls

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
688
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.
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
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.)
 

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
688
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
 

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,786
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,100
Messages
5,622,704
Members
415,922
Latest member
gemmatay88

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