Row insertion and sorting issue

meandean

New Member
Joined
Jul 27, 2007
Messages
10
I have a spreadsheet with data in 4 different sections with a summary section at the top. Data titles are in column A with the data to the right, arranged by month.
I have created macros to sort the data in each section. Occasionally I need to add a new row to each section for a new data set. When I do this, it screws up my sorting macros for the sections below. It will sort the same rows that it sorted before, not taking into account that the data is now one row lower. How do I fix this?
 
If I put blank rows between the data and the titles it seems to select just the data with Ctrl-Shift-*
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
This code should work for your section that includes cell L152:
Code:
Sub Macro1()
Dim Rng As Range
Dim Rng2 As Range
Dim ER As Integer, EC As Integer, SR As Integer, SC As Integer

Set Rng = Range("L152").CurrentRegion
'create variables for EndRow, EndColumn, StartRow, StartColumn
ER = Cells(152, "L").End(xlDown).Row - 1
EC = Range("L152").End(xlToRight).Column
SR = Rng.Row
SC = Rng.Column

Set Rng2 = Range(Cells(SR, SC), Cells(ER, EC))
    Rng2.Sort Key1:=Range("L152"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

'clear memory
Set Rng = Nothing
Set Rng2 = Nothing
End Sub
This code should self-adjust as rows or columns are added.
 
Upvote 0
I tried it in a different section and this is how the recorder got it. Seems to work too.
Code:
 Range("J5").Select
    Selection.CurrentRegion.Select
    ActiveWorkbook.Worksheets("PE").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("PE").Sort.SortFields.Add Key:=Range("A5:A27"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("PE").Sort
        .SetRange Range("A5:EO27")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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