clear textboxes based on selected two ComboBoxes together on userform

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
428
Office Version
  1. 2016
Platform
  1. Windows
Hello
I use theses codes
VBA Code:
Private Sub UserForm_Initialize()

  If ComboBox1.Value = and <> "" ComboBox2.Value <> "" Then
TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
  end if 
End Sub


and

VBA Code:
Private Sub ComboBox1_Change()
 If ComboBox1.Value <> "" And ComboBox2.Value <> "" Then
 TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
 
  End If
 
End Sub

VBA Code:
Private Sub ComboBox2_Change()
 If ComboBox2.Value <> "" And ComboBox1.Value <> "" Then
 TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
 
  End If
 
End Sub
but doesn't work for me
my goal if I select items from two comboboxes should clear textboxes . if I select another item from combobox1 and the combobox2 is already selected item (not empty) then should clear all of textboxes ,if I select another item from combobox2 and the combobox1 is already selected item (not empty) then should clear all of textboxes .
any better way to do that
thanks in advance.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I expect that you need to change all those If statements to something like this:
VBA Code:
If ComboBox1.ListIndex > -1 Or ComboBox2.ListIndex > -1 Then
 
Upvote 0
thanks
the same result as in original code .:cry:
 
Upvote 0
With this code:
VBA Code:
Private Sub UserForm_Initialize()
    TextBox1.Value = "1"
    TextBox2.Value = "2"
    TextBox3.Value = "3"
    ComboBox1.List = Array(1, 2, 3)
    ComboBox2.List = Array(1, 2, 3)
End Sub


Private Sub ComboBox1_Change()
    If ComboBox1.Value <> "" Or ComboBox2.Value <> "" Then
        TextBox1.Value = ""
        TextBox2.Value = ""
        TextBox3.Value = ""

    End If

End Sub

Private Sub ComboBox2_Change()
    If ComboBox2.Value <> "" Or ComboBox1.Value <> "" Then
        TextBox1.Value = ""
        TextBox2.Value = ""
        TextBox3.Value = ""

    End If

End Sub

as soon as I change either combobox1 or 2, the textboxes are cleared.
 
Upvote 0
well
it gives denied permission error in this line
VBA Code:
ComboBox2.List = Array(1, 2, 3)
 
Upvote 0
Odd, works just fine for me. Alternatively, use this to fill the boxes:
VBA Code:
With ComboBox1
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
 
Upvote 0
so should I do like this
VBA Code:
 With ComboBox1
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
With ComboBox2
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
still the error shows, sorry!
 
Upvote 0
I would suggest not using this in your userform Initialize.
Put the script in a Button on your Useform till you get it working then put the script in your Initialize code.

What else is in your Initialize code?
 
Upvote 0
now I understood why the error because in my project will there is items in resource from windows properties
so I can't use LIST properties for combobox I have to fill the items in comboboxes manually with this way. it's not practical totally
This works for me.
so you say from the first time will clear, but what if select another item from combobox1 and the combobox2 is already filled ? does work too?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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