Sorting by name blank cell issue

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
You remove the name and you are left with a blank cell.
What do you want instead of the blank cell?
 
Upvote 0

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
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

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
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

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
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

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,348
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

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
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

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
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

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
684
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
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,191,717
Messages
5,988,258
Members
440,146
Latest member
rgomes8

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