+ Reply to Thread
Results 1 to 16 of 16

Case Select not catching right column in Userform's Combobox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Case Select not catching right column in Userform's Combobox

    Good day,
    I have the following code in my Userform:

    Dim arrCat As Variant
    
    Private Sub ComboBox1_Change()
    ActiveCell = ComboBox1
    Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    
        With Sheets("Pre-Solicitation")
            Select Case ActiveCell.Column
                
                Case 6: arrCat = Range("Strategy").Value
                Caption = "Strategy Selection"
                      
                Case 7: arrCat = Range("Type").Value
                Caption = "Type Selection"
                
                Case 8: arrCat = Range("Peer").Value
                Caption = "Peer Selection"
        
            End Select
        End With
    
        With Sheets("Evaluation")
            Select Case ActiveCell.Column
                Case 6: arrCat = Range("Peer").Value
                Caption = "Peer Selection"
    
            End Select
        End With
    
        With Sheets("Report")
            Select Case ActiveCell.Column
                Case 8: arrCat = Range("Peer").Value
                Caption = "Peer Selection"
    
                Case 12: arrCat = Range("Peer").Value
                Caption = "Peer Selection"
            End Select
        End With
    
        ComboBox1.List = arrCat
        
    End Sub
    I have different sheet but some information will be repeated in these sheets. When I bring my Userform on Case 6, it doesn't populate the right case Selection. Why is this code not functioning well?

    I mean I am closing my "With" therefore should not be looking in my Sheets("Evaluation") on case 6 when I trigger Case 6 in Sheets("Pre-Solicitation").
    Last edited by Excelnoub; 05-08-2014 at 12:41 PM.

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Case Select not catching right column in Userform's Combobox

    Hi,

    Maybe this :

    Dim arrCat As Variant
    
    Private Sub ComboBox1_Change()
    ActiveCell = ComboBox1
    Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    
        With Sheets("Pre-Solicitation")
            Select Case ActiveCell.Column
                
                Case 6: arrCat = .Range("Strategy").Value
                Caption = "Strategy Selection"
                      
                Case 7: arrCat = .Range("Type").Value
                Caption = "Type Selection"
                
                Case 8: arrCat = .Range("Peer").Value
                Caption = "Peer Selection"
        
            End Select
        End With
    
        With Sheets("Evaluation")
            Select Case ActiveCell.Column
                Case 6: arrCat = .Range("Peer").Value
                Caption = "Peer Selection"
    
            End Select
        End With
    
        With Sheets("Report")
            Select Case ActiveCell.Column
                Case 8: arrCat = .Range("Peer").Value
                Caption = "Peer Selection"
    
                Case 12: arrCat = .Range("Peer").Value
                Caption = "Peer Selection"
            End Select
        End With
    
        ComboBox1.List = arrCat
        
    End Sub
    When working with different sheets, make sure that you use .Range(xxx) instead of Range(xxx) to "link" the range to the proper sheet. Otherwise, the range refers to the active sheet.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Case Select not catching right column in Userform's Combobox

    Runtime Error '381':
    Could not set the List Property. Invalid Property Array Index
    In which sheet are your named range?

  4. #4
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    Runtime Error '381':
    Could not set the List Property. Invalid Property Array Index.

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Case Select not catching right column in Userform's Combobox

    Maybe an other hint :
    Let's say you are in your sheet "Pre-solicitation", cell A1. This is your ActiveCell.
    Your Select Case doesn't validate if the ActiveCell is in sheet "Pre-sollicitation" or "Evaluation" or "Report". It only checks the column of the ActiveCell.

    You might need to add an other verification such as

    If ActiveCell.Parent.Name = "Evaluation" then
    ....
    End if
    To verify in which sheet is your ActiveCell.

    Am I clear?

  6. #6
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    Sheets("Control")

  7. #7
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Case Select not catching right column in Userform's Combobox

    Ok, then this is not the problem an d you do not need to use .Range("xxxx").
    Best practice, I would use Sheets("Control").Range("xxx").

    What about my other suggestion ?

  8. #8
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    The:

    If ActiveCell.Parent.Name = "Pre-Solicitation" Then
        With Sheets("Pre-Solicitation")
                Select Case ActiveCell.Column
                    
                    Case 6: arrCat = Sheets("Control").Range("Strategy").Value
                    Caption = "Strategy Selection"
                          
                    Case 7: arrCat = Sheets("Control").Range("Type").Value
                    Caption = "Type Selection"
                    
                    Case 8: arrCat = Sheets("Control").Range("Peer").Value
                    Caption = "Peer Selection"
            
                End Select
            End With
    End If
    Works...

  9. #9
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    lol actually both code work Thank you guys I will make sure next time I check these things next time

    Thank you...

  10. #10
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    Testing it right now

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Case Select not catching right column in Userform's Combobox

    What is the code meant to do?

    Is it supposed to populate ComboBox1 based on which sheet is active and which cell is selected in that sheet?

    If it is then perhaps something like this, untested, code would work.
    Private Sub UserForm_Initialize()
    Dim arrCat As Variant
    
        Select Case ActiveSheet.Name
    
            Case "Pre-Solicitation"
                Select Case ActiveCell.Column
    
                    Case 6: arrCat = Range("Strategy").Value
                        Caption = "Strategy Selection"
    
                    Case 7: arrCat = Range("Type").Value
                        Caption = "Type Selection"
    
                    Case 8: arrCat = Range("Peer").Value
                        Caption = "Peer Selection"
                End Select
    
            Case "Evaluation"
                Select Case ActiveCell.Column
                    Case 6: arrCat = Range("Peer").Value
                        Caption = "Peer Selection"
                End Select
    
            Case "Report"
                Select Case ActiveCell.Column
                    Case 8: arrCat = Range("Peer").Value
                        Caption = "Peer Selection"
    
                    Case 12: arrCat = Range("Peer").Value
                        Caption = "Peer Selection"
                End Select
    
        End Select
    
        ComboBox1.List = arrCat
    
    End Sub
    If posting code please use code tags, see here.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Case Select not catching right column in Userform's Combobox

    Your With in that code doesn't do anything.

  13. #13
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    You're right, thank you Norie

  14. #14
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    Actually to make it more easier to add information afterward to my range I have modified my code to the following.

    Dim arrCat As Variant
    Dim LastRow, LastRow1, LastRow2, X As Integer
    
    Private Sub ComboBox1_Change()
    ActiveCell = ComboBox1
    Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    LastRow = Worksheets("Control").Range("E2").End(xlDown).row
    LastRow1 = Worksheets("Control").Range("C2").End(xlDown).row
    LastRow2 = Worksheets("Control").Range("D2").End(xlDown).row
    
        Select Case ActiveSheet.Name
        
            Case "Pre-Solicitation"
                Select Case ActiveCell.Column
                    Case 6: arrCat = Worksheets("Control").Range("C2:C" & LastRow1).Value
                        Caption = "Strategy Selection"
                    Case 7: arrCat = Worksheets("Control").Range("D2:D" & LastRow2).Value
                        Caption = "Type Selection"
                    Case 8: arrCat = Worksheets("Control").Range("E2:E" & LastRow).Value
                        Caption = "Peer Selection"
                End Select
    
            Case "Evaluation"
                Select Case ActiveCell.Column
                    Case 6: arrCat = Worksheets("Control").Range("E2:E" & LastRow).Value
                        Caption = "Peer Selection"
                End Select
    
    
            Case "Report"
                Select Case ActiveCell.Column
                    Case 8: arrCat = Worksheets("Control").Range("E2:E" & LastRow).Value
                        Caption = "Peer Selection"
                    Case 12: arrCat = Worksheets("Control").Range("E2:E" & LastRow).Value
                        Caption = "Peer Selection"
                End Select
    
        End Select
    
        ComboBox1.List = arrCat
    
    End Sub

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Case Select not catching right column in Userform's Combobox

    No need for 3 variables for the last row(s).

    Again untested.
    Private Sub UserForm_Initialize()
    Dim LastRow As Long
    Dim arrCat As Variant
    Dim arrStrategy As Variant
    Dim arrType As Variant
    Dim arrPeer As Variant
    
        LastRow = Worksheets("Control").Range("C2").End(xlDown).Row
        arrStrategy = Worksheets("Control").Range("C2:C" & LastRow).Value
        
        LastRow = Worksheets("Control").Range("D2").End(xlDown).Row
        arrType = Worksheets("Control").Range("D2:D" & LastRow).Value
        
        LastRow = Worksheets("Control").Range("E2").End(xlDown).Row
        arrPeer = Worksheets("Control").Range("E2:E" & LastRow).Value
        
        Select Case ActiveSheet.Name
        
            Case "Pre-Solicitation"
                Select Case ActiveCell.Column
                    Case 6: arrCat = arrStrategy
                        Caption = "Strategy Selection"
                    Case 7: arrCat = arrType
                        Caption = "Type Selection"
                    Case 8: arrCat = arrPeer
                        Caption = "Peer Selection"
                End Select
    
            Case "Evaluation"
                Select Case ActiveCell.Column
                    Case 6: arrCat = arrPeer
                        Caption = "Peer Selection"
                End Select
    
            Case "Report"
                Select Case ActiveCell.Column
                    Case 8, 12: arrCat = arrPeer
                        Caption = "Peer Selection"
                End Select
    
        End Select
    
        ComboBox1.List = arrCat
    
    End Sub

  16. #16
    Forum Contributor
    Join Date
    03-29-2012
    Location
    Canada
    MS-Off Ver
    2007
    Posts
    818

    Re: Case Select not catching right column in Userform's Combobox

    Once again Norie you have made my life less complicated.

    Works Perfect


+ 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. [SOLVED] Using userform for Select Case
    By hcyeap in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 02-14-2014, 09:00 AM
  2. [SOLVED] ComboBox Select Case Question
    By yoko21 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-02-2013, 06:49 AM
  3. [SOLVED] Excel Userform: Populate other controls (i.e. textbox & combobox) based on combobox select
    By MileHigh_PhD in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-22-2013, 04:50 PM
  4. Select Case and UserForm Buttons
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2011, 11:35 PM
  5. Combobox via Select Case
    By AnthonyWB in forum Access Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2011, 05:19 PM

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