VBA: Use Variable with Sort Range

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
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:
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
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...
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You seem to be going a very roundabout way for this. Try something like:

VBA Code:
Sub testSort_v1()

'Get Table
Dim lo as listobject
set lo = ActiveCell.ListObject

'Sort Book Level Ascending
    with lo.Sort
        with .SortFields
            .Clear
            .Add Key:=lo.Listcolumns("Book Level").Range
        end with
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub
 
Upvote 1
Solution
You seem to be going a very roundabout way for this. Try something like:
True that. VBA confuses the hell out of me, so I have to take baby steps on trying to make sense of those recorded macros in hopes to make them more dynamic in the future. Unfortunately, I haven't seen a lot of success outside of this forum. :cool:

Thanks for the VBA code you provided...

I ran it in my sample workbook, but it's throwing the following run-time error:

1678232027134.png

I noticed in the Locals window that the lo variable was showing a value of Nothing...

1678232136368.png
So I thought I could add .Name to get the Table Name, but that just threw up another error:

1678232227197.png
I assume the Table Object is not being identified...?

Thanks,
 
Upvote 0
Did you have a cell within the table selected when you ran the code?
 
Upvote 0
Did you have a cell within the table selected when you ran the code?
An excellent catch, Mark! (y)A total brain fart on my end...

Interestingly enough, just finished studying up on working with Tables using VBA. One thing I picked up was selecting Tables, so I went ahead and added the following at the beginning of the code to select the first table in the active sheet in case I do that again.
VBA Code:
ActiveSheet.ListObjects(1).HeaderRowRange(1).Select
Much better now. Thanks...
 
Upvote 0
You seem to be going a very roundabout way for this. Try something like:
Hi Rory,
Disregard my first reply regarding the run-time error. It was my mistake that Mark was able to catch.
Everything works perfectly!
Thanks again for your help on this.
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,135
Members
453,021
Latest member
Justyna P

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