[英]VBA: Finding a sheet by name across workbooks and copy a sheet next to it
I am kind of lost with the workbook reference and nesting if condition. 我对工作簿参考和条件嵌套感到有些迷惑。 What I want to achieve is,
我想要实现的是
I am reaching so far but not sure how to copy the non-active-sheet "COMPANY" from the active workbook to the workbook which we find in point 1. 到目前为止,我仍无法确定如何将非活动工作表“ COMPANY”从活动工作簿复制到我们在第1点中找到的工作簿中。
Any suggestion will be much appreciated. 任何建议将不胜感激。
Application.DisplayAlerts = False
For Each wb In Application.Workbooks
For Each ws In wb.Sheets
If ws.Name = "EMPLOYEE" Then wb.Sheets("COMPANY").Delete
Next ws
Next wb
Application.DisplayAlerts = True
By Active workbook do you mean the one with the code module running the operation? “ 活动工作簿”是指运行该代码模块的代码吗? If that is the case, then
ThisWorkbook
can be used to reference it as the parent of the Company worksheet you wish to copy to the workbook you just deleted a Company worksheet from. 在这种情况下,
ThisWorkbook
可以用作您要复制到的公司工作表的父级,以引用到刚刚从中删除公司工作表的工作簿。
Application.DisplayAlerts = False
On Error Resume Next
For Each wb In Application.Workbooks
For Each ws In wb.Sheets
If ws.Name = "EMPLOYEE" Then
wb.Sheets("COMPANY").Delete
ThisWorkbook.Sheets("Company").Copy Before:=wb.Sheets(1)
End If
Next ws
Next wb
Application.DisplayAlerts = True
I find ThisWorkbook
to be more definitive than ActiveWorkbook
particularly in operations like this where the focus may (by default) be changed to the workbook receiving the copy of the Company worksheet. 我发现
ThisWorkbook
比ActiveWorkbook
更具权威性,尤其是在这样的操作中,在这种操作中,焦点(默认情况下)可以更改为接收公司工作表副本的工作簿。
Alternately, you can assign a worksheet object reference to the Company worksheet you wish to copy across and use that reference repeatedly as you progress through the workbooks collection. 或者,您可以将工作表对象引用分配给您希望复制的公司工作表,并在遍历工作簿集合时重复使用该引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.