+ Reply to Thread
Results 1 to 10 of 10

Assign Open Workbook To A Variable Based On A Condition

  1. #1
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Assign Open Workbook To A Variable Based On A Condition

    I have this code at the start of my VBA.

    Sub Starting_Workbook()

    Dim x As Workbook, y As Workbook

    Set x = Workbooks(1)
    Set y = Workbooks(2)

    End Sub

    I have a macro workbook looking at two open workbooks where the workbook name is always variable.
    On one of the workbooks cell E1 always has the text "Wage Code".

    Depending on the order the user opens the workbooks or which one is active when the macro is executed I cant guarantee I will get the result I expect.

    If some extra code can be added so the workbook which contains "Wage Code" in E1 is always set as variable y that would solve my problem.

    Any help would be appreciated.

    Thank you

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Assign Open Workbook To A Variable Based On A Condition

    Hi

    Why not use the workbbok name. i.e.

    Set y = Workbooks("TheWorkbookName.xlsm")
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: Assign Open Workbook To A Variable Based On A Condition

    Hello Richard,

    Your suggestion won't assist me because the workbook names are variable.

    Ideally what I'm looking for is code that will loop through open workbooks and checking cell E1 each time.
    When it finds a workbook that has "Wage Code" as the text in cell E1 it sets that workbook as variable y.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Assign Open Workbook To A Variable Based On A Condition

    Are there several sheets in each workbook and in which case which sheet is the E1 value you want to test? If more than one and assuming the sheet vb codename is always the same - (unlikely it isn't_ then perhaps something like

    Please Login or Register  to view this content.

  5. #5
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,052

    Re: Assign Open Workbook To A Variable Based On A Condition

    Maybe:
    Please Login or Register  to view this content.
    The code assumes you want to search for "Wage Code" in Sheet1 of each workbook.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  6. #6
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: Assign Open Workbook To A Variable Based On A Condition

    Thank you Richard, that is exactly what I needed.

    Regards
    Jonathan Williams

  7. #7
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: Assign Open Workbook To A Variable Based On A Condition

    Thank you Mumps1 your code works for me.

    Regards
    Jonathan Williams

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,052

    Re: Assign Open Workbook To A Variable Based On A Condition

    Glad to help.

  9. #9
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: Assign Open Workbook To A Variable Based On A Condition

    Hello Richard,

    Apologies for coming back to you on some code you gave where I closed the thread.
    Is there a way so your third line of code does refer to sheet 1, but the actual name of the first sheet can be variable.
    I am finding your code works as long as the first sheet in the workbook is called Sheet1.

    Thanks for your help.
    Jonathan

  10. #10
    Registered User
    Join Date
    03-27-2013
    Location
    Wiltshire, England
    MS-Off Ver
    MS 365
    Posts
    84

    Re: Assign Open Workbook To A Variable Based On A Condition

    Hello Richard, if you saw my post earlier today, you can ignore it.

    With some trial and error I have determined that if you change the third line of code to:

    If WB.Worksheets(1).Range("E1") = "Wage Code" Then Set y = wb

    Then the code will work no matter what sheet 1 is actually called.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How do I assign a workbook to a variable?
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-09-2015, 01:45 PM
  2. [SOLVED] Open Workbooks with Variable name (Date) and take specific data based on the workbook
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-24-2014, 08:25 PM
  3. [SOLVED] assign sheet to variable by code name in another workbook
    By karen53 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-17-2014, 11:27 PM
  4. Open variable name workbook & vlookup to find that variable named wookbook
    By vh_llc@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-06-2014, 07:49 PM
  5. [SOLVED] Assign entire row to variable, how do you paste variable values in a new workbook?
    By tv69 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-18-2013, 08:01 PM
  6. assign variable name to the workbook
    By yjmmay34 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-17-2010, 02:18 AM
  7. Assign the name of the workbook to a variable?
    By syntex in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2008, 07:26 AM

Tags for this Thread

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