Excel VBA筛选2列

时间:2017-07-05 04:25:29

标签: excel-vba vba excel

是否有可能过滤2列?就像它会过滤国家和日期(比今天的日期少)或者如果可能的话它将仅过滤第二个过滤器上突出显示的单元格。谢谢。顺便说一下,我使用Ron de Bruin代码,版权给他。

我正在寻找的例子:sample excel

Sub Send_Row_Or_Rows_2()

Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = ActiveSheet

'Set filter range and filter column (column with e-mail addresses)
Set FilterRange = Ash.Range("A1:H" & Ash.Rows.Count)
FieldNum = 3 'Filter column = B because the filter range start in column A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
        Action:=xlFilterCopy, _
        CopyToRange:=Cws.Range("A1"), _
        CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

'If there are unique values start the loop
If Rcount >= 2 Then
    For Rnum = 2 To Rcount

        'Filter the FilterRange on the FieldNum column
        FilterRange.AutoFilter Field:=FieldNum, _
                               Criteria1:=Cws.Cells(Rnum, 1).Value

        'If the unique value is a mail addres create a mail
        'If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

            With Ash.AutoFilter.Range
                On Error Resume Next
                Set rng = .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
            End With

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .To = Cws.Cells(Rnum, 1).Offest(0, 1).Value
                .Subject = "Test mail"
                .HTMLBody = RangetoHTML(rng)
                .Display  'Or use Send
            End With
            On Error GoTo 0

            Set OutMail = Nothing


        'Close AutoFilter
        Ash.AutoFilterMode = False

    Next Rnum
End If

1 个答案:

答案 0 :(得分:0)

使用

过滤一次时
FilterRange.AutoFilter Field:=FieldNum, _
                           Criteria1:=Cws.Cells(Rnum, 1).Value

所有你需要做的就是进一步过滤它是在那之后写另一个类似的声明:

FilterRange.AutoFilter Field:=FieldNum2, _
                           Criteria1:= 'your criteria

如果您这样做,它将在两个选定的列上进行过滤。同一数据集上的三个,四个或多个其他过滤器也是如此。