PC Review


Reply
Thread Tools Rate Thread

How do I direct a macro to a newly opened workbook

 
 
dave caizley
Guest
Posts: n/a
 
      12th Mar 2008
A Macro in my MasterFile.XLS opens a New Workbook and pastes a copy of one of
the sheets into the new workbook. I want to then return to MasterFile.xls,
clear contents in cell C6 of the Input page and then return to the newly
created workbook.

Whilst in the abbreviated coding below, the new book is Book12, it could of
course be any number depending on how many times I run the process in the
course of the day. After creating the new workbook, it gets saved with a name
so there is only ever one new workbook open at a time but with a variable
number.

How do I achieve this ?

Cells.Select
Selection.Copy

Workbooks.Add Template:="Workbook"
Cells.Select
ActiveSheet.Paste

Windows("MasterFile.xls").Activate
Sheets("Input Page").Select
Range("C6").Select
Application.CutCopyMode = False
Selection.ClearContents


Windows("Book12").Activate
Range("F31").Select

End Sub
 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      12th Mar 2008
Something like this should do

dim wbkNew as workbook

set wbknew = Workbooks.Add Template:="Workbook"

thisworkbook.Sheets("Sheet1").Cells.copy Destination:= _
wbknew.sheets("Sheet1").Cells

thisworkbook.Sheets("Input Page").Range("C6").ClearContents

Range("F31").Select

--
HTH...

Jim Thomlinson


"dave caizley" wrote:

> A Macro in my MasterFile.XLS opens a New Workbook and pastes a copy of one of
> the sheets into the new workbook. I want to then return to MasterFile.xls,
> clear contents in cell C6 of the Input page and then return to the newly
> created workbook.
>
> Whilst in the abbreviated coding below, the new book is Book12, it could of
> course be any number depending on how many times I run the process in the
> course of the day. After creating the new workbook, it gets saved with a name
> so there is only ever one new workbook open at a time but with a variable
> number.
>
> How do I achieve this ?
>
> Cells.Select
> Selection.Copy
>
> Workbooks.Add Template:="Workbook"
> Cells.Select
> ActiveSheet.Paste
>
> Windows("MasterFile.xls").Activate
> Sheets("Input Page").Select
> Range("C6").Select
> Application.CutCopyMode = False
> Selection.ClearContents
>
>
> Windows("Book12").Activate
> Range("F31").Select
>
> End Sub

 
Reply With Quote
 
Gary''s Student
Guest
Posts: n/a
 
      12th Mar 2008
Right after:
Workbooks.Add Template:="Workbook"
insert the line:

s = ActiveWorkbook.Name


and then instead of:
Windows("Book12").Activate
use:

Windows(s).Activate


This works because after you Add the new workbook, it becomes the Active
workbook and we set the variable s to remember its name.
--
Gary''s Student - gsnu200773


"dave caizley" wrote:

> A Macro in my MasterFile.XLS opens a New Workbook and pastes a copy of one of
> the sheets into the new workbook. I want to then return to MasterFile.xls,
> clear contents in cell C6 of the Input page and then return to the newly
> created workbook.
>
> Whilst in the abbreviated coding below, the new book is Book12, it could of
> course be any number depending on how many times I run the process in the
> course of the day. After creating the new workbook, it gets saved with a name
> so there is only ever one new workbook open at a time but with a variable
> number.
>
> How do I achieve this ?
>
> Cells.Select
> Selection.Copy
>
> Workbooks.Add Template:="Workbook"
> Cells.Select
> ActiveSheet.Paste
>
> Windows("MasterFile.xls").Activate
> Sheets("Input Page").Select
> Range("C6").Select
> Application.CutCopyMode = False
> Selection.ClearContents
>
>
> Windows("Book12").Activate
> Range("F31").Select
>
> End Sub

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run a macro as soon a a workbook (.xls) is opened? =?Utf-8?B?U2FuZ2FtZXNoIEFjaGFyeWE=?= Microsoft Excel Worksheet Functions 1 15th Dec 2005 11:15 PM
Close file and run macro from newly opened file =?Utf-8?B?UHJhZGlwIEphaW4=?= Microsoft Excel Programming 1 23rd Apr 2005 11:39 PM
Win98SE, Excel2000: Newly created ODBC from Excel tables work only, when source workbook is opened !!! Arvi Laanemets Microsoft Excel Misc 1 30th Sep 2004 10:09 AM
Looking for a way to run a macro when the workbook is opened Bob Reynolds Microsoft Excel Programming 3 25th Sep 2004 02:58 PM
Run a macro when a workbook is opened Mervyn Thomas Microsoft Excel Programming 2 25th Jul 2003 11:40 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:00 AM.