Excel 筛选指定列并检查是否为空(如果有空单元格),然后删除整行

Excel 筛选指定列并检查是否为空(如果有空单元格),然后删除整行,excel,vba,Excel,Vba,我对宏非常陌生。。。我希望宏在名为“Container”的特定列上进行筛选,并检查是否有空白单元格。如果有空白单元格,则应删除整行。我下面的代码只是挂起整个excel,它不工作 我的代码:- Dim tst As Worksheet Dim lRow As Long Dim colNumber As Long Dim r As Range Dim rows As Long Dim i As Long Dim colHB As String Dim ColumnLetter As String D

我对宏非常陌生。。。我希望宏在名为“Container”的特定列上进行筛选,并检查是否有空白单元格。如果有空白单元格,则应删除整行。我下面的代码只是挂起整个excel,它不工作

我的代码:-

Dim tst As Worksheet
Dim lRow As Long
Dim colNumber As Long
Dim r As Range
Dim rows As Long
Dim i As Long
Dim colHB As String
Dim ColumnLetter As String
Dim CCollum As Long

colHB = "Container"

Set tst = ThisWorkbook.Worksheets("POL")

lRow = Range("A1").End(xlDown).Row
CCollum = ActiveWorkbook.Worksheets("POL").UsedRange.Columns.Count

For i = 1 To CCollum
    If ActiveWorkbook.Worksheets("POL").Cells(1, i).Value = colHB Then
        colNumber = i
        ColumnLetter = Split(Cells(1, i).Address, "$")(1)
        GoTo Continue
    End If
Next i
Continue:
  Set r = ActiveWorkbook.Worksheets("POL").Range("A1:" & ColumnLetter & lRow)
  rows = r.rows.Count
  For i = rows To 1 Step (-1)
    If (Len(tst.Cells(i, colNumber))) = 0 Then r.rows(i).EntireRow.Delete
    Next
Set tst = Nothing

名为container的列有很少的空白单元格,因此宏应该在名为container的列上进行筛选,然后它应该删除具有空白单元格的整个/整个对应行


请参见下面的脚本:

Option Explicit

Dim wb As Workbook

Dim sRng As Range
Dim fRng As Range

Dim cel As Range

Dim tRow As Long
Dim fCol As Long


Sub foo()
    
    'setting wb as thisworkbook
    Set wb = ThisWorkbook
    
    'row 1 assigned into fRng(find range) object
    Set fRng = wb.Sheets("POL").Rows(1).Find(what:="Container", LookIn:=xlValues, lookat:=xlWhole)
    
    'gets fRng range object, and assigns its column property value into fCol variable
    fCol = fRng.Column
    

    'finding the last row for column 1, make sure you select a col that covers the whole data set, based on last row
    tRow = wb.Sheets("POL").Cells(Rows.Count, fCol).End(xlUp).Row

    'assigning range based on col index based on str search(fCol) + total row count (tRow) in sRng range object
    With wb.Sheets("POL")
    
        Set sRng = .Range(.Cells(1, fCol), .Cells(tRow, fCol))
    
    End With
    
    'call sub deltR, the function does a filter based on range, string, and col number passed as argument.
    'passing arguments: range (sRng), delete anything not empty, on col#1 (sRng has only one range = columns("U:U" + tRow)
    Call deltR(sRng, "", 1)


End Sub





Private Sub deltR(ByRef sRng As Range, ByVal aStr As String, ByVal f As Integer)

    'this sub procedure looks for a string (aStr) passed in (sRng) range object range, based on col number (f)
    With sRng

        .AutoFilter field:=f, Criteria1:=aStr
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

    End With

    wb.Sheets("POL").AutoFilterMode = False

    Set sRng = Nothing

End Sub
请注意,trow将把“Container”作为字符串的列中的最后一行计算在内


请让我知道脚本是否工作。

什么是错误,或者它没有错误,但没有提供您想要的结果?我已经告诉过您使用自动筛选。如果使用自动筛选,您不需要任何循环。此代码没有结果!它只是运行和挂起你能分享你的文件吗..如果你没有任何问题吗?没有上传文件的选项,但是我在上面的问题中又粘贴了一个文件的屏幕截图。。。。