xyz
xyz

Reputation: 2300

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

I Have WName = ThisWorkbook.Name and then I want to set Windows("WName").Activate at another point in my code but is not working.

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

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

Upvotes: 2

Views: 9289

Answers (2)

Santosh
Santosh

Reputation: 12353

Try this code:

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

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

enter image description here

Upvotes: 1

enderland
enderland

Reputation: 14135

If you want to keep similar syntax by keeping a reference to your string value 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)

Upvotes: 1

Related Questions