+ Reply to Thread
Results 1 to 6 of 6

Display/Hide textboxes with combobox and VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    56

    Post Display/Hide textboxes with combobox and VBA

    Hi,

    In the Useform I have a ComboBox with drop down options "1 to 12". When you select a number a NameBox and CompanyBox become visible. The number of NameBox and CompanyBox become visible depend on the number selected int he ComboBox. How can I shorten this code:

    Private Sub ComboBox1_Change()
    
        If ComboBox1 = "" Then
            NameBox1.Visible = False
            CompanyBox1.Visible = False
            NameBox2.Visible = False
            CompanyBox2.Visible = False
            NameBox3.Visible = False
            CompanyBox3.Visible = False
            NameBox4.Visible = False
            CompanyBox4.Visible = False
            NameBox5.Visible = False
            CompanyBox5.Visible = False
            NameBox6.Visible = False
            CompanyBox6.Visible = False
            NameBox7.Visible = False
            CompanyBox7.Visible = False
            NameBox8.Visible = False
            CompanyBox8.Visible = False
            NameBox9.Visible = False
            CompanyBox9.Visible = False
            NameBox10.Visible = False
            CompanyBox10.Visible = False
            NameBox11.Visible = False
            CompanyBox11.Visible = False
            NameBox12.Visible = False
            CompanyBox12.Visible = False
            End If
            
        If ComboBox1 = "1" Then
            NameBox1.Visible = True
            CompanyBox1.Visible = True
            NameBox2.Visible = False
            CompanyBox2.Visible = False
            NameBox3.Visible = False
            CompanyBox3.Visible = False
            NameBox4.Visible = False
            CompanyBox4.Visible = False
            NameBox5.Visible = False
            CompanyBox5.Visible = False
            NameBox6.Visible = False
            CompanyBox6.Visible = False
            NameBox7.Visible = False
            CompanyBox7.Visible = False
            NameBox8.Visible = False
            CompanyBox8.Visible = False
            NameBox9.Visible = False
            CompanyBox9.Visible = False
            NameBox10.Visible = False
            CompanyBox10.Visible = False
            NameBox11.Visible = False
            CompanyBox11.Visible = False
            NameBox12.Visible = False
            CompanyBox12.Visible = False
            End If

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Display/Hide textboxes with combobox and VBA

    Private Sub ComboBox1_Change()
    Dim A As Long
    
    Select Case Val(ComboBox1.ListIndex)
        Case 0
            For A = 1 To 12
                Me.Controls("NameBox" & A).Visible = False
                Me.Controls("CompanyBox" & A).Visible = False
            Next
        
        Case 1 To 12
            
            For A = 1 To 12
                If A And ComboBox1.ListIndex Then
                    Me.Controls("NameBox" & ComboBox1.ListIndex).Visible = True
                    Me.Controls("CompanyBox" & ComboBox1.ListIndex).Visible = True
                Else
                    Me.Controls("NameBox" & A).Visible = False
                    Me.Controls("CompanyBox" & A).Visible = False
                End If
            Next
    End Select
            
    End Sub
            
    End Sub
    Last edited by Tinbendr; 04-22-2013 at 03:00 PM.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    10-24-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Display/Hide textboxes with combobox and VBA

    Hi David,

    Thanks for your reply,

    However that didnt work, various NameBoxs and CompanyBoxs appear as you choose different number from dropbox.

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Display/Hide textboxes with combobox and VBA

    Oops! That should be

    If A = ComboBox1.ListIndex Then
    Last edited by Tinbendr; 04-22-2013 at 03:54 PM.

  5. #5
    Registered User
    Join Date
    10-24-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Display/Hide textboxes with combobox and VBA

    Hi David,

    The correction worked but i would like to show this:

    When 3 is selected i see:

    NameBox CompanyBox
    NameBox CompanyBox
    NameBox CompanyBox

    Right now when i select 3, only third line is displayed

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Display/Hide textboxes with combobox and VBA

    Hi Kaurka,

    Try this:

    Private Sub ComboBox1_Change()
    Dim O As Object, S As String
        
        For Each O In ActiveSheet.Shapes: S = O.Name
        If InStr(1, S, "Name") Or InStr(1, S, "Comp") Then
        If Right(S, 1) = Val(ComboBox1.Value) Then
            O.Visible = True
        Else
            O.Visible = False: End If: End If: Next: End Sub
    Last edited by xladept; 04-22-2013 at 06:46 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1