TrainingExcellence
New Member
- Joined
- Apr 6, 2017
- Messages
- 10
- Office Version
- 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.
Thanks!
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!