从VBA范围中选择同一项目
以下代码复制了另一个工作簿中的值。但是,我希望它在源工作簿中出现的方式复制它们,而是希望它复制相同类型的项目(全部),插入空白行,然后复制另一种相同类型的项目。附加的示例。
Sub NewWBandPasteSpecialALLSheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim sh As Worksheet
Dim shNew As Worksheet
Set wb = ThisWorkbook
Workbooks.Add
Set wbNew = ActiveWorkbook
On Error Resume Next
For Each sh In wb.Worksheets
sh.Range("C1:C180, D1:D180,E1:E180, F1:F180, G1:G180").Copy ' I want this range to copy groups of items that are the same and insert a row.
With wbNew.Worksheets
Set shNew = Nothing
Set shNew = .Item(sh.Name)
If shNew Is Nothing Then
.Add After:=.Item(.Count)
.Item(.Count).Name = sh.Name
Set shNew = .Item(.Count)
End If
End With
With shNew.Range("A1")
.PasteSpecial (xlPasteColumnWidths)
.PasteSpecial (xlValues)
End With
Next
End Sub
The code below copies values from another workbook. However, instead of copying values exactly the way they appear in the source workbook, I want it to copy items of the same kind (all of them), insert a blank row, and copy another of the same kind. Example attached.
Sub NewWBandPasteSpecialALLSheets()
Dim wb As Workbook
Dim wbNew As Workbook
Dim sh As Worksheet
Dim shNew As Worksheet
Set wb = ThisWorkbook
Workbooks.Add
Set wbNew = ActiveWorkbook
On Error Resume Next
For Each sh In wb.Worksheets
sh.Range("C1:C180, D1:D180,E1:E180, F1:F180, G1:G180").Copy ' I want this range to copy groups of items that are the same and insert a row.
With wbNew.Worksheets
Set shNew = Nothing
Set shNew = .Item(sh.Name)
If shNew Is Nothing Then
.Add After:=.Item(.Count)
.Item(.Count).Name = sh.Name
Set shNew = .Item(.Count)
End If
End With
With shNew.Range("A1")
.PasteSpecial (xlPasteColumnWidths)
.PasteSpecial (xlValues)
End With
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将重复的行复制到新工作簿
Copy Duplicated Rows to a New Workbook