VBA新手需要编写代码来检索列filter中的多个项目,然后删除带回的行

我用一个macros将文本文件导入到excel中。 现在,我需要帮助select每个列中的多个项目删除。

在列A中,我需要删除:2, – ,St,T。这些项目通常始终在文本文件上。 但有时,我得到其他数字,如13。我想写一个代码,将筛选列Aselect所有这些项目。 如果可能的话,我也想写一个公式来selectless于24的数字。从长远来看,这将对我有所帮助。 然后,删除可见的行。

其次,我需要编写一个代码来过滤列b,select:From和所有空白行。 然后,删除可见的行。 我知道我可以用macros做这个,但是我担心如果行数增加或A列中出现一个“新数字”,macros就不会工作。

这是我迄今为止设法做到的,但我知道这是错误的:

Cells.AutoFilter Field:=1, Criteria1:=Array("=St", "=T", "=2", "=--") Range(Selection, Selection.End(xlDown)).Select Selection.Delete Shift:=xlUp 

使用Range.CurrentRegion属性来定义您的AutoFilter方法将影响的单元格。

 Sub filterIt() With Worksheets("sheet1") If .AutoFilterMode Then .AutoFilterMode = False With .Cells(1, 1).CurrentRegion 'first array of criteia (St, T, 2, --) .AutoFilter Field:=1, Operator:=xlFilterValues, _ Criteria1:=Array("St", "T", "2", "--") 'step off the header row With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'determine if there are cells to delete If CBool(Application.Subtotal(103, .Cells)) Then 'there are visible row - delete them! Debug.Print .SpecialCells(xlCellTypeVisible).Address(0, 0) '.SpecialCells(xlCellTypeVisible).EntireRow.Delete End If End With 'clear filter .AutoFilter Field:=1 'second numeric criteia .AutoFilter Field:=1, Criteria1:="<24" 'step off the header row With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'determine if there are cells to delete If CBool(Application.Subtotal(103, .Cells)) Then 'there are visible row - delete them! Debug.Print .SpecialCells(xlCellTypeVisible).Address(0, 0) '.SpecialCells(xlCellTypeVisible).EntireRow.Delete End If End With 'clear filter .AutoFilter Field:=1 'second numeric criteia .AutoFilter Field:=2, Operator:=xlFilterValues, _ Criteria1:=Array("from", "=") 'step off the header row With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0) 'determine if there are cells to delete If CBool(Application.Subtotal(103, .Cells)) Then 'there are visible row - delete them! Debug.Print .SpecialCells(xlCellTypeVisible).Address(0, 0) '.SpecialCells(xlCellTypeVisible).EntireRow.Delete End If End With 'clear filter .AutoFilter Field:=2 End With If .AutoFilterMode Then .AutoFilterMode = False End With End Sub 

点击[F8],浏览VBE中的代码。 您可以暂停并观察工作表中的情况。 可见行的范围地址被报告给VBE的立即窗口 。 当您对过程满意时,请删除实际删除行的注释行。

尝试这个

 Option Explicit Sub DeletRowsByKeywords() Dim dataDB As Range, headerRow As Range Dim keywordsArray As Variant Dim iKey As Integer With ActiveSheet Set dataDB = .Range("A1:A100") '<== set it as per your needs and beware: it must include the first row as the header Set dataDB = dataDB.Resize(.Cells(.Rows.Count, dataDB.Columns(1).column).End(xlUp).Row) ' get only cells down to the last non empty one End With Set headerRow = dataDB.Rows(1).EntireRow '<== set the header row explicitly. it wil be hidden and shown multiple times keywordsArray = Array("St", "2", "--", "T") '<== set your keywords for rows to be deleted For iKey = LBound(keywordsArray) To UBound(keywordsArray) With dataDB .AutoFilter Field:=1, Criteria1:=keywordsArray(iKey) ' filter data on current key If .SpecialCells(xlCellTypeVisible).Count > 1 Then headerRow.Hidden = True .SpecialCells(xlCellTypeVisible).EntireRow.Delete ' filter data again on Product Type headerRow.Hidden = False End If .AutoFilter End With Next iKey End Sub