Sorting by name blank cell issue

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
707
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello,
I am trying to sort a column after an item is removed.
Is is sorted by name. I remove the name and hit sort and it works fine but I am left with a blank cell. Is there anything I can do for this?
Here is the code.
Code:
Private Sub CommandButton1_Click()
Range("A1:A10").sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
End Sub
Thanks,
Pujo
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You remove the name and you are left with a blank cell.
What do you want instead of the blank cell?
 
Upvote 0
Nothing.
It is just column A with 10 or so items.
From time to time I have to remove an item, thus causing a blank cell in the column between cell A1 and A10. After I hit sort I am left with a blank cell in the range of A1:A10. Just don't want the blank cell, want to get all items one under another with no blank cells. The cell is usually A1, at the very top. If I could just make the blank cell be at the bottom of the list, that way it looks ok and all in order.

Thanks
 
Upvote 0
Sorting usually takes blank cells to the bottom of the range, are they truely blank cells or do they contain ""?
If so, this should work
Code:
Sub Button1_Click()
    With Range("A1:A10")
        .Value = .Value
        .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub
 
Last edited:
Upvote 0
There is nothing in these cells.
Just select the cell hit the space bar to clear out the name.
Hit sort and the blank cell is now at the top of the list¿ not at the bottom.
Go figure¿
 
Upvote 0
Select a cell, hit the space bar, and the cell is not blank, it contains the string " ".

This will deal with that
Code:
Sub Button1_Click()
    Dim oneCell As Range
    With Range("A1:A10")
        For Each oneCell In .Cells
            .Value = Application.Trim(.Value)
        Next oneCell
        .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub
 
Upvote 0
So what do in order to clear the cell?
Right click clear contents then sort?
If this is so, how can I automate this to the cell that I have cleared out?
There has to be a simple way? I think?
Thanks for the reply!
 
Upvote 0
See my edited post above.

I added your code and nothing happens. Like the macro doesn't run, but no errors. I just did a copy & paste of the code.

Any ideas?

Wait, I think its me. Give me a sec to see what I did wrong..............
 
Upvote 0
That did it.
Don't really know what I did, but it works correctly now.

Again, thanks for your time.

Pujo
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

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