I have some code that works just fine for sorting and filtering a workbook that contains tables.
I have tested it many a time recently and it works as I need it to.
I run the code from within a module in the workbook where the data is located. Let's call this workbook 1.
However, I now need to run the code from within a different workbook, referencing the data workbook. Let's call this workbook 2.
Basically I run the code from within workbook 2 to open workbook 1, sort and filter workbook 1, and do a few other things.
For some reason a single line of code doesn't want to work now and I cannot figure out why.
Here is the basic code from within workbook 2:
The line of code that is giving me a headache is:
I can't figure out why, where this error is.
The only difference between that line in workbook 2 and the same line in workbook 1 is that workbook 1 is:
But that also fails in workbook 2.
Any thoughts or ideas as to why this is failing?
The weirdest thing is that when it fails, I go into debug, it highlights that line, I press F8 and then it continues with out any further issues.
I really am at a loss as to why in one workbook it works, but in the other it doesn't, even thought the code is referencing the same workbook and data set.
Thanks for you help and input!!
-Spydey
I have tested it many a time recently and it works as I need it to.
I run the code from within a module in the workbook where the data is located. Let's call this workbook 1.
However, I now need to run the code from within a different workbook, referencing the data workbook. Let's call this workbook 2.
Basically I run the code from within workbook 2 to open workbook 1, sort and filter workbook 1, and do a few other things.
For some reason a single line of code doesn't want to work now and I cannot figure out why.
Here is the basic code from within workbook 2:
Code:
Set Wkb = Workbooks.Open(filename:=nLink, ReadOnly:=False)
Set dws = Wkb.Worksheets("Summary")
With dws.ListObjects("Summary")
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("Summary[Generate Scorecard]"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range("Summary[Item]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range("Summary[Location]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
.Range.AutoFilter Field:=5, Criteria1:="Yes"
End With
The line of code that is giving me a headache is:
Code:
.Sort.SortFields.Add Key:=Range("Summary[Generate Scorecard]"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
I can't figure out why, where this error is.
The only difference between that line in workbook 2 and the same line in workbook 1 is that workbook 1 is:
Code:
.Sort.SortFields.Add Key:=Range("Summary[[#Header],Generate Scorecard]]"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
But that also fails in workbook 2.
Any thoughts or ideas as to why this is failing?
The weirdest thing is that when it fails, I go into debug, it highlights that line, I press F8 and then it continues with out any further issues.
I really am at a loss as to why in one workbook it works, but in the other it doesn't, even thought the code is referencing the same workbook and data set.
Thanks for you help and input!!
-Spydey