[Excel VBA] How to Extract and Reorder Specific Columns Using AdvancedFilter

目次

Overview

By using the CopyToRange parameter of AdvancedFilter, you can extract only the necessary columns from your source data and copy them to another sheet in any order you like.

You can achieve this simply by arranging the “desired column names in the desired order” in the header row of the destination sheet. This allows for easy layout changes without needing Pivot Tables or Power Query.

Prerequisites

ItemDetails
Excel VersionMicrosoft 365 / 2016 or later
Source Data“Sales” sheet, Range B3:F200 (includes headers)
Destination Sheet“Export”
Columns to CopyDate, Amount, Customer (3 columns)
Macro LocationStandard Module

Preparation Steps for the Destination Sheet

Enter the headers for the 3 columns mentioned above in range A1:C1 of the “Export” sheet in your desired order.

A1B1C1
DateAmountCustomer

Leave the cells other than the headers blank. AdvancedFilter will recognize the headers in the destination sheet and write only the corresponding column data in that order.

VBA Sample Code

Sub CopySelectedFieldsInOrder()

    Dim rngSource As Range      ' Source data range
    Dim rngDest   As Range      ' Destination header cells
    
    '--- Set Ranges ---
    Set rngSource = Worksheets("Sales").Range("B3").CurrentRegion   ' B3:F200
    Set rngDest   = Worksheets("Export").Range("A1:C1")             ' Headers only
    
    '--- Copy using AdvancedFilter ---
    rngSource.AdvancedFilter _
        Action:=xlFilterCopy, _
        CriteriaRange:=Empty,    ' No conditions (all rows)
        CopyToRange:=rngDest, _
        Unique:=False            ' Change to True to remove duplicate rows

    MsgBox "Copied necessary columns in the desired order to the Export sheet.", vbInformation

End Sub

Code Explanation

LineExplanation
7–8Uses CurrentRegion to get the entire table from the “Sales” sheet, including the header row.
13Omitting CriteriaRange (or setting it to Empty) targets all records without filtering.
14Specifies the destination header range A1:C1 in CopyToRange to control the column order.
15Changing Unique:=False to True allows you to exclude duplicate rows during the copy.

Application Points

GoalHow to Implement
Add a Serial Number columnAfter copying, add code like: With Worksheets("Export").Range("D2").Resize(recordNum): .Formula = "=ROW()-1": End With.
Filter by specific conditionsSet a condition range in CriteriaRange. You can freely combine OR/AND conditions.
Split sheets by monthUse a loop to shift the CopyToRange starting cell for each year/month to automatically generate sheets.

Frequently Asked Questions

Q: I misspelled a header cell, and the column was left blank.

A: The data will not be copied unless the header matches the source data exactly. Please use copy and paste to ensure accuracy.

Q: Will existing data in the destination be overwritten?

A: Yes, if there are existing cells, they will be overwritten; data is not appended below them. If you want to append data, please change rngDest to point to empty cells below the existing data.

Summary

By simply preparing the destination headers for AdvancedFilter‘s CopyToRange and setting the Unique parameter as needed, you can easily copy specific columns in your preferred order.

Please use this method when you want to quickly organize your data layout.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次