+ Reply to Thread
Results 1 to 9 of 9

insert changing file name into vba code

  1. #1
    Registered User
    Join Date
    11-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    23

    insert changing file name into vba code

    I have the following code from recording a macro:

    Please Login or Register  to view this content.
    I am running the macro in open file "1st 05-01-13 MB.xlsx"
    I want to go to file "2nd 05-01-13 MB.xlsx", copy the worksheet "Master" and insert it after the last worksheet in file "1st 05-01-13 MB.xlsx"

    The file names will always begin with "1st" and "2nd" but the date and letters may change.
    I would like to 'clean up ' the code so that it will pick up the correct file names regardless of the change in date and letters after the "1st " and "2nd ".

    Any help would be appreciated.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: insert changing file name into vba code

    Please Login or Register  to view this content.
    Last edited by patel45; 05-25-2013 at 04:24 AM.
    If solved remember to mark Thread as solved

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: insert changing file name into vba code

    Hi, patel45,

    AFAIK ThisWorkbook.Name will always include the extension of the file?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    11-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: insert changing file name into vba code

    Thanks patel45.
    But when I do this, wb2name returns PERSONAL.xlsb, so I think I am missing something still probably because i'm so new to vba. The next line of the sub works fine as it returns "1stSONAL.xlsb" from the assignment as expected. The macro is store in my PERSONAL so is why ThisWorkBook.name is returning PERSONAL.XLSB?

    I am in open file "1st 05-01-13 MB.xlsx" when I call the main macro where the code I listed contained.
    I want to go to file "2nd 05-01-13 MB.xlsx", copy the worksheet "Master" from it and insert it after the last worksheet in file "1st 05-01-13 MB.xlsx"

    how do I get wb2name to return "2nd 05-01-13 MB.xlsx"?
    Sorry to be so dense. Thanks

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: insert changing file name into vba code

    Put this code in a module of 1st 05-01-13 MB.xlsm, not in PERSONAL.xlsb, the 2 files must have xlsm extension
    Please Login or Register  to view this content.
    Last edited by patel45; 05-26-2013 at 03:27 AM.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: insert changing file name into vba code

    Hi, patel45,

    seems you´re having some kind of problem with this thread:
    Please Login or Register  to view this content.
    wb2name hasn´t been assigned when being used, and I would have expected wb1name there.

    @kramtelbuab:
    there is no check if the files are open in Excel but this code would assume the active workbook to be 1st while the code still may be hosted in Personal.xlsb:
    Please Login or Register  to view this content.
    I really do not like this approach very much and would rather work with GetOpenFilename for opening and setting objects for the workbooks.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    11-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: insert changing file name into vba code

    Patel45: your code worked great once I put it in a module in the "1st" file. Thanks

    HaHoBe: As I would like to have the code in the Personal, I tried your code. I got a Compile Error: type mismatch that highlighted "name" in the line
    Please Login or Register  to view this content.
    Not sure what this means. I will go and do some reading on GetOpenFilename as you recommended it as well.

    thanks to you both for your patience and help

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: insert changing file name into vba code

    Hi, kramtelbuab,

    my bad (I had been working with Strings before). It should read
    Please Login or Register  to view this content.
    without name and will set up an object to the workbook.

    @patel45:
    will cause a RTE for sure - this is definitely not a thread you have a lot success with your guessing it seems.

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    11-26-2011
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: insert changing file name into vba code

    Thanks to you both for your help. I was able to incorporate the lines from HaHoBe with successful outcome. Love this forum and the willingness of the contributors to help.

+ 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