Kenja824
Automotive
- Nov 5, 2014
- 950
The below code is supposed to sort the excel table by the "R/L" column header. The problem I am running into is that I have a new file given to me and my code errors out when it gets to this bit of code. I believe it is because this excel file happens to not have a "R/L" column.
How would I change this so that if the column is not found, it will skip this bit of code and continue to the next?
ActiveWorkbook.ActiveSheet.ListObjects("Table1") _
.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.ListObjects("Table1") _
.Sort.SortFields.Add Key:=Range("Table1[[#All],[R/L]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.ListObjects( _
"Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
How would I change this so that if the column is not found, it will skip this bit of code and continue to the next?
ActiveWorkbook.ActiveSheet.ListObjects("Table1") _
.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.ListObjects("Table1") _
.Sort.SortFields.Add Key:=Range("Table1[[#All],[R/L]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.ListObjects( _
"Table1").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With