我有2本工作簿。 Book_A和Book_B。
我想从Book_B工作表1中删除Book_A工作表1中的空白行。
我使用VBA编写了一个代码来删除空行。
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
此代码有效,可以删除工作表的空白行。
假设我在Book_A,worksheet1中有行,使用vba编辑器,
我为VBA项目Book_A插入一个模块并输入编码,
并运行宏,
Book_A中的空白行,工作表1被删除。
............................................... .............................
**但: 此代码不允许我从Book_B工作表1中删除Book_A工作表1中的空白行。
我想从Book_B工作表1中删除Book_A工作表1中的空白行。
如何做到这一点?我如何编辑我的编码? **
答案 0 :(得分: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