Hello,
I'm trying to convert one of my recorded macros to use dynamic values instead of the Table or column header names.
So far, I've got everything working up to this point. This is the one line where I can't figure out how to use variables with:
Here's what I'm currently working with:
I'm using a filter as well - not included here - where I get the Column number to filter by. Perhaps one way would be to use the column number with sort...? Online resources point to using Range, but I never stumbled on anyone using the column number to identify the column to sort.
Any insight would be greatly appreciated.
Thank you,
A sample data visual for reference if needed...
I'm trying to convert one of my recorded macros to use dynamic values instead of the Table or column header names.
So far, I've got everything working up to this point. This is the one line where I can't figure out how to use variables with:
VBA Code:
SortFields.Add Key:=Range("tblBooks6[[#Headers],[#Data],[Book Level]]")
Here's what I'm currently working with:
VBA Code:
'Filter Book Level Range
Sub testSort_v1()
'Get Sheet Name
Dim SheetName As String
SheetName = ActiveSheet.Name
'Get Table Name
Dim TblName As String
TblName = ActiveCell.ListObject.Name
'Get "Book Level" column number
Dim ColNum As Integer
ColNum = ActiveSheet.ListObjects(TblName).ListColumns("Book Level").Index
'Sort Book Level Ascending
ActiveWorkbook.Worksheets(SheetName).ListObjects(TblName).Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets(SheetName).ListObjects(TblName).Sort. _
SortFields.Add Key:=Range("tblBooks6[[#Headers],[#Data],[Book Level]]")
With ActiveWorkbook.Worksheets(SheetName).ListObjects(TblName).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Any insight would be greatly appreciated.
Thank you,
A sample data visual for reference if needed...