vba 使用VBA复制范围内所有可见和非空单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18625468/
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 23:09:32  来源:igfitidea点击:

Use VBA to copy all visible and non-empty cells in a range

excel-vbacopyconstantsvisiblevba

提问by Ryan

After filtering, I want to copy all visible and non-empty cells (the cells which contain text). For some reason, my current code isn't working. Any help would be greatly appreciated. Thanks!

过滤后,我想复制所有可见和非空单元格(包含文本的单元格)。出于某种原因,我当前的代码不起作用。任何帮助将不胜感激。谢谢!

  Sheets("Sheet1").Range("S2:S5000").SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants).Copy

回答by Jaycal

Full admission that I did not try your code, but you can also try the following

完全承认我没有尝试你的代码,但你也可以尝试以下

With Sheets("Sheet1").Range("S2:S5000")
    Application.Intersect(.SpecialCells(xlCellTypeVisible), _
                       .SpecialCells(xlCellTypeConstants)).Copy
End With

Making the open ended assumption that the reason for copying is to paste somewhere else, you can update the above code by using

做出开放式假设,即复制的原因是粘贴到其他地方,您可以使用以下代码更新上述代码

With Sheets("Sheet1").Range("S2:S5000")
    Application.Intersect(.SpecialCells(xlCellTypeVisible), _
          .SpecialCells(xlCellTypeConstants)).Copy _
               Destination:= Sheets("destSheet").Range("destRange")
End With