Thursday, December 1, 2011

Excel: Removing Blank Rows

Excel has a powerful filtering capability that lets you view, among other options, the data without blank rows. But how do you remove the blank rows from your data? The following VBA code does the following:
  1. Select a cell in the maximum row
  2. Check if the selection is blank
  3. Delete if blank
  4. Move up 1 cell

      Dim x As Integer
      ' Set numrows = number of rows of data.
      NumRows = 1400 -2
      ' Select max cell
      Range("B1400").Select
      ' Establish "For" loop to loop "numrows" number of times.
      For x = 1 To NumRows
         ' Insert your code here.
         If Selection.Text = "" Then
            Selection.EntireRow.Delete
         End If
         ' Selects cell down 1 row from active cell.
         ActiveCell.Offset(-1, 0).Select

There is probably a smarter way to select the maximum number of rows, but for my data it was fixed.