Hide multiple textbox/combobox/label if condition is met

MK29

New Member
Joined
Jun 29, 2020
Messages
15
Office Version
  1. 2010
Platform
  1. Windows
Hello all.

ive been stuck on this string on vba for HOURS.

I am trying to write a code that depending on the number inputed by a user will make select combo boxes visible... i.e. users selects 2 therefore 2 combo boxes become visible out of the 7 and the remaining are hidden.

as there is a large amount of combo boxes to hide and unhide i thought it would be easier and mor efficient to make a string but it doesnt seem to be working when i test it..

does anyone know a code that will help?

VBA Code:
VBA Code:
Public Counterparty1, Counterparty2, Counterparty3, Counterparty4, Counterparty5, Counterparty6, Counterparty7 As Long

'and then in the module

If Counterparty1 = True Then
UserForm1.CPLabel1.Visible = True
UserForm1.SHLabel1.Visible = True
UserForm1.CIF1.Visible = True
UserForm1.CPTB1.Visible = True
UserForm1.SHTB1.Visible = True
UserForm1.CIFTB1.Visible = True
End If

If Counterparty2 = True Then
UserForm1.CPLabel2.Visible = True
UserForm1.SHLabel2.Visible = True
UserForm1.CIF2.Visible = True
UserForm1.CPTB2.Visible = True
UserForm1.SHTB2.Visible = True
UserForm1.CIFTB2.Visible = True
End If

If Counterparty3 = True Then
UserForm1.CPLabel3.Visible = True
UserForm1.SHLabel3.Visible = True
UserForm1.CIF3.Visible = True
UserForm1.CPTB3.Visible = True
UserForm1.SHTB3.Visible = True
UserForm1.CIFTB3.Visible = True
End If

If Counterparty4 = True Then
UserForm1.CPLabel4.Visible = True
UserForm1.SHLabel4.Visible = True
UserForm1.CIF4.Visible = True
UserForm1.CPTB4.Visible = True
UserForm1.SHTB4.Visible = True
UserForm1.CIFTB4.Visible = True
End If

If Counterparty5 = True Then
UserForm1.CPLabel5.Visible = True
UserForm1.SHLabel5.Visible = True
UserForm1.CIF5.Visible = True
UserForm1.CPTB5.Visible = True
UserForm1.SHTB5.Visible = True
UserForm1.CIF5.Visible = True
End If

If Counterparty6 = True Then
UserForm1.CPLabel6.Visible = True
UserForm1.SHLabel6.Visible = True
UserForm1.CIF6.Visible = True
UserForm1.CPTB6.Visible = True
UserForm1.SHTB6.Visible = True
UserForm1.CIF6.Visible = True
End If

If Counterparty7 = True Then
UserForm1.CPLabel7.Visible = True
UserForm1.SHLabel7.Visible = True
UserForm1.CIF7.Visible = True
UserForm1.CPTB7.Visible = True
UserForm1.SHTB7.Visible = True
UserForm1.CIF7.Visible = True
End If


If UserForm1.NumberCP.Text = "1" Then
Counterparty1 = True
Counterparty2 = False
Counterparty3 = False
Counterparty4 = False
Counterparty5 = False
Counterparty6 = False
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "2" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = False
Counterparty4 = False
Counterparty5 = False
Counterparty6 = False
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "3" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = True
Counterparty4 = False
Counterparty5 = False
Counterparty6 = False
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "4" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = True
Counterparty4 = True
Counterparty5 = False
Counterparty6 = False
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "5" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = True
Counterparty4 = True
Counterparty5 = True
Counterparty6 = False
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "6" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = True
Counterparty4 = True
Counterparty5 = True
Counterparty6 = True
Counterparty7 = False
End If

If UserForm1.NumberCP.Text = "7" Then
Counterparty1 = True
Counterparty2 = True
Counterparty3 = True
Counterparty4 = True
Counterparty5 = True
Counterparty6 = True
Counterparty7 = True
End If
 
Last edited by a moderator:
hey dave
Probably easier if you publish ALL the code in your userform & explain which control will be "inputted by a user" or even better, place copy of your workbook in a dropbox & provide a link to it here.

Dave

would you be able to amend the coding you sent to what i would write for my code to help me understand
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
hey dave


would you be able to amend the coding you sent to what i would write for my code to help me understand

Place your workbook on in a file sharing site like dropbox - this will make it much easier for either myself or others here to assist you

Dave
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

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