simplify working code

ndendrinos

Well-known Member
Joined
Jan 17, 2003
Messages
1,694
is there a way to simplify this code?
what it does is sorts within range pushing rows with empty values in A at the bottom and then clears them

Code:
Application.EnableEvents = False
[A17:F35].Select
With Selection

        .Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBottom           
            
[A17:A35].Select
With Selection
.SpecialCells(xlCellTypeBlanks).EntireRow.ClearContents
End With
End With
    
    
Application.EnableEvents = True
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this:

Code:
Range("A17:A35").Sort Key1:=Range("A17"), Order1:=xlAscending

Robert
 
Upvote 0
Code:
Application.EnableEvents = False
With [A17:F35]
    .Sort .Cells(2, 1), 1
    .Resize(, 1).SpecialCells(4).EntireRow.ClearContents
End With
Application.EnableEvents = True
 
Upvote 0
You're welcome. I didn't include code to clear because if the cells are actually null then there's no need to.
 
Upvote 0
thanks Trebor76
maybe I did not explained myself well (happens all the time)

the set up is like this:

......Qty....Shipped....B/O........Product....Cost.....Prod#


A17...2........ 1............1.........apple.......$1........ap1
A18......................................banana....$.95......bn6
A19...3.........2.............1........melons.....$1.75....mel1

after the code I need:

......Qty....Shipped....B/O........Product....Cost.....Prod#


A17...2........ 1............1.........apple.......$1........ap1
A19...3.........2.............1........melons.....$1.75....mel1
 
Upvote 0
Nendrinos,

It was clear from your initial code that you wanted the entire row deleted when the cells in just Column A were empty, regardless of whether the other cells in the row were empty or not. No good assuming they're empty when you didn't specify it.

If you want the modification I suggested to be simplified further, swap around the two lines between the With... and End With. That's because there's fewer operations to be carried out if you delete first and then sort, rather than sorting first and then deleting. (May make a bit of difference with a large spreadsheet.)
 
Upvote 0

Forum statistics

Threads
1,214,576
Messages
6,120,350
Members
448,956
Latest member
Adamsxl

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