Excel VBA - Delete empty rows

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

I would like to delete the empty rows my ERP Quotation generates. I'm trying to go through the document (A1:Z50) and for each row where there is no data in the cells (A1-B1...Z1 = empty, A5-B5...Z5 = empty) I want to delete them.

I found this, but can't seem to configure it for me.

On Error Resume Next Worksheet.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete On Error GoTo 0 

回答1:

How about

sub foo()   dim r As Range, rows As Long, i As Long   Set r = ActiveSheet.Range("A1:Z50")   rows = r.rows.Count   For i = rows To 1 Step (-1)     If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete   Next End Sub 


回答2:

Try this

Option Explicit  Sub Sample()     Dim i As Long     Dim DelRange As Range      On Error GoTo Whoa      Application.ScreenUpdating = False      For i = 1 To 50         If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then             If DelRange Is Nothing Then                 Set DelRange = Range("A" & i & ":" & "Z" & i)             Else                 Set DelRange = Union(DelRange, Range("A" & i & ":" & "Z" & i))             End If         End If     Next i      If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp LetsContinue:     Application.ScreenUpdating = True      Exit Sub Whoa:     MsgBox Err.Description     Resume LetsContinue End Sub 

IF you want to delete the entire row then use this code

Option Explicit  Sub Sample()     Dim i As Long     Dim DelRange As Range      On Error GoTo Whoa      Application.ScreenUpdating = False      For i = 1 To 50         If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "Z" & i)) = 0 Then             If DelRange Is Nothing Then                 Set DelRange = Rows(i)             Else                 Set DelRange = Union(DelRange, Rows(i))             End If         End If     Next i      If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp LetsContinue:     Application.ScreenUpdating = True      Exit Sub Whoa:     MsgBox Err.Description     Resume LetsContinue End Sub 


回答3:

To make Alex K's answer slightly more dynamic you could use the code below:

Sub DeleteBlankRows()  Dim wks As Worksheet Dim lngLastRow As Long, lngLastCol As Long, lngIdx As Long, _     lngColCounter As Long Dim blnAllBlank As Boolean Dim UserInputSheet As String  UserInputSheet = Application.InputBox("Enter the name of the sheet which you wish to remove empty rows from")  Set wks = Worksheets(UserInputSheet)  With wks     'Now that our sheet is defined, we'll find the last row and last column     lngLastRow = .Cells.Find(What:="*", LookIn:=xlFormulas, _                              SearchOrder:=xlByRows, _                              SearchDirection:=xlPrevious).Row     lngLastCol = .Cells.Find(What:="*", LookIn:=xlFormulas, _                              SearchOrder:=xlByColumns, _                              SearchDirection:=xlPrevious).Column      'Since we need to delete rows, we start from the bottom and move up     For lngIdx = lngLastRow To 1 Step -1          'Start by setting a flag to immediately stop checking         'if a cell is NOT blank and initializing the column counter         blnAllBlank = True         lngColCounter = 2          'Check cells from left to right while the flag is True         'and the we are within the farthest-right column         While blnAllBlank And lngColCounter  "" Then                 blnAllBlank = False             Else                 lngColCounter = lngColCounter + 1             End If          Wend          'Delete the row if the blnBlank variable is True         If blnAllBlank Then             .rows(lngIdx).delete         End If      Next lngIdx End With   MsgBox "Blank rows have been deleted."   End Sub 

This was sourced from this website and then slightly adapted to allow the user to choose which worksheet they want to empty rows removed from.



回答4:

This worked great for me (you can adjust lastrow and lastcol as needed):

Sub delete_rows_blank2()  t = 1 lastrow = ActiveSheet.UsedRange.Rows.Count lastcol = ActiveSheet.UsedRange.Columns.Count  Do Until t = lastrow  For j = 1 To lastcol      If Cells(t, j) = "" Then          j = j + 1              If j = lastcol Then             Rows(t).Delete             t = t + 1             End If      Else          t = t + 1      End If  Next  Loop  End Sub 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!