Code to delete Blank Rows

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,605
Office Version
  1. 2021
Platform
  1. Windows
I have the following code to delete blank rows on sheets "Output Accounts" and "Input Accounts" as well as to insert headings in A1 & B2 on these sheets

When Running the code it is deleting all the rows on sheet "Output accounts" and not doing anything on sheet "Input accounts"

After running the Macro the sheet output accounts looks like this


Excel 2012
AB
1Amount
2Account Number
3
4
5
6
7
Output Accounts


See my code below

It would be appreciated if someone could assist me

Code:
 Sub Remove_Blanks()
Dim Lr As Long
Lr = Cells(Rows.Count, "A").End(xlUp).Row


With Sheets("Output Accounts")
   With Range("A1:B" & Lr)
    .SpecialCells(xlCellTypeBlanks).Select
    .EntireRow.Delete
    With Range("A1")
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    .FormulaR1C1 = "Account Number"
    With Range("B1")
    .FormulaR1C1 = "Amount"
    With Range("A:A")
    .EntireColumn.AutoFit
     End With
       End With
         End With
           End With
             End With
    With Sheets("Input Accounts")
    
    
    With Range("A1:B" & Lr)
    .SpecialCells(xlCellTypeBlanks).Select
    .EntireRow.Delete
    With Range("A1")
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    .FormulaR1C1 = "Account Number"
    With Range("B1")
    .FormulaR1C1 = "Amount"
    With Range("A:A")
    .EntireColumn.AutoFit
     End With
       End With
         End With
           End With
             End With
   With Sheets("Extracted Data")
   With Range("A1").Activate
     End With
       End With
       
End Sub [\code]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
In a With ... End With construct you need to precede calls to the object's properties/methods with a dot qualifier. For example:

Rich (BB code):
With .Range("A1:B" & Lr)

If you don't the ActiveSheet will be used.
 
Upvote 0
Hi Peter

Thanks for the help.

When running the code on sheet "Extracted Data" all the rows are being deleted


See Sample Data Below


Excel 2012
AB
1122215-1903.5
2125915-266.49
3
4197415502.64
512591570.38
6
7174040701.01
81259150.19
9
101259150.19
1112591521.39
12
1312591521.39
1412591598.14
15
16153810-209.07
17125925-29.27
18
19133810-1911.1
20125925-267.56
Extracted Data


See my amended code below

It would be appreciated if you would correct

Code:
 Sub Remove_Blanks()
Dim Lr As Long
Lr = Cells(Rows.Count, "A").End(xlUp).Row
With Sheets("Extracted Data")
   With .Range("A1:B" & Lr)
    .SpecialCells(xlCellTypeBlanks).Select
    .EntireRow.Delete
   End With
     End With
With Sheets("Output Accounts")
  
    With .Range("A1")
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    .FormulaR1C1 = "Account Number"
    With .Range("B1")
    .FormulaR1C1 = "Amount"
    With .Range("A:A")
    .EntireColumn.AutoFit
    
       End With
         End With
           End With
             End With
    With Sheets("Input Accounts")
    
    
    
    With .Range("A1")
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    .FormulaR1C1 = "Account Number"
    With .Range("B1")
    .FormulaR1C1 = "Amount"
    With Range("A:A")
    .EntireColumn.AutoFit
    
       End With
         End With
           End With
             End With
   With Sheets("Extracted Data")
   With Range("A1").Activate
     End With
       End With
       
End Sub
 
Upvote 0
This:

Code:
    .SpecialCells(xlCellTypeBlanks).Select
    .EntireRow.Delete

should be one line:

Code:
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0
howard,

How about something like this?


Sample raw data in worksheet Extracted Data:


Excel 2007
AB
1122215-1903.5
2125915-266.49
3
4197415502.64
512591570.38
6
7174040701.01
81259150.19
9
101259150.19
1112591521.39
12
1312591521.39
1412591598.14
15
16153810-209.07
17125925-29.27
18
19133810-1911.1
20125925-267.56
21
Extracted Data


After the updated macro:


Excel 2007
AB
1Account NumberAmount
2122215-1903.5
3125915-266.49
4197415502.64
512591570.38
6174040701.01
71259150.19
81259150.19
912591521.39
1012591521.39
1112591598.14
12153810-209.07
13125925-29.27
14133810-1911.1
15125925-267.56
16
17
18
19
20
21
Extracted Data


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub Remove_Blanks_V2()
' hiker95, 09/07/2014, ME803780
Dim Lr As Long

Application.ScreenUpdating = False

With Sheets("Extracted Data")
  Lr = .Cells(Rows.Count, "A").End(xlUp).Row
  On Error Resume Next
  .Range("A1:B" & Lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  On Error GoTo 0
  .Rows(1).Insert
  .Range("A1").Resize(, 2).Value = Array("Account Number", "Amount")
  .Columns(1).Resize(, 2).AutoFit
  .Activate
  .Range("A1").Select
End With

Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the Remove_Blanks_V2 macro.
 
Upvote 0
howard,

And, with the other two worksheets added to the macro (based on your posted macro code):

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub Remove_Blanks_V3()
' hiker95, 09/07/2014, ME803780
Dim Lr As Long

Application.ScreenUpdating = False

With Sheets("Output Accounts")
  .Rows(1).Insert
  .Range("A1").Resize(, 2).Value = Array("Account Number", "Amount")
  .Columns(1).Resize(, 2).AutoFit
End With
             
With Sheets("Input Accounts")
  .Rows(1).Insert
  .Range("A1").Resize(, 2).Value = Array("Account Number", "Amount")
  .Columns(1).Resize(, 2).AutoFit
End With

With Sheets("Extracted Data")
  Lr = .Cells(Rows.Count, "A").End(xlUp).Row
  On Error Resume Next
  .Range("A1:B" & Lr).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  On Error GoTo 0
  .Rows(1).Insert
  .Range("A1").Resize(, 2).Value = Array("Account Number", "Amount")
  .Columns(1).Resize(, 2).AutoFit
  .Activate
  .Range("A1").Select
End With

Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the Remove_Blanks_V3 macro.
 
Upvote 0
howard,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,226,218
Messages
6,189,693
Members
453,563
Latest member
Aswathimsanil

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