Global range error message when attempting to sort tables with multiple workbooks open

TrainingExcellence

New Member
Joined
Apr 6, 2017
Messages
10
Office Version
  1. 365
Greetings,

I have a subroutine in which I sort a couple tables. I realized today that when I have multiple workbooks open, the following routine only works if the correct workbook is active. How do I write the code so that it doesn't matter which is the active workbook?

You can see I tried a couple different references for the two tables. Neither work.

VBA Code:
    'Table1
    With shtEnrollments.ListObjects("tblEnrollments")
        With .Sort
            With .SortFields
                .Clear
                .Add Key:=Range(shtEnrollments.ListObjects("tblEnrollments") & "[[#All],[LP Name]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            End With
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
            End With
    avEnrollments = .Range.Value
    End With
   
    'Table2
    With shtLearningPlans.ListObjects("LPTable")
        With .Sort
            With .SortFields
                .Clear
                .Add Key:=Range("LPTable[LP Name]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            End With
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
         avLearningPlans = .Range.Value
    End With

Thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,214,845
Messages
6,121,902
Members
449,053
Latest member
Guy Boot

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