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ỏ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

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

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 

Bài Viết Liên Quan

Chủ Đề