[Excel VBA] How to Copy Only Visible Cells to Another Sheet After Filtering

目次

Overview

There are many situations where you want to transfer rows filtered by AutoFilter directly to another sheet. If you simply execute Copy, hidden rows are also copied. However, by combining this with SpecialCells(xlCellTypeVisible), you can transfer only the visible cells.

This article explains a VBA sample that automates the process from extraction to copying visible cells and deleting the header row.

Prerequisites

ItemContent
Supported ExcelMicrosoft 365 or 2016 and later
Source DataTable starting at cell C3 on the “Data” sheet (with headers)
DestinationResult” sheet
Macro LocationStandard Module

Sample Code (VBA)

Sub CopyVisibleRows()

    Dim srcSheet  As Worksheet        ' Source data sheet
    Dim dstSheet  As Worksheet        ' Destination sheet
    Dim filterRng As Range            ' Filter target range
    
    Set srcSheet  = Worksheets("Data")
    Set dstSheet  = Worksheets("Result")
    Set filterRng = srcSheet.Range("C3").CurrentRegion   ' Table including headers
    
    '--- Extract "Mori" in the "Person" column (3rd column) ---
    filterRng.AutoFilter _
        Field:=3, _
        Criteria1:="Mori"
    
    '--- Copy only visible cells to A1 of Result sheet ---
    filterRng.SpecialCells(xlCellTypeVisible).Copy _
        Destination:=dstSheet.Range("A1")
    
    '--- Delete header row of destination sheet (Optional) ---
    dstSheet.Rows(1).Delete
    
    '--- Display message ---
    MsgBox "Copied extracted data to Result sheet.", vbInformation

End Sub

Key Points of the Code

LineExplanation
9Retrieves the entire table including headers using CurrentRegion. Adjust the starting cell according to your actual data.
12–14Field:=3 indicates the 3rd column (Person column) in the table, and Criteria1:="Mori" extracts the corresponding rows.
17–18SpecialCells(xlCellTypeVisible) copies only the cells displayed after extraction.
21If the header row is unnecessary, you can delete it with Rows(1).Delete. Skip this step if needed.

Application Examples

RequirementImplementation Hint
Keep header rowComment out Rows(1).Delete.
Extract with multiple conditionsCall AutoFilter multiple times or set conditions on different fields to build an AND condition.
Adjust column width after copyAdd dstSheet.Columns.AutoFit to automatically adjust column widths.

Frequently Asked Questions

Q: Can I use this with Tables (ListObject)?

A: Yes. You can transfer only visible cells using the same procedure by setting ListObject.DataBodyRange to filterRng.

Q: Does having many hidden rows affect copy speed?

A: Since only visible cells are copied, it is relatively fast even with large data. If performance is a concern, using Application.ScreenUpdating = False will further improve it.

Summary

By using SpecialCells(xlCellTypeVisible), you can reliably copy only the rows extracted by AutoFilter.

First, run the sample code and verify that the extraction results are correctly transferred to the Result sheet. Simply by changing the column number or extraction conditions, you can apply this to various data transfer tasks.

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

この記事を書いた人

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

目次