Hi everyone, I need some help trying to figure out why I cant get a sort macro to work.
I have the macro working perfectly on one worksheet but im trying to implement the same macro on a different worksheet and I cant get it working at all
This is the VBA I get when I record the macro
Sub BSORT()
'
' BSORT Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Rows("106:124").Select
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"K106:K124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"I106:I124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"H106:H124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"D106:D124"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES-B").Sort
.SetRange Range("A106:CO124")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
This is not how I want the marco to work as this uses the cells that I highlighted but I want to sort a user defined range instead. The VBA for the marco that does work how I would like is this
Sub Sort()
'
' Sort Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(11), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(9), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(8), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES").Sort
.SetRange Selection
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
This is not how I want the marco to work as this uses the cells that I highlighted but I want to sort a user defined range instead. The VBA for the marco that does work how I would like is this
When I try to make the changes in the first VBA to match this second one though I get a runtime error '40036' Application-defined or Object-defined error and when I check the debug, this line is what it seems to not like, highlighted in yellow
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Clear
Whats even more puzzling is I have used the exact same VBA on another seperate worksheet using a different trigger character and it works perfectly as well.
Any ideas??
I have the macro working perfectly on one worksheet but im trying to implement the same macro on a different worksheet and I cant get it working at all
This is the VBA I get when I record the macro
Sub BSORT()
'
' BSORT Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Rows("106:124").Select
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"K106:K124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"I106:I124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"H106:H124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"D106:D124"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES-B").Sort
.SetRange Range("A106:CO124")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
This is not how I want the marco to work as this uses the cells that I highlighted but I want to sort a user defined range instead. The VBA for the marco that does work how I would like is this
Sub Sort()
'
' Sort Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(11), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(9), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(8), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES").Sort
.SetRange Selection
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
VBA Code:
Sub BSORT()
'
' BSORT Macro
'
' Keyboard Shortcut: Ctrl+Shift+B
'
Rows("106:124").Select
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"K106:K124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"I106:I124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"H106:H124"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Add2 Key:=Range( _
"D106:D124"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES-B").Sort
.SetRange Range("A106:CO124")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
VBA Code:
Sub Sort()
'
' Sort Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(11), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(9), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(8), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("RES").Sort.SortFields.Add Key:=Selection.Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("RES").Sort
.SetRange Selection
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
When I try to make the changes in the first VBA to match this second one though I get a runtime error '40036' Application-defined or Object-defined error and when I check the debug, this line is what it seems to not like, highlighted in yellow
ActiveWorkbook.Worksheets("RES-B").Sort.SortFields.Clear
Whats even more puzzling is I have used the exact same VBA on another seperate worksheet using a different trigger character and it works perfectly as well.
Any ideas??
Last edited by a moderator: