ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro - New Workbook (https://www.excelbanter.com/excel-programming/439766-macro-new-workbook.html)

Goaliemenace

Macro - New Workbook
 
I have a macro that opens a new workbook and creates 120 tabs of information,
everything process correctly works perfectly only when run for the first
time. The issue is when i run the macro again, Excel now opens "book2" The
macro then stops as it is set for Book 1. Can this variable be adjusted for

Cells.Select
Selection.Copy
Windows("book1").Activate

This is where it runs all the subsequent part of the macro from. The macro
before this opens up a new work book with the tab settings then it grabs the
required info and copies it and then goes to the workbook.

Please help

Cheers

broro183[_152_]

Macro - New Workbook
 

hi Goaliemenace

Here are two examples of how you can set a variable to be a workbook.
The first is when you open an existing file & the second is if you are
create a file...



VBA Code:
--------------------


Option Explicit
Sub Macro2()
Dim Createdwb As Workbook
Set Createdwb = Workbooks.Open(Filename:="blah.xls")
'or
Set Createdwb = Workbooks.Add
'other code, eg...
Createdwb.activate
'free memory
Set Createdwb = Nothing
End Sub

--------------------




hth
Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: 333
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180996

Microsoft Office Help


Goaliemenace

Macro - New Workbook
 
The latter worked perfectly.

Really appreciate your help

Cheers



"broro183" wrote:


hi Goaliemenace

Here are two examples of how you can set a variable to be a workbook.
The first is when you open an existing file & the second is if you are
create a file...




VBA Code:
--------------------



Option Explicit
Sub Macro2()
Dim Createdwb As Workbook
Set Createdwb = Workbooks.Open(Filename:="blah.xls")
'or
Set Createdwb = Workbooks.Add
'other code, eg...
Createdwb.activate
'free memory
Set Createdwb = Nothing
End Sub


--------------------




hth
Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: 333
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180996

Microsoft Office Help

.


broro183[_153_]

Macro - New Workbook
 

Thanks for the feedback - I'm pleased I could help :-)

Rob


--
broro183

Rob Brockett. Always learning & the best way to learn is to
experience...
------------------------------------------------------------------------
broro183's Profile: 333
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=180996

Microsoft Office Help



All times are GMT +1. The time now is 10:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com