Hannah
Hannah

Reputation: 169

Linking un-named workbook to variable in VBA

I am writing a macro that will copy and paste information form one workbook into another workbook in excel 2010. The workbook that the data is in is the same workbook as the macro. I have made VBA create a new workbook to paste the data in. How do I assign the new workbook that VBA has just created to a variable.

Thanks For Any Help

Upvotes: 0

Views: 330

Answers (2)

Olle Sjögren
Olle Sjögren

Reputation: 5384

You haven't mentioned exactly how you create the workbook, but you can set a reference to the new Workbook object in the same statement that creates it.

Example:

Option Explicit

Sub AddWorkbook()

    Dim oWb As Workbook

    Set oWb = Workbooks.Add

    'Do something with the new workbook
    Debug.Print oWb.FullName

    Set oWb = Nothing
End Sub

Upvotes: 2

Niraj Nawanit
Niraj Nawanit

Reputation: 2451

try seeing names of all workbooks by iterating over Workbooks. I think the name of newly created workbook will "Workbook1" until there is already no other unnamed workbook. So basically newly created workbook is still not unnamed.

Upvotes: 0

Related Questions