使用vba删除不同excel工作簿中excel工作表上的空行
我有两本工作手册。A书和B书 我想从Book_B工作表1中删除Book_A工作表1中的空行 我已经用VBA编写了一个代码来删除空行使用vba删除不同excel工作簿中excel工作表上的空行,vba,excel,excel-2013,Vba,Excel,Excel 2013,我有两本工作手册。A书和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(xlCellTypeLa
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
此代码有效,使我能够删除工作表的空行 假设我使用vba编辑器在Book_A工作表1中有行 我为VBA项目手册插入一个模块并键入编码 并运行宏 将删除Book_A工作表1中的空行 **但是: 此代码不允许我从Book_B工作表1中删除Book_A工作表1中的空行 我想从Book_B工作表1中删除Book_A工作表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
您不需要而且应该避免选择/
活动*
那么还有什么替代方案@克里斯尼尔森