Consulting

Results 1 to 3 of 3

Thread: Simple way to refer to a workbook whose name varies

  1. #1

    Simple way to refer to a workbook whose name varies

    Hi all. I had to post this because I was killing myself trying to make it work for a long time.
    Everyday I have a need to open an excel workbook from a previous day and copy data from it to a new work book...so the name always changes. Today I had to open NameOfWorkbook 100510.xls and tomorrow it will be NameOfWorkbook 100610.xls and so on. I could never figure out how to refer to that work book in VBA once I got it opened so I could navigate around between that one and my current one. Below is what I figured out how to do. After I open the previous days workbook, I name the window. It doesn't change the name of the workbook but it does give me a constant to use.
    [VBA]ActiveWindow.Caption = "Yesterday"[/VBA]
    So now no matter what the name of the workbook is (100910, 101010,101110) I just use
    [VBA]Windows("Yesterday").Activate[/VBA]

    I hope someone can benefit from this like I did.
    What happens if you get scared half to death twice?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why not set a global variable to that workbook after opening it

    [vba]

    'code to open workbook
    Set myWB = Activeworkbook
    'more code
    [/vba]

    and then use that variable later

    [vba]

    MsgBox myWB.Name
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Pure lack of knowledge....I must admit that my vba skills are in their infancy. Too bad I didn't find you a month ago.

    Anyway...my solution will also work even if it is not as "proper". I just hoped that it could help someone.
    What happens if you get scared half to death twice?

Posting Permissions

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