Delete blank lines in excel sheet in another excel workbook using vba

I have 2 workbooks. Book_A and Book_B.

I want to remove blank lines in the worksheet Book_A1, from the worksheet Book_B1.

I wrote code using VBA to remove blank lines.

Sub RemoveEmptyRows()

' this macro will remove all rows that contain no data

Dim i             As Long
Dim LastRow      As Long

LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Application.ScreenUpdating = False

For i = LastRow To 1 Step -1

  If WorksheetFunction.CountA(ActiveSheet.Rows(i)) = 0 Then
   ActiveSheet.Rows(i).EntireRow.Delete

End If
Next i

Application.ScreenUpdating = True
End Sub

This code works and allows me to delete blank lines from the worksheet.

Say I have lines in Book_A, workheet1 using the vba editor,

i insert the module for the VBA Book_A project and enter the encoding,

and run the macro,

empty lines in Book_A, workheet1 are deleted.

.................................................. ..........................

** A: This code will not allow me to delete empty lines in the worksheet Book_A1, from the worksheet Book_B1.

I want to remove blank lines in the worksheet Book_A1, from the worksheet Book_B1.

How can I do that? How to change your encoding? **

+4
1

.

Sub RemoveEmptyRows()

' this macro will remove all rows that contain no data

Dim file_name as String
Dim sheet_name as String

   file_name = "c:\my_folder\my_wb_file.xlsx"  'Change to whatever file you want
   sheet_name = "Sheet1"   'Change to whatever sheet you want

Dim i             As Long
Dim LastRow      As Long

Dim wb As New Workbook

Set wb = Application.Workbooks.Open(file_name)

wb.Sheets(sheet_name).Activate




LastRow = wb.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Application.ScreenUpdating = False

For i = LastRow To 1 Step -1

  If WorksheetFunction.CountA(wb.ActiveSheet.Rows(i)) = 0 Then
   wb.ActiveSheet.Rows(i).EntireRow.Delete

End If
Next i

Application.ScreenUpdating = True
End Sub
+1

All Articles