VBA: Sorting data with dynamic heading location.

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I have a list of headers in row 13. I am trying to find the header titled "Revenue" and sort in descending order.

Code:
lCol = WorksheetFunction.Match("Revenue", Rows("13:13"), 0)
Rows("13").Sort key1:=lCol, Header:=xlYes

What am I doing wrong?

Thank you in advance
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe use the Find method of VBA
Code:
Dim fn As Range
Set fn = Rows(13).Find("Revenue", , xlValues)
    If Not fn Is Nothing Then
        fn.EntireColumn.Sort fn, xlAscending
    End If

It was assumed that you wanted to sort the column, not the row.
 
Last edited:
Upvote 0
Substitute the below Macro for what you WERE using...

Code:
Sub Foo()
Range("A13").Select
lCol = WorksheetFunction.Match("Revenue", Rows("13:13"), 0)
With ActiveCell.CurrentRegion
    .Sort Key1:=Cells(13, lCol), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
End Sub
 
Upvote 0
Substitute the below Macro for what you WERE using...

Code:
Sub Foo()
Range("A13").Select
lCol = WorksheetFunction.Match("Revenue", Rows("13:13"), 0)
With ActiveCell.CurrentRegion
    .Sort Key1:=Cells(13, lCol), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
End Sub

This is close!
1) The cells should be sorted starting on row 14. This macro is sorting them and the lowest value is starting on row 9 and the cell that contained "Revenue" on row 13 is now erased with data.
2) The Revenue column is sorting but some of the other columns to the left and right aren't sorting with it.
 
Upvote 0
Obviously you must have non blank data in all rows above Row 13 (meaning all rows between 9 and 13)... Had I known that I would not have used the CurrentRegion property -- which caused the reported problem.
Best work-a-round would be prior to the running of the revised micro below -- SELECT (Or Highlight) the Range from A13 across and down to your last record involved in the Sort.
Then run the Macro -- Save your File BEFORE Doing this in case there ends up being still another unforeseen problem.
Jim


Code:
Sub Foo()
lCol = WorksheetFunction.Match("Revenue", Rows("13:13"), 0)
With Selection
    .Sort Key1:=Cells(13, lCol), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
End Sub
 
Upvote 0
Solution
Obviously you must have non blank data in all rows above Row 13 (meaning all rows between 9 and 13)... Had I known that I would not have used the CurrentRegion property -- which caused the reported problem.
Best work-a-round would be prior to the running of the revised micro below -- SELECT (Or Highlight) the Range from A13 across and down to your last record involved in the Sort.
Then run the Macro -- Save your File BEFORE Doing this in case there ends up being still another unforeseen problem.
Jim


Code:
Sub Foo()
lCol = WorksheetFunction.Match("Revenue", Rows("13:13"), 0)
With Selection
    .Sort Key1:=Cells(13, lCol), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With
End Sub

Worked like a charm. Thank you so much! Sorry I should have clarified that in my original post.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top