I've reviewed this forum and 2 others, but have been unable to find answers that fit my scenario. I'm needing to sort a range of rows that changes. The sort will occur on 2 columns. I've recorded the macro, but it continues to specify a set range. I was able to find the snippet included in the code below, but I am getting an error saying that a variable isn't set. The code works fine without the sorting, so I know that's the issue, I just can't nail it down. Any help is appreciated.
Code:
Private Sub UserForm_Initialize()
Dim cGender As Range
Dim cPymtFreq As Range
Dim cEntryType As Range
Dim cStatus As Range
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim ws4 As Worksheet
Dim ws5 As Worksheet
Dim LastRow As Range
Set ws1 = ThisWorkbook.Sheets("Bios")
Set ws2 = ThisWorkbook.Sheets("Stats")
Set ws3 = ThisWorkbook.Sheets("Services")
Set ws4 = ThisWorkbook.Sheets("Payments")
Set ws5 = ThisWorkbook.Sheets("Variables")
LastRow = ws1.Range("G" & Rows.Count).End(xlUp).Row + 1
For Each cGender In ws5.Range("Gender")
With Me.cobo_Gender
.AddItem cGender.Value
End With
Next cGender
For Each cPymtFreq In ws5.Range("PymtFreq")
With Me.cobo_DPFreq
.AddItem cPymtFreq.Value
End With
Next cPymtFreq
For Each cPymtFreq In ws5.Range("PymtFreq")
With Me.cobo_DCFreq
.AddItem cPymtFreq.Value
End With
Next cPymtFreq
For Each cPymtFreq In ws5.Range("PymtFreq")
With Me.cobo_OCFreq
.AddItem cPymtFreq.Value
End With
Next cPymtFreq
For Each cPymtFreq In ws5.Range("PymtFreq")
With Me.cobo_CTIFreq
.AddItem cPymtFreq.Value
End With
Next cPymtFreq
For Each cPymtFreq In ws5.Range("PymtFreq")
With Me.cobo_CTOFreq
.AddItem cPymtFreq.Value
End With
Next cPymtFreq
ws1.Range("A2:M" & LastRow).Select
ActiveWorkbook.Worksheets("Bios").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Bios").Sort.SortFields.Add Key:=Range( _
"G2:G" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Bios").Sort.SortFields.Add Key:=Range( _
"B2:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A2:M" & LastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub