Sort column macro

instanceoftime

Board Regular
Joined
Mar 23, 2011
Messages
83
I'm using the code below to format spreadsheets with various number of rows.
Unfortunately I have noticed my "sort" portion isn't always working correctly?

end result:

1999.99
1699.99
599.99
249.99
449.99
449.99
399.99
199.99
119.99
349.99
349.99
169.99
329.99
299.99
299.99
299.99
299.99
129.99
249.99
49.99
239.99

VBA Code:
Sub invoice()
'
' formating Macro
' control u
'
' Keyboard Shortcut: Ctrl+u
'
    Cells.Select  'left align
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        
    Range("C:C,D:D,H:M").Select 'delete some columns
    Range("H1").Activate
    Selection.delete Shift:=xlToLeft
    
    Columns("C:C").Select 'resize column
    Selection.ColumnWidth = 45
    
[COLOR=rgb(226, 80, 65)]    Range("A2", Range("E" & Rows.Count).End(xlUp)).Sort [A2], xlDescending 'sort by column E[/COLOR]
    
    With Range("A2", Range("A" & Rows.Count).End(xlUp))   'truncate number in A
      .Value = Evaluate(Replace("If(@="""","""",right(@,3))", "@", .Address))
   End With

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,162
Office Version
  1. 365
Platform
  1. Windows
Will you always have data in the last row of col E?
 

EXCEL MAX

Active Member
Joined
Nov 11, 2020
Messages
488
Office Version
  1. 2016
Platform
  1. Windows
Try to change this line...
VBA Code:
Range("A2", Range("A" & Rows.Count).End(xlUp)).Sort [A2:A22], xlDescending 'sort by column
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,162
Office Version
  1. 365
Platform
  1. Windows
Can you answer my question in post#2?
Also in what way isn't your code sorting properly?
 

Forum statistics

Threads
1,140,917
Messages
5,703,168
Members
421,279
Latest member
emzy

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
Top