この記事では、自動フィルターで使用される基準を表示するマクロを作成します。

生データは、名前、電話番号、電子メールID、会社名などのクライアントの詳細で構成されます。

ArrowRawData

Company列にフィルターを適用しましたが、フィルターが適用されている会社の名前を判別します。

ArrowFilterData

「フィルターデータの分離」ボタンをクリックして、フィルターが適用される基準を取得します。

ArrowOutput

コードの説明

IntRow = Range( “A10″)。CurrentRegion.Rows.Count + 12上記のコードは、出力が表示される行番号を取得するために使用されます。

ActiveSheet.AutoFilter.Filters(IntCol).On上記のコードは、フィルターが特定の列に適用されているかどうかを確認するために使用されます。

For Each StringValue In .Criteria1 MainString = MainString + Mid(StringValue、2)+ “|”

次へ上記のコードは、フィルターで使用されるすべての基準値で構成される文字列を作成するために使用されます。

Range( “A10″)。CurrentRegion.SpecialCells(xlCellTypeVisible).Copy _ Cells(IntRow + 1、1)

上記のコードは、表示されている行を指定された宛先にコピーするために使用されます。

コードについては以下に従ってください

Option Explicit

Sub FilterCriteria()

'Declaring variables

Dim IntRow, IntCol As Integer

Dim MainString, StringValue As Variant

'Initializing the row and column number

IntRow = Range("A10").CurrentRegion.Rows.Count + 12

IntCol = 1

'Looping through all the cells until blank cell is encountered in the 10th row

Do Until IsEmpty(Cells(10, IntCol))



With ActiveSheet.AutoFilter.Filters(IntCol)

'Checking whether filter is applied on the column

If .On Then



MainString = "Filter On Column no. " & IntCol & " on values : "



'Creating text which consists of values used in the filter

For Each StringValue In .Criteria1

MainString = MainString + Mid(StringValue, 2) + "|"

Next



'Assigning value to cell

Cells(IntRow, 1).Value = MainString



Exit Do



End If



End With



IntCol = IntCol + 1

Loop

'Copying the visible cells to row after the filter data

Range("A10").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy _

Cells(IntRow + 1, 1)

End Sub

このブログが気に入ったら、FacebookやFacebookで友達と共有してください。

皆様からのご意見をお待ちしております。私たちの仕事を改善し、あなたのために改善する方法をお知らせください。 [email protected]までご連絡ください