我使用宏将文本文件导入excel。现在,我需要帮助选择要删除的每列中的多个项目。
在A栏中,我需要删除:2, - ,St,T。这些项通常一直在文本文件中。但有时,我会得到其他数字,例如13.我想编写一个代码来过滤A列以选择所有这些项目。如果可能的话,我也想写一个公式来选择少于24的数字。从长远来看,这对我有帮助。然后,删除可见行。
其次,我需要编写一个代码来过滤列b,选择:From和所有空白行。然后,删除可见行。我知道我可以使用宏来执行此操作,但我担心如果行数增加或者在A列中出现“新数字”,宏将无效。
这是我迄今为止所做的,但我知道这是错误的:
Cells.AutoFilter Field:=1, Criteria1:=Array("=St", "=T", "=2", "=--")
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
答案 0 :(得分:1)
Use the Range.CurrentRegion property to define the cells that your AutoFilter method will affect.
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
Step through the code in the VBE by tapping [F8]. You can pause and observe what is going on with your worksheet. The range addresses of the visible rows are reported to the VBE's Immediate window. When you are satisfied with the procedure, remove the commented lines that actually delete the rows.
答案 1 :(得分:0)
试试这个
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