Is there a way to add this VBA code into a function and only call the function instead of writing out this code every time?
Here is the code -
opt.Activate
If ActiveSheet.AutoFilterMode = True Then
opt.AutoFilter.Sort.SortFields.Clear
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
Else:
Range("A6:CK6").AutoFilter
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
With opt.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The range that I sort on changes so I was wondering if I could declare that as a variable and just fill in the range I need when I call the function.
Any help would be greatly appreciated.
Here is the code -
opt.Activate
If ActiveSheet.AutoFilterMode = True Then
opt.AutoFilter.Sort.SortFields.Clear
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
Else:
Range("A6:CK6").AutoFilter
opt.AutoFilter.Sort.SortFields.Add Key _
:=Range("BQ6"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
With opt.AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
The range that I sort on changes so I was wondering if I could declare that as a variable and just fill in the range I need when I call the function.
Any help would be greatly appreciated.