【问题标题】:Filter on a specified column and check for blank if there are blank cells then deletes the entire row过滤指定列并检查是否有空白单元格,然后删除整行
【发布时间】:2020-10-12 04:44:38
【问题描述】:

我对宏很陌生...我希望宏过滤名为“容器”的特定列,并检查是否有任何空白单元格,如果有任何空白单元格,那么它应该删除整行。我下面的代码只是挂起整个 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 的列,然后它应该删除整个/整个相应的行,有空白单元格。

【问题讨论】:

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

标签: excel vba


【解决方案1】:

请看下面的脚本:

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”作为字符串的最后一行。

如果脚本有效,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多