Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

Tôi muốn xóa các hàng trống mà trích dẫn ERP của tôi tạo ra. Tôi đang cố gắng đi qua tài liệu (

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
1) và đối với mỗi hàng không có dữ liệu trong các ô (
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,
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
3), tôi muốn xóa chúng.

Tôi đã tìm thấy điều này, nhưng dường như không thể cấu hình nó cho tôi.

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

Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

hỏi ngày 21 tháng 2 năm 2012 lúc 14:55Feb 21, 2012 at 14:55

4

Bạn nghĩ thế nào về

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

Đã trả lời ngày 21 tháng 2 năm 2012 lúc 15:15Feb 21, 2012 at 15:15

Alex K.alex K.Alex K.

168K30 Huy hiệu vàng260 Huy hiệu bạc281 Huy hiệu Đồng30 gold badges260 silver badges281 bronze badges

1

Thử cái này

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

Nếu bạn muốn xóa toàn bộ hàng thì hãy sử dụng mã này

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

Đã trả lời ngày 21 tháng 2 năm 2012 lúc 15:13Feb 21, 2012 at 15:13

Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

Siddharth Routsiddharth RoutSiddharth Rout

Phù bằng vàng 144K1717 gold badges201 silver badges246 bronze badges

3

Tôi biết tôi đến muộn bữa tiệc, nhưng đây là một số mã tôi đã viết/sử dụng để thực hiện công việc.

Sub DeleteERows()
    Sheets("Sheet1").Select
    Range("a2:A15000").Select
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Kingsley

13.9k5 Huy hiệu vàng31 Huy hiệu bạc51 Huy hiệu Đồng5 gold badges31 silver badges51 bronze badges

Đã trả lời ngày 12 tháng 12 năm 2018 lúc 21:45Dec 12, 2018 at 21:45

Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

3

Đối với những người được kết nối để loại bỏ các hàng "trống" và "trống" (ctrl + shift + end đi sâu vào bảng tính của bạn) .. đây là mã của tôi. Nó sẽ tìm thấy hàng "thật" cuối cùng trong mỗi tờ và xóa các hàng trống còn lại.

Function XLBlank()
    For Each sh In ActiveWorkbook.Worksheets
        sh.Activate
        Cells(1, 1).Select
        lRow = Cells.Find(What:="*", _
            After:=Range("A1"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row
        
        Range("A" & lRow + 1, Range("A1").SpecialCells(xlCellTypeLastCell).Address).Select
        On Error Resume Next
        Selection.EntireRow.SpecialCells(xlBlanks).EntireRow.Delete
        Cells(1, 1).Select
    Next
    ActiveWorkbook.Save
    ActiveWorkbook.Worksheets(1).Activate
End Function

Mở VBA (Alt + F11), Chèn -> Mô -đun, sao chép qua mã của tôi và khởi chạy nó với F5. ET voila: D

Đã trả lời ngày 5 tháng 3 năm 2019 lúc 8:47Mar 5, 2019 at 8:47

Tôi có một cái khác cho trường hợp khi bạn muốn xóa chỉ các hàng hoàn toàn trống, nhưng không phải là các ô trống đơn. Nó cũng hoạt động bên ngoài Excel, ví dụ: Khi truy cập Excel bằng Access-VBA hoặc VB6.

Public Sub DeleteEmptyRows(Sheet As Excel.Worksheet)
    Dim Row As Range
    Dim Index As Long
    Dim Count As Long

    If Sheet Is Nothing Then Exit Sub

    ' We are iterating across a collection where we delete elements on the way.
    ' So its safe to iterate from the end to the beginning to avoid index confusion.
    For Index = Sheet.UsedRange.Rows.Count To 1 Step -1
        Set Row = Sheet.UsedRange.Rows(Index)

        ' This construct is necessary because SpecialCells(xlCellTypeBlanks)
        ' always throws runtime errors if it doesn't find any empty cell.
        Count = 0
        On Error Resume Next
        Count = Row.SpecialCells(xlCellTypeBlanks).Count
        On Error GoTo 0

        If Count = Row.Cells.Count Then Row.Delete xlUp
    Next
End Sub

Đã trả lời ngày 27 tháng 8 năm 2019 lúc 11:34Aug 27, 2019 at 11:34

AranxoaranxoAranxo

5472 Huy hiệu bạc14 Huy hiệu Đồng2 silver badges14 bronze badges

2

Để làm cho câu trả lời của Alex K năng động hơn một chút, bạn có thể sử dụng mã bên dưới:

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 <= lngLastCol

            'If the cell is NOT blank, trip the flag and exit the loop
            If .Cells(lngIdx, 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

Điều này có nguồn gốc từ trang web này và sau đó được điều chỉnh một chút để cho phép người dùng chọn bảng tính mà họ muốn xóa các hàng bị xóa.

Đã trả lời ngày 18 tháng 12 năm 2017 lúc 21:50Dec 18, 2017 at 21:50

ThảmkidrugskidRugsKid

3047 Huy hiệu bạc25 Huy hiệu Đồng7 silver badges25 bronze badges

Để có chức năng tiếp tục lỗi, bạn phải khai báo các giá trị sổ làm việc và bảng tính như vậy

On Error Resume Next  
ActiveWorkbook.Worksheets("Sheet Name").Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete  
On Error GoTo 0

Tôi đã có cùng một vấn đề và điều này đã loại bỏ tất cả các hàng trống mà không cần phải thực hiện một vòng lặp.

Đã trả lời ngày 30 tháng 4 năm 2018 lúc 14:39Apr 30, 2018 at 14:39

Điều này làm việc tuyệt vời cho tôi (bạn có thể điều chỉnh Lastrow và LastCol khi cần thiết):

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
    'This only checks the first column because the "Else" statement below will skip to the next row if the first column has content.
    If Cells(t, j) = "" Then

        j = j + 1

            If j = lastcol Then
            Rows(t).Delete
            t = t + 1
            End If

    Else
    'Note that doing this row skip, may prevent user from checking other columns for blanks.
        t = t + 1

    End If

Next

Loop

End Sub

Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

Đã trả lời ngày 27 tháng 2 năm 2018 lúc 15:10Feb 27, 2018 at 15:10

Đây là cách nhanh nhất để xóa tất cả các hàng trống (dựa trên một cột)

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
0

Đã trả lời ngày 10 tháng 3 lúc 15:46Mar 10 at 15:46

Hướng dẫn excel vba delete blank rows at bottom of spreadsheet - excel vba xóa các hàng trống ở cuối bảng tính

Làm cách nào để xóa tất cả các hàng trống ở dưới cùng của bảng tính Excel?

Chọn tất cả các hàng được lọc: Nhấn Ctrl + Home, sau đó nhấn phím trục xuống để đi đến hàng dữ liệu đầu tiên, sau đó nhấn Ctrl + Shift + End.Nhấp chuột phải vào bất kỳ ô đã chọn nào và chọn "Xóa hàng" khỏi menu ngữ cảnh hoặc chỉ nhấn Ctrl + - ((trừ dấu hiệu).Nhấp vào OK trong "Xóa toàn bộ hàng?"hộp thoại.Right-click on any selected cell and choose "Delete row" from the context menu or just press Ctrl + - (minus sign). Click OK in the "Delete entire sheet row?" dialog box.