【问题标题】:VBA Macro that filters on specific column "U" if column U has data or a date it deletes the entire row如果列 U 有数据或删除整行的日期,则过滤特定列“U”的 VBA 宏
【发布时间】:2020-10-07 15:51:23
【问题描述】:

我是 VBA 的新手,宏。如果该列有日期或数据,我希望宏过滤列“U”,最好使用列名“从装货港出发的船舶”,然后删除整个相应的行。 我下面的代码不起作用...请帮助我在下面粘贴代码和 excel 屏幕以供参考

Private Sub CommandButton5_Click()

ActiveWorkbook.Worksheets("POL").Range("U").NumberFormat = "m/d/yyyy"
Rows(2).Select

Rows(1).AutoFilter
 
'Create a variable that = the last used row in column U
lr = Cells(Rows.Count, "U").End(xlUp).Row
 
'Create a looping variable # to go backwards from the last used Row# to 1
For i = lr To 1 Step -1
    If Cells(i, "U").Value < #1/3/2009# Or Cells(i, "U").Value > #2/3/2010# Then
        Rows(i).EntireRow.Delete shift:=xlUp
    End If
Next i

End Sub

如果 U 列“从装货港出发的船舶”有数据或日期,则也删除整个相应的行。而不是给出一个列号“U”列名“从装货港出发的船舶”会更好,因为列号可能会有所不同......请帮助

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我编写了一个宏,它将删除列“U”(或 column21)上非空白的任何内容,包括整个行,在名为“POL”的工作表中

    根据您的描述,我将执行以下操作:

    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:="Vessel Departed from Port of Loading", 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, 1).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
    

    只需运行子过程“foo()”即可运行脚本。

    我根据您上面编写的代码将工作表命名为“POL”。我不知道您的工作簿的结构如何。所以我根据你的解释猜测了

    试一试,然后在 cmets 中与我联系以解决可能出现的问题。

    更新:请再次检查,我创建了一个额外的范围对象,它在第 1 行搜索“从装货港出发的船只”字符串,并返回 col 索引 最后我也删除了“set wb = nothing”。我想这可能是导致我在 cmets 中读到的错误。

    再试一次,如果您有任何问题,请告诉我。

    谢谢

    【讨论】:

    • 完美的@Victor Song 这已经奏效了!但是,它已停止在“wb.Sheets("POL").AutoFilterMode = False" 处循环,其中我将代码提供/更改为“ActiveWorkbook.Worksheets("POL").AutoFilterMode = False”,之后它正常工作......但是,我不想给出列号“U1:U”,而是想提及列名,即“从装货港出发的船舶”,因为列号 U1:U 可能会有所不同,以便更好地给出一个列名,基于该列名进一步删除数据......如果你能帮助我,我将非常感激。再次感谢。
    • 我在您的描述中看到了,并且看到了该评论。有一种方法可以根据字符串查找列位置。如果您正在寻找名称为“船舶离开港口装载”的列,则可以实施该脚本。我会更新脚本并发送给你
    • 请参阅更新,我创建了一个额外的范围对象,它在第 1 行搜索“从装货港出发的船只”字符串,并返回 col 索引。现在脚本将找到包含“Vessel (...) Loading”的列并搜索非空白并删除整行。
    • 嗨@Victor Song 我今天又发布了 1 个查询,我正在为此苦苦挣扎...如果您能检查并帮助我,我会很高兴...查询标题是“过滤特定列并删除所有未来日期的数据”谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2014-08-24
    相关资源
    最近更新 更多