Results 1 to 6 of 6

Thread: Copying to a new workbook??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Copying to a new workbook??

    Hi

    I'm running a macro that sorts, filters and formats some source data - until now my results have shown in the same workbook as the macro is running from, but I've found it more handy (and a lot of harddrive space saving), if my results appeared in a new workbook.

    I used the macro recorder to try to get a clue of what to do, but it only gave me more questions;

    I'm using a commandbutton to execute the code. The first time it is run it should generate a new workbook and paste the results in there, however the second time it is run it shouldn't create a new workbook (since it has already been created), but merely copy the results into two new sheets. Something like;

    VB Code:
    1. if not workbook("book1") exists then
    2. workbook.add
    3. 'code
    4. else
    5. 'code
    6. end if

    any ideas? bet there is a dead easy solution

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Copying to a new workbook??

    you have a sheets collection in the workbook.
    So you need to add a new sheet (holding a reference variable to it) then copy or do whatever to paste the results into that new sheet.

    Code:
    dim wrk as workbook
    dim sht as worksheet, shtnew as worksheet
    
    set wrk = activeworkbook
    set sht = wrk.sheets(1)
    set shtnew = wrk.sheets.add 'check the help/intellisense for options
    sht.range("A1:b3").copy
    shtnew.cells(1,1).paste
    
    set sht = nothing
    set shtnew = nothing
    set wrk = nothing
    The above code is guidelines only - probably won't run, but should give you an idea.
    Last edited by Ecniv; Jun 27th, 2006 at 03:50 AM. Reason: I cannot type :(

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Copying to a new workbook??

    Thanks, but I'm well aware how to add new sheets to an existing workbook.

    What I want is to add a new workbook and then copy some of the sheets from the existing workbook into the new workbook - only once I have made a new workbook I don't want to make another one, so the second time I run the code I only want to add new sheets to the new workbook and then paste my results into these... Something like the fictive code I wrote in my post.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copying to a new workbook??

    VB Code:
    1. Dim mynewworkbook As Workbook
    2. Set mynewworkbook = Application.Workbooks.Add

    this will create a new workbook ,book1

    pete

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Copying to a new workbook??

    Thanks, but I know how to add a workbook as well.

    What I need is something like;

    VB Code:
    1. if not book1 exists then 'This is the part that bothers me!
    2. workbooks.add 'adding book1
    3. 'copy results to book1
    4. else
    5. 'copy results to book1 - which I'm now certain already exist
    6. end if

    Nicoaj

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Copying to a new workbook??

    your workbook is an xls file

    VB Code:
    1. myfile = "myworkbook.xls"
    2. if  dir(myfile) = "" then
    3.     workbooks.add
    4.   else
    5.      'open myfile
    6. end if
    7.  
    8. 'your code


    you would need to save book1 as myfile after adding it so it will get the same workbook name, for the rest of the code

    pete
    Last edited by westconn1; Jun 27th, 2006 at 07:46 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width