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.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,792
Office Version
  1. 365
Platform
  1. Windows
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,792
Office Version
  1. 365
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,661
Messages
5,838,651
Members
430,560
Latest member
DaliaKenawy86

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