Thread: Shortening code
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Martin Brown Martin Brown is offline
external usenet poster
 
Posts: 230
Default Shortening code

On 20/07/2010 17:10, IanC wrote:
I have the following code which disables CommandButton1 if correct
selections have not been made in all 6 comboboxes:

If Me.ComboBox1.MatchFound = False Then Me.CommandButton1.Enabled = False
If Me.ComboBox2.MatchFound = False Then Me.CommandButton1.Enabled = False
If Me.ComboBox3.MatchFound = False Then Me.CommandButton1.Enabled = False
If Me.ComboBox4.MatchFound = False Then Me.CommandButton1.Enabled = False
If Me.ComboBox5.MatchFound = False Then Me.CommandButton1.Enabled = False
If Me.ComboBox6.MatchFound = False Then Me.CommandButton1.Enabled = False

It works fine but I was wondering if it was possible to loop through each
ComboBox withouthaving to repeat the line. I'm looking for something like
"for each ComboBox in UserForm1 if MatchFound=False then disable
CommandButton1" but I don't know where to begin with it.


You can speed it up a bit along the lines of nesting the tests

Me.CommandButton1.Enabled = False
If Me.ComboBox1.MatchFound Then
If Me.ComboBox2.MatchFound Then
If Me.ComboBox3.MatchFound Then
If Me.ComboBox4.MatchFound Then
If Me.ComboBox5.MatchFound Then
If Me.ComboBox6.MatchFound Then
Me.CommandButton1.Enabled = True
Endif
Endif
Endif
Endif
Endif
Endif

Though it might be better to use data validation in the worksheet to
prevent invalid input form ever getting this far.

Regards,
Martin Brown