Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,533
- Office Version
- 365
- 2016
- Platform
- Windows
I have this code which sorts a dynamic range of data.
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.
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.