vba 将一个工作表中的所有筛选数据复制到新工作簿中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8997645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:02:29  来源:igfitidea点击:

Copy all filtered data from one worksheet into new workbook

excelvbaexcel-vba

提问by Matt Rowles

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.

这不起作用。

回答by Joseph

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

看起来您需要先将 SpecialCells 范围设置为 Range 对象,然后再进行复制。尝试这个:

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