martinpaiva
New Member
Hi All,
I have the follwing code to sort two tables. This code works when its executed in the sheet where the tables are stored. But I want to move this code to another sheet and execute it when that sheet is selected.
For some reason i get the folliwing error: "Application-defined or object-defined error"
'
Sheets("Source Data").Select
ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD"). _
Sort.SortFields.Add Key:=Range( _
"EMPLEADOSSWISSGUARD[[#All],[EMPLEADOS SWISSGUARD]]"), SortOn:=xlSortOnValues _
, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD" _
).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort.SortFields. _
Add Key:=Range("Table1[[#All],[EMPLEADOS CLEANWARD]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'
I have the follwing code to sort two tables. This code works when its executed in the sheet where the tables are stored. But I want to move this code to another sheet and execute it when that sheet is selected.
For some reason i get the folliwing error: "Application-defined or object-defined error"
'
Sheets("Source Data").Select
ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD"). _
Sort.SortFields.Add Key:=Range( _
"EMPLEADOSSWISSGUARD[[#All],[EMPLEADOS SWISSGUARD]]"), SortOn:=xlSortOnValues _
, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Source Data").ListObjects("EMPLEADOSSWISSGUARD" _
).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort.SortFields. _
Add Key:=Range("Table1[[#All],[EMPLEADOS CLEANWARD]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Source Data").ListObjects("Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'