简体   繁体   English

Excel VBA筛选2列

[英]Excel VBA to filter 2 columns

Is it possible that it will filter 2 columns? 是否可以过滤2列? Like it will filter country and date (which are less than today's date) or if possible it will filter only the highlighted cell on the second filter. 像它将过滤国家和日期(小于今天的日期),或者如果可能的话,它将仅过滤第二个过滤器上突出显示的单元格。 Thank you. 谢谢。 Btw, I'm using Ron de Bruin codes, copyrights to him. 顺便说一句,我使用的是罗恩·德·布鲁因(Ron de Bruin)代码,版权归他所有。

Example of what I'm looking: sample excel 我正在寻找的示例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

When you filter once using 当您使用一次过滤

FilterRange.AutoFilter Field:=FieldNum, _
                           Criteria1:=Cws.Cells(Rnum, 1).Value

all you have to do to filter it further is write another analogous statement after that: 您需要做的进一步过滤它的是在那之后写另一个类似的语句:

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

If you do so, it will be filtered on both chosen columns. 如果这样做,它将在两个选定的列上进行过滤。 Same goes for three, four, or multiple other filters on the same dataset. 同一数据集上的三个,四个或多个其他过滤器也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM