简体   繁体   中英

Copy all filtered data from one worksheet into new workbook

I am having trouble finding a surefire to copy only visible cells from one worksheet into a new workbook. My initial workbook is filtered. Something along the lines of:

Sub RangeToNew()

    Dim newBook as Workbook
    Set newBook = Workbooks.Add

    ThisWorkbook.Worksheets("worksheet").SpecialCells(xlCellTypeVisible).Copy _
        Before:=newBook.Worksheets(1)

End Sub

This doesn't work.

Looks like you need to set the SpecialCells range to a Range object first, then do your copy. Try this:

Sub rangeToNew_Try2()
    Dim newBook As Excel.Workbook
    Dim rng As Excel.Range

    Set newBook = Workbooks.Add

    Set rng = ThisWorkbook.Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeVisible)

    rng.Copy newBook.Worksheets("Sheet1").Range("A1")
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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