复制工作簿名称,然后稍后使用 vba 激活具有复制名称的工作簿

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

Copy Workbook name then Activate workbook with copyed name later with vba

excelvba

提问by xyz

I Have WName = ThisWorkbook.Nameand then I want to set Windows("WName").Activateat another point in my code but is not working.

我有WName = ThisWorkbook.Name,然后我想Windows("WName").Activate在我的代码中的另一个点设置但不起作用。

I am aware this might not make sense but it does with all of the sutf that is going on

我知道这可能没有意义,但它适用于所有正在发生的 sutf

below is a small test sub (I am not using this just trying to get it to work)

下面是一个小的测试子(我不是为了让它工作而使用它)

Thanks

谢谢

 Sub test()
 'Copy Active Workbook Name
 WName = ThisWorkbook.Name

 'Activate a different Workbook
 Windows("MyWorkbookAAA").Activate

 'Activate Original workbook using WName
 Windows("WName").Activate  'Have also tried Windows(WName).Activate

 range("D2:D25").Select
 Selection.Copy

 Windows("MyWorkbookBBB").Activate
 range("C22").Select
 ActiveSheet.Paste

End Sub

采纳答案by Santosh

Try this code:

试试这个代码:

Avoid using Select/Activate in your code . Refer this link

避免在代码中使用 Select/Activate 。参考这个链接

 ThisWorkbook.ActiveSheet.Range("D2:D25").Copy Workbooks("MyWorkbookBBB").ActiveSheet.Range("C22")

enter image description here

在此处输入图片说明

回答by enderland

If you want to keep similar syntax by keeping a reference to your string value WName.

如果您想通过保留对字符串值 WName 的引用来保留类似的语法。

Workbooks(WName).Activate 

This activates that workbook.

这将激活该工作簿。

Also can do

也可以做

ThisWorkbook.Activate

if it's the workbook running your code.

如果它是运行您的代码的工作簿。

(there are easier ways to do the specific operation you are ONLY doing one thing like copying, but this explains how to do what you are trying to do)

(有更简单的方法可以执行您只做复制等一件事的特定操作,但这解释了如何做您正在尝试做的事情)