Macro recorded to sort colm,n A-Z so now need to condense it

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
I have recorded amacro to sort column E from A-Z
Row 1 are headers & value are row 2 then down the page.

The sort works fine BUT the recorded code is way to long so need to condense it.

Also noticed the range was still selected once sorted.
At the end of the recorded code i thought by adding to select cell A1 would work but i then get an error.

Please advise thanks

Rich (BB code):
Private Sub CommandButton3_Click()
    Dim varNewValue As Variant
    
    varNewValue = InputBox("Enter the desired value for Col. E:")
    
    If Len(varNewValue) > 0 Then
        ThisWorkbook.Sheets("DATABASE INFO").Range("E" & Rows.Count).End(xlUp).Offset(1, 0).Value = varNewValue
    End If
        ActiveWorkbook.Worksheets("DATABASE INFO").ListObjects("Table5").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("DATABASE INFO").ListObjects("Table5").Sort. _
        SortFields.Add Key:=Range("Table5[[#All],[CLONING MODEL]]"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("DATABASE INFO").ListObjects("Table5").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With ThisWorkbook.Worksheets("DATABASE INFO")
        .Range("A1").Select
        End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
The length of that code is actually fairly short. I see little that can be done other than using a With block for repeated reference
ActiveWorkbook.Worksheets("DATABASE INFO").ListObjects("Table5")
That would remove 3 lines and add 2 (the With and End With lines) so no big improvement there in terms of shortening code. It might make it a bit easier to read and grasp that all of that applies to one thing, but if the goal is to shorten the code, it won't do much for that.
 
Upvote 0
OK had to look at it again but now confused myself so please advise how i need to write this.

I wish that when the worksheet DATABASE INFO is opened COLUMN A & COLUMN E are sorted A-Z
As mentioned ROW 1 are HEADERS.
The data to sort is then ROW 2 & down the page.

Thanks
 
Upvote 0
I got this to do what i require BUT i dont want it with a spcific but with the code to find last row.

Rich (BB code):
Private Sub Workbook_Open()
With Worksheets("DATABASE INFO")
.Activate
Range("A1:A18").Sort Key1:=Range("A1"), Header:=xlYes
Range("B1:B5").Sort Key1:=Range("B1"), Header:=xlYes
Range("C1:C10").Sort Key1:=Range("C1"), Header:=xlYes
Range("D1:D35").Sort Key1:=Range("D1"), Header:=xlYes
Range("E1:E66").Sort Key1:=Range("E1"), Header:=xlYes
Range("F1:F11").Sort Key1:=Range("F1"), Header:=xlYes
End With

Application.WindowState = xlMaximized

End Sub
 
Upvote 0
Then I guess you will have to do this for every column
intColA = Sheets("DATABASE INFO").Range("A" & Rows.Count).End(xlUp).Rows
intColB = Sheets("DATABASE INFO").Range("B" & Rows.Count).End(xlUp).Rows
and so on. Then replace the range reference with Range("A1:A" & intColA)
That assumes there is no data in the column below the row you've show. In other words, there can't be anything below row 18 in column A, or below row 5 in B etc.
 
Upvote 0

Forum statistics

Threads
1,215,195
Messages
6,123,572
Members
449,108
Latest member
rache47

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