簡體   English   中英

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

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

我有2本工作簿。 Book_A和Book_B。

我想從Book_B worksheet1中刪除Book_A worksheet1中的空白行。

我已經使用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

此代碼有效,使我能夠刪除工作表的空白行。

假設我使用vba編輯器在Book_A,worksheet1中有行,

我為VBA項目Book_A插入一個模塊,然后輸入編碼,

然后運行宏

Book_A,worksheet1中的空白行將被刪除。

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

**但是:此代碼將使我無法從Book_B worksheet1中刪除Book_A worksheet1中的空白行。

我想從Book_B worksheet1中刪除Book_A worksheet1中的空白行。

如何才能做到這一點? 我該如何編輯編碼? **

這很容易做到..

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