Wanting to sort column using VBA

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
So I have a command button that when clicked, I would like to sort a cell range in a worksheet.

For some reason, my cells are not sorting using my code below. Any suggestions?

Code:
Sheets("Formula Data").Range("I2").Value = rowNum1
Sheets("Formula Data").Range("I3").Value = rowNum2
Sheets("Formula Data").Range("I4").Value = rowNum3
Sheets("Formula Data").Range("I5").Value = rowNum4
Sheets("Formula Data").Range("I6").Value = rowNum5


Application.EnableEvents = False


For u = 1 To 1


    On Error Resume Next


    Sheets("Formula Data").Range("I1:I6").Sort Key1:=Range("I1"), Order1:=xlAscending, Header:=xlYes
    
Next u


Application.EnableEvents = True
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
What are the values of rowNum1 - rowNum5?

What is the purpose of the loop (since going from "1 to 1" is just doing it once, no need for a loop)?

Try temporarily commenting out the "On Error Resume Next" line (that way if it is encountering any errors, you will see exactly what they are).
 
Upvote 0
Code:
    Sheets("Formula Data").Range("I1:I6").Sort Key1:=Range("I1"), Order1:=xlAscending, Header:=xlYes


You can try this

Code:
    Range("I2").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Formula Data").AutoFilter.sort.SortFields.Clear


    With ActiveWorkbook.Worksheets("Formula Data").AutoFilter.sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("I1:A6"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Apply
    End With
 
Last edited:
Upvote 0
Thanks for the help guys. I took away the On error resume next bit and got an error message that helped me figure out my issue. It was not sorting because key1:=Range was looking for I1 in the wrong worksheet. A lot harder if you don't have the error message. I should have posted! Thanks for the help anyways!

Below is my code change.

Code:
Sheets("Inventory").Range("I1:I6").Sort Key1:=Sheets("Inventory").Range("I1"), Order1:=xlAscending, Header:=xlYes
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

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