Excel/Sort with VBA


Using Excel 2007 VBA:
I need to sort my sheet in order to remove blank rows. There is a header. The code I have now is as follows:

   Cells.sort Key1:=Range("d2"), Order1:=xlAscending,    Header:=xlYes, OrderCustom:=1, _
   MatchCase:=False, Orientation:=xlTopToBottom

It works, but it's slow, presumably because it selects the entire sheet (cells). (If I were to do it manually, I'd do crtl end, shift crtl home, then sort)
What's the faster and better way to do this in VBA?


I can restrict the area you are sorting, but I can't guarantee it will speed things up.

Dim r as Range, lastrow as long, lastcol as Long
lastrow = cells(rows.count,"A").End(xlUp).row
lastcol = cells(1, columns.Count).End(xltoLeft).Column

set r = Range("A1", cells(lastrow, lastcol))

r.sort Key1:=Range("d2"), _
      Order1:=xlAscending, _
      Header:=xlYes, _
      OrderCustom:=1, _
      MatchCase:=False, _

This assumes that I can tell the last used row by looking in column A and I can find the last used column by using row 1. Further it assumes your headers are in row 1 starting in A1.

Tom Ogilvy

---------- FOLLOW-UP ----------

Your code works and is substantially faster than what I had. Thank You!
What I don't understand is how it can work when Column A has lots of blank cells, but yet it still finds the lastrow correctly. I thought that
lastrow = cells(rows.count,"A").End(xlUp).row
would be equivalent to manually selecting A1, End/cursor down (in which case it wouldn't find the last used row).
What am I missing?


what you describe would be  cells(1,"A").End(xldown).row

mine is equivalent to going to the bottom of the spread sheet (row 65536 in excel 2003 and earlier, row 1048576 in excel 2007 to present) in column A.   then hitting the end key followed by hitting the up arrow.  

So I am approaching from the bottom and not affected by embedded blank cells.  

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.