+ Reply to Thread
Results 1 to 2 of 2

Variable Command Statment w/ Select Case

Hybrid View

  1. #1
    mpeplow
    Guest

    Variable Command Statment w/ Select Case

    What I want to do is use a select case statement within a statement, for example.

    I want to make this code

    Sub Sort_by(First_Sort As String, Optional Second_Sort As String)
    
    If Second_Sort = "" Then
    	Select Case First_Sort
    	    Case A
    	        Cells.Select    'Sort by Sku
    	        Selection.Sort Key1:=Range("A" & "2"), Order1:=xlAscending, Header:=xlYes, _
                	OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    	            DataOption1:=xlSortTextAsNumbers
    
    	    Case B
    	        Cells.Select 'Sort by Core/Partner
    	        Selection.Sort Key1:=Range("B"& "2"), Order1:=xlAscending, Header:=xlYes, _
    	            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                	DataOption1:=xlSortTextAsNumbers    
    	End Select
    End If
    
    End Sub
    Work Like the code below, having a embedded select case statement within a statement so to cut down on the amount of code that I need to write.

    Or does anyone know of a simple way to do this?
    Sub Sort_by(First_Sort As String, Optional Second_Sort As String)
    
    If Second_Sort = "" Then
    	Select Case First_Sort
    	    Case "A"
    	        Cells.Select  
    	        Selection.Sort Key1:=Range("A" & "2"), Order1:=xlAscending, _
    		Select Case Second_String
    			Case A
    				 Key2:=Range("A2") _, Order2:=xlAscending, _
    			Case B
    				 Key2:=Range("B2") _, Order2:=xlAscending, _
    		End Select
    		 Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    	             DataOption1:=xlSortTextAsNumbers
    
    	    Case "B""
    	        Cells.Select 'Sort by Core/Partner
    	        Selection.Sort Key1:=Range("B" & "2"), Order1:=xlAscending, Header:=xlYes, _
    	            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                	DataOption1:=xlSortTextAsNumbers    
    	End Select
    End If
    
    End Sub
    Does that make sense?

  2. #2
    Forum Contributor
    Join Date
    03-13-2005
    Posts
    6,195
    Quote Originally Posted by mpeplow
    What I want to do is use a select case statement within a statement, for example.

    I want to make this code

    Sub Sort_by(First_Sort As String, Optional Second_Sort As String)
    
    If Second_Sort = "" Then
    	Select Case First_Sort
    	    Case A
    	        Cells.Select    'Sort by Sku
    	        Selection.Sort Key1:=Range("A" & "2"), Order1:=xlAscending, Header:=xlYes, _
                	OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    	            DataOption1:=xlSortTextAsNumbers
    
    	    Case B
    	        Cells.Select 'Sort by Core/Partner
    	        Selection.Sort Key1:=Range("B"& "2"), Order1:=xlAscending, Header:=xlYes, _
    	            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                	DataOption1:=xlSortTextAsNumbers    
    	End Select
    End If
    
    End Sub
    Work Like the code below, having a embedded select case statement within a statement so to cut down on the amount of code that I need to write.

    Or does anyone know of a simple way to do this?
    Sub Sort_by(First_Sort As String, Optional Second_Sort As String)
    
    If Second_Sort = "" Then
    	Select Case First_Sort
    	    Case "A"
    	        Cells.Select  
    	        Selection.Sort Key1:=Range("A" & "2"), Order1:=xlAscending, _
    		Select Case Second_String
    			Case A
    				 Key2:=Range("A2") _, Order2:=xlAscending, _
    			Case B
    				 Key2:=Range("B2") _, Order2:=xlAscending, _
    		End Select
    		 Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    	             DataOption1:=xlSortTextAsNumbers
    
    	    Case "B""
    	        Cells.Select 'Sort by Core/Partner
    	        Selection.Sort Key1:=Range("B" & "2"), Order1:=xlAscending, Header:=xlYes, _
    	            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                	DataOption1:=xlSortTextAsNumbers    
    	End Select
    End If
    
    End Sub
    Does that make sense?
    Hi,

    if I have your question correctly understood, then
    Sub TestCase()
    Dim a As String, b As String
    a = "b"
    b = "a"
    Select Case a
    Case "a"
        Select Case b
        Case "a"
            MsgBox "a & a"
        Case "b"
            MsgBox "a & b"
        Case Else
            MsgBox "a & other"
        End Select
    Case "b"
        MsgBox "b"
    End Select
    End Sub
    will allow you to set a & b to a or b to test that it works.

    hth
    ---
    Si fractum non sit, noli id reficere.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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