+ Reply to Thread
Results 1 to 8 of 8

macro to rename the workbook that has been created in the same macro

  1. #1
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    macro to rename the workbook that has been created in the same macro

    Hi all
    Hope you had a nice weekend! Here is the description of the problem that I`m currently struggling with:

    I have a given macro that at some point creates a new workbook, and in later steps it activates this newly created workbook several times - so the name `book1` appears several times in this macro. The problem is that in some cases (e.g. when you want to use this macro more than once) this newly created workbook is not called `book1`, but `book2`, `book3`etc. - it makes the macro does not work properly. That is why I want to rename the workbook right after creating it, but without saving on the disk. Is that possible? If not, is there any other way to deal with that, e.g. change the code in order to activate the workbook that has been recently created, but keeping in mind that other workbook is being activated as well?

    Thanks in advance for your help.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: macro to rename the workbook that has been created in the same macro

    Please post your code, ideally in a sample workbook.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to rename the workbook that has been created in the same macro

    here is the sample of the code (in a sample workbook) - I don`t know if it works or not, but this gives you the flavour of the issue.
    Attached Files Attached Files

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: macro to rename the workbook that has been created in the same macro

    Use:

    Please Login or Register  to view this content.

    Then replace all occurrences of:

    Please Login or Register  to view this content.

    With:

    Please Login or Register  to view this content.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to rename the workbook that has been created in the same macro

    hm, this should be fine, but in my macro the following error is being shown:
    `object doesn`t support this property or method`

    and then the first line of TheNewWB.Select instead of Book1.Activate is being highlighted. What`s the reason for that? I`ve put
    Please Login or Register  to view this content.
    at the very beginning of my macro, right after the name.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: macro to rename the workbook that has been created in the same macro

    Sorry, should be:

    Please Login or Register  to view this content.

    Didn't test that far down as I didn't have the worksheets to match the code. Tested now.


    Regards, TMS

  7. #7
    Registered User
    Join Date
    07-01-2012
    Location
    Tilburg, the Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: macro to rename the workbook that has been created in the same macro

    I also should have noticed the difference between select and activate
    thank you Trevor

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: macro to rename the workbook that has been created in the same macro

    You're welcome. Thanks for the rep.


    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1