[英]How to copy only visible cells from workbook to workbook?
I've never used VBA before, I don't know the commands and stuff.我以前从未使用过 VBA,我不知道命令和东西。 I really trying and I need some help please.
我真的很努力,我需要一些帮助。 I've to copy only the visible data from specified columns and paste to another worksheet, but I receive Subscript out of range error, while running the code.
我只需要复制指定列中的可见数据并粘贴到另一个工作表,但在运行代码时收到下标超出范围错误。 In the code I've to select the rows from the 7th row and I think I coded this a bit rough.
在代码中,我必须从第 7 行中选择行,我想我的编码有点粗糙。 Can anyone check my code why is this not working?
任何人都可以检查我的代码为什么这不起作用? Any suggestions for a better solution is appreciated.
对更好的解决方案的任何建议表示赞赏。
Sub CopyData()
Windows("Source.xlsx").Activate
Range("D7, F7, G7, I7, J7, K7, L7, M7, O7, AD7, AX7, CO7, CQ7, CR7, AX7").Select
Range(Selection, Selection.End(xlDown)).Select
If Selection.EntireColumn.Hidden = False Then
Selection.Copy
End If
Windows("Destination.xlsx").Activate
Range("A2").Select
ActiveSheet.Paste
End Sub
Give this a shot:试一试:
Sub CopyData()
'set variables for wkb and ws to copy
Dim wbSource As Workbook
Set wbSource = Workbooks("Source.xlsx")
Dim wsCopy As Worksheet
Set wsCopy = wbSource.Worksheets("Sheet1") 'change name as needed
'set variables for wkb and ws to paste
Dim wbDest As Workbook
Set wbDest = Workbooks("Destination.xlsx")
Dim wsDest As Worksheet
Set wsDest = wbDest.Worksheets("Sheet1")
'copy visible cells for specific range
With wsCopy
Dim lRow As Long
lRow = .Range("D" & .Rows.Count).End(xlUp).Row
Dim rCopy As Range
Set rCopy = Union(.Range("D7:D" & lRow), .Range("F7:F" & lRow), _
.Range("G7:G" & lRow), .Range("I7:I" & lRow), .Range("J7:J" & lRow), _
.Range("K7:K" & lRow), .Range("L7:L" & lRow), .Range("M7:M" & lRow), _
.Range("O7:O" & lRow), .Range("AD7:AD" & lRow), .Range("AX7:AX" & lRow), _
.Range("CO7:CO" & lRow), .Range("CQ7:CQ" & lRow), .Range("CR7:CR" & lRow))
End With
'paste
rCopy.SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A2")
End Sub
Dim rng As Range
Set rng = Application.Intersect(ActiveSheet.UsedRange, Range("A1:H500"))'range depends your work
Windows("Destination.xlsx").Activate
rng.SpecialCells(xlCellTypeVisible).Copy Destination:=Range("A2")
or you just use,或者你只是使用,
ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible)
without intersection method.无交集法。 Totally depends on your work.
完全取决于你的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.