查找并过滤具体列中的特定文本

时间:2018-01-23 13:23:07

标签: excel vba excel-vba

希望有人能够很好地理解如何解决我遇到的问题。 我有一个名为"内容总计"

我在不同的列中设置了两个自动过滤器,然后在此列"内容总计"中,我想找到所有行,其中包含单词" Rebill"。

过滤器的设置效果很好,但是我试图找到单词" Rebill"的部分代码根本不起作用。

这是我的代码:

Sheets("DATA").Select

Const TagetColumnLabel_2 = "Logistics/CTD"
Const TagetColumnLabel_1 = "Contents Total"
Dim tbl As ListObject
Dim i As Integer
Dim r4, r00, r5, z As Long

Dim myArray As String
Group = Array("SG185", "US1038", "AU1063", "FK1043")
Dim element As Variant

SubGroup = Array("Investigational Drugs", "IP Return")
Dim SubElement As Variant

SubGroup1 = Array("Kitproduction")
Dim SubElement1 As Variant


Set tbl = Sheets("DATA").ListObjects("tb_DATA")

r4 = Rows("1").Find("Ch To key").Column
r00 = Rows("1").Find("Logistics/CTD").Column
r5 = Rows("1").Find("Contents Total").Column

    For Each element In Group

        If element = "SG185" Then

                With Sheets("DATA")
                    .ListObjects("tb_DATA").Range.AutoFilter Field:=r4, Criteria1:=element
                    .ListObjects("tb_DATA").Range.AutoFilter Field:=r00, Criteria1:=""

                For i = 1 To tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Rows.Count

                    If InStr(1, tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Column, "Rebill") Then
                        tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]"

                    Else

                        For Each SubElement In SubGroup
                                .ListObjects("tb_DATA").Range.AutoFilter Field:=r5, Criteria1:=SubElement
                                tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]"
                        Next SubElement

                        For Each SubElement1 In SubGroup1
                                .ListObjects("tb_DATA").Range.AutoFilter Field:=r5, Criteria1:=SubElement1
                                tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Search]]"
                        Next SubElement1
                    End If
                Next i
            End With

            tbl.ShowAutoFilter = False
        Else

                With Sheets("DATA")
                .ListObjects("tb_DATA").Range.AutoFilter Field:=r4, Criteria1:=element
                .ListObjects("tb_DATA").Range.AutoFilter Field:=r00, Criteria1:=""
                    tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]"
            End With
        End If
    Next element

tbl.ShowAutoFilter = False

这部分代码无效:

For i = 1 To tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Rows.Count

If InStr(1, tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Column, "Rebill") Then
tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]"

请知道,有人知道如何轻松查找特定的文字/单词" Rebill"? 我非常感谢每一个建议。 非常感谢提前!

2 个答案:

答案 0 :(得分:0)

我认为您的错误在于您尝试使用InStr本质上返回一个列号,尝试使用类似的方式更改您的问题代码;

          For i = 1 To tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Rows.Count
                Dim val As Variant
                Dim foundrebil As Range
                val = tbl.ListColumns(TagetColumnLabel_1).DataBodyRange.Column 'find column number and store in val
                Set foundrebill = Sheets("DATA").Columns(val).Find(What:="ReBill") 'search for Rebill in that column
                If Not foundrebill Is Nothing Then 'if found
                    val2 = tbl.ListColumns(TagetColumnLabel_2).DataBodyRange.Column 'get the column number of TargetColumnLabel_2
                    Sheets("DATA").Cells(i, val2).FormulaR1C1 = "=tb_DATA[[#This Row],[Service/Log Formula]]" 'enter formula in that column and row
                Else

答案 1 :(得分:0)

  

我知道代码有点复杂,而且不太好。

我在理解你想要什么时遇到了一些麻烦,但是我知道你在内容总计中过滤了Rebill,你还有另外两个过滤条件。

代码末尾rVisible将保留对Rebill数据的引用,或rSelection对前10行Rebill数据的引用。

Sub Test()

    Dim wrkSht As Worksheet
    Dim rContentsTot As Range
    Dim rChToKey As Range
    Dim rCTD As Range
    Dim tbl As ListObject
    Dim lTblFirstCol As Long
    Dim rVisible As Range
    Dim rRow As Range
    Dim rSelection As Range
    Dim lCounter As Long

    Set wrkSht = ThisWorkbook.Worksheets("Data")
    Set tbl = wrkSht.ListObjects("tb_DATA")

    With tbl

        lTblFirstCol = .HeaderRowRange.Column

        Set rContentsTot = .HeaderRowRange.Find("Contents Total")
        Set rChToKey = .HeaderRowRange.Find("Ch To key")
        Set rCTD = .HeaderRowRange.Find("Logistics/CTD")

        'Only continue if all columns have been found.
        If Not rContentsTot Is Nothing And Not rChToKey Is Nothing And Not rCTD Is Nothing Then

            'Turn on table autofilter if it's not on, or show all data if it is.
            If .AutoFilter Is Nothing Then
                .Range.AutoFilter
            Else
                .AutoFilter.ShowAllData
            End If

            'Filter as required.  Field:=1 is first column in table, Field:=5 is the fifth.
            'NB - Is there a better way to return the correct column within the table?
            '     This works, but feel I should be able to equate the found column number to
            '     a column number within the table.
            With .Range
                .AutoFilter Field:=rCTD.Column - lTblFirstCol + 1, Criteria1:="3"
                .AutoFilter Field:=rChToKey.Column - lTblFirstCol + 1, Criteria1:="6"
                .AutoFilter Field:=rContentsTot.Column - lTblFirstCol + 1, Criteria1:="Rebill"
            End With

            '********************************
            'Not sure why I added this next bit of code - it gets the first 10 rows
            'of filtered data.  Think I must've combined two posts into one in my
            'mind overnight (started answering this question yesterday).
            'Anyway.... it's there, it does stuff, so I'm leaving it in
            '********************************

            'Now to grab the top 10 visible rows in the table.
            Set rVisible = .DataBodyRange.SpecialCells(xlCellTypeVisible)
            For Each rRow In rVisible.Rows
                If lCounter < 10 Then
                    If lCounter = 0 Then
                        Set rSelection = rRow
                    Else
                        Set rSelection = Application.Union(rSelection, rRow)
                    End If
                    lCounter = lCounter + 1
                Else
                    Exit For
                End If
            Next rRow

            'Remove filter and select the top 10 rows that appeared in the filter.
            .AutoFilter.ShowAllData
            rSelection.Select

            '******************
            'End of extra code that you may or may not need
            '******************

        Else
            'Raise error as not all columns have been found.
        End If

    End With

End Sub