Repeating Code x Number of Times

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code which sorts a dynamic range of data.

Code:
Sub sort()

    With Sheets("RPL") '(arr(g))

        Dim lRowst As Long
        Dim lRowed As Long
        Dim vg As String
    
           
        llastrow = .Range("R" & Rows.Count).End(xlUp).Row
        Set rdata = .Range("R13:R" & llastrow)
    
        vg = "DT"
    
        cntdr = Application.CountIf(rdata, vg)
    
        If cntdr > 0 Then 'there are DRs
            
            On Error Resume Next
            lRowst = Application.Match(vg, rdata, 0)
            On Error GoTo 0
            
            lRowst = lRowst + 12
            lRowed = lRowst + cntdr - 1
               
            Set oRangeSort = .Range("A" & lRowst & ":R" & lRowed)
            oRangeSort.sort key1:=Range("B" & lRowst), order1:=xlAscending, key2:=Range("Q" & lRowst), order2:=xlDescending, Header:=xlNo, _
                    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
        Else
            MsgBox "No"
        End If
    End With

End Sub

It is written to only work with one criteria (vg) ... DT. I need to repeat this code another 5 different criteria (vg = DR, DTR, FT, FR).
How can I do this without having to repeat the code 5 times. I simply need to change the variable vg to do this.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You could feed the 5 different values into an array and loop on the array index - not sure if there is a more elegant solution, however.
 
Upvote 0
Hi s hal ... I forgot about the use of arrays. I will try that. Thanks for sparking some creativity!
As far as elegance, not a requirement ... none of the rest of my application is.
 
Upvote 0
One way to do this would be to wrap the code you want to repeat in a FOR loop, something like this:

Code:
Sub sort()
    With Sheets("RPL") '(arr(g))
        Dim lRowst As Long
        Dim lRowed As Long
        Dim vg As String
        [COLOR=#ff0000][B]Dim intCounter As Integer
[/B][/COLOR]          
        [COLOR=#ff0000][B]For intCounter = 1 To 5
[/B][/COLOR]           llastrow = .Range("R" & Rows.Count).End(xlUp).Row
            Set rdata = .Range("R13:R" & llastrow)
            
[COLOR=#ff0000][B]           Select Case intCounter
                Case 1
                    vg = "DT"
                Case 2
                    vg = "DR"
                Case 1
                    vg = "DTR"
                Case 2
                    vg = "FT"
                Case 1
                    vg = "FR"
            End Select
[/B][/COLOR]           
            cntdr = Application.CountIf(rdata, vg)
        
            If cntdr > 0 Then 'there are DRs
                
                On Error Resume Next
                lRowst = Application.Match(vg, rdata, 0)
                On Error GoTo 0
                
                lRowst = lRowst + 12
                lRowed = lRowst + cntdr - 1
                   
                Set oRangeSort = .Range("A" & lRowst & ":R" & lRowed)
                oRangeSort.sort key1:=Range("B" & lRowst), order1:=xlAscending, key2:=Range("Q" & lRowst), order2:=xlDescending, Header:=xlNo, _
                        OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
                        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
            Else
                MsgBox "No"
            End If
        [COLOR=#ff0000][B]Next intCounter[/B][/COLOR]
    End With
End Sub
 
Upvote 0
I like the Select Case solution! :) MUCH more elegant than what I was thinking - I knew there had to be a more elegant solution!
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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