+ Reply to Thread
Results 1 to 6 of 6

How to simplify Multiple ComboBox Change Event code

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    How to simplify Multiple ComboBox Change Event code

    Hi All Expert,

    I have one comboBox depend on another 4 comboBoxes. It would be short to create change event

    Private Sub ComboBox27_Change()
    
        If Me.ComboBox27.Value <> Empty Then
             For i = 28 To 31
                With Me.Controls("ComboBox" & i)
                       .Clear
                       .Enabled = False
                End With
            Next i
    
        Else
            For i = 28 To 31
                With Me.Controls("ComboBox" & i)
                       .Enabled = True
                       .Clear
                       .List = wsData.Range("AA2:AA11").Value
                End With
            Next i
        End If
    End Sub


    However, each of 4 ComboBoxes also depend on a single comboBox as described above. Thus, I have to create 4 times change event to ensure it working fine. Is there any method to create only one change event instead of 4 as described below?

    Private Sub ComboBox28_Change()
    Private Sub ComboBox29_Change()
    Private Sub ComboBox30_Change()
    Private Sub ComboBox31_Change()
    
    
     '\\The same code use for all change event
       If Me.ComboBox28.Value <> Empty Or _
          Me.ComboBox29.Value <> Empty Or _
          Me.ComboBox30.Value <> Empty Or _
          Me.ComboBox31.Value <> Empty Then
          
            With Me.ComboBox27
                   .Clear
                   .Enabled = False
            End With
       Else
            With Me.ComboBox27
                   .Enabled = True
                   .Clear
                   .List = wsData.Range("AA2:AA11").Value
            End With
       End If
    Last edited by Faridwahidi; 10-21-2016 at 11:42 AM.
    1. Thank those who have helped you by clicking the Star * below the post.
    2. Please mark your post [SOLVED] if it has been answered satisfactorily.

    Sincerely,
    Farid

  2. #2
    Registered User
    Join Date
    03-23-2011
    Location
    West Bromwich, England
    MS-Off Ver
    Excel 2007 & 2013 in Azure
    Posts
    83

    Re: How to simplify Multiple ComboBox Change Event code

    You can create a combobox class, or if not familiar with classes, create a "sub sAllComboBoxes_Change" with your required code, then each combobox should have it's own change event with 1 line that calls "sAllComboBoxes_Change"

  3. #3
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: How to simplify Multiple ComboBox Change Event code

    Hi tooley,

    I'm really not familiar on the step mentioned. can you show me the sample?

    My ComboBox27 depend on ComboBox28 to 31 and vice versa. It means both set depend to each other.

  4. #4
    Registered User
    Join Date
    03-23-2011
    Location
    West Bromwich, England
    MS-Off Ver
    Excel 2007 & 2013 in Azure
    Posts
    83

    Re: How to simplify Multiple ComboBox Change Event code

    Sure, there's plenty of examples for class events online, one of which simple to follow is http://www.ozgrid.com/forum/showthread.php?t=154659.

    Regarding dependencies, if your comboboxes are all named "ComboBox" plus numbered and in the same sequence they are dependent (eg when combobox1 is populated open combobox2....etc) then your code can be kept generic relatively simple (untested example is for another generic sub in the userform module and is not using the class method):

    Standard ComboBox Event:
    Private Sub ComboBox27_Change()
        Call sAllComboBoxes_Change(Me.Combobox27)
    End Sub
    Untested generic sub in the UserForm module (change to suit etc):
    Private Sub sAllComboBoxes_Change(objComboBox As Control)
        
        Dim iComboBoxNumber As Integer
        Dim sComboBoxName As String
        Dim oControl As Control
        
        sComboBoxName = "ComboBox"
        iComboBoxNumber = Application.Replace(objComboBox.Name, sComboBoxName, "")
        
        Set oControl = Me.Controls(objComboBox.Name)
        
        With oControl
            'do stuff with combobox that called this sub
        End With
            
        If Not oControl = Nothing Then Set oControl = Nothing
        
        Set oControl = Me.Controls(sComboBoxName & iComboBoxNumber + 1)
        
        With oControl
            'do stuff with the next ComboBox
        End With
                
        If Not oControl = Nothing Then Set oControl = Nothing
        
    End Sub
    The above can be slimmed down further, but is hopefully easier to understand?

  5. #5
    Valued Forum Contributor
    Join Date
    03-28-2014
    Location
    Kuala Lumpur, Malaysia
    MS-Off Ver
    Excel 2016
    Posts
    702

    Re: How to simplify Multiple ComboBox Change Event code

    Hi,

    Your sample is awesome, but i am not familiar using class module. would appreciate if you could provide solution based on the sample given
    Last edited by Faridwahidi; 10-22-2016 at 12:10 AM.

  6. #6
    Registered User
    Join Date
    03-23-2011
    Location
    West Bromwich, England
    MS-Off Ver
    Excel 2007 & 2013 in Azure
    Posts
    83

    Re: How to simplify Multiple ComboBox Change Event code

    See attached, in Excel 2010. Hope this helps
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Combobox change event
    By buhwheet in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2015, 10:39 AM
  2. Same WorkSheet Change Event Code Across Multiple Sheets Help
    By jaylove in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-03-2014, 06:01 PM
  3. Fix change event code to add todays date when data is added to multiple cells in column
    By Nitefox in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 3
    Last Post: 12-21-2013, 06:40 PM
  4. VBA Code: Change event, multiple if's for a range
    By dhopman in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-13-2012, 11:27 AM
  5. Change event for comboBox
    By cmpcwil2 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2006, 10:01 AM
  6. Combobox change event only by UI
    By Excelerate-nl in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-02-2006, 11:10 AM
  7. MsgBox in Enter event causes combobox not to run Change event
    By Richard in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2006, 10:55 AM

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