简体   繁体   English

使用vba删除其他excel工作簿中excel工作表上的空白行

[英]delete blank rows on an excel worksheet, in a different excel workbook, using vba

I have 2 work books. 我有2本工作簿。 Book_A and Book_B. Book_A和Book_B。

I want to delete blank rows in Book_A worksheet1, from Book_B worksheet1. 我想从Book_B worksheet1中删除Book_A worksheet1中的空白行。

I have written a code using VBA to delete blank rows. 我已经使用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

This code works and enables me to delete blank rows of a worksheet. 此代码有效,使我能够删除工作表的空白行。

Say i have rows in Book_A, worksheet1,using the vba editor, 假设我使用vba编辑器在Book_A,worksheet1中有行,

i insert a module for VBA project Book_A and type the coding, 我为VBA项目Book_A插入一个模块,然后输入编码,

and run the macro, 然后运行宏

the blank rows in Book_A, worksheet1 get deleted. Book_A,worksheet1中的空白行将被删除。

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

**But: This code will not enable me to delete blank rows in Book_A worksheet1, from Book_B worksheet1. **但是:此代码将使我无法从Book_B worksheet1中删除Book_A worksheet1中的空白行。

I want to delete blank rows in Book_A worksheet1, from Book_B worksheet1. 我想从Book_B worksheet1中删除Book_A worksheet1中的空白行。

How can this be done? 如何才能做到这一点? How do i edit my coding? 我该如何编辑编码? ** **

This is very easy to do.. 这很容易做到..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM