+ Reply to Thread
Results 1 to 7 of 7

using a variable workbook's name to define another variable

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    using a variable workbook's name to define another variable

    Hello,

    I have the following code that is giving me the error Object doesn't support this property or method :

    First, I have publicly declared the following:

    Please Login or Register  to view this content.
    As I need to use this outside of the current function.

    Next, I have the following:
    Please Login or Register  to view this content.
    Excel is having an issue with the following line:
    Please Login or Register  to view this content.
    And I'm not sure why. If I type msgbox DWMBook.name, I get the correct value - but I can't use DWMBook to define StepOne. Within the workbook DWMbook, in cell B6, is the NAME of the workbook I want to define as StepOne. The file is open, and the name matches exactly. I can't figure out why this isn't working....

    To make things more complicated...... If I do this:

    Please Login or Register  to view this content.
    it works fine - I get back the value inside of the workbook that has been set to DWMbook, in the Start sheet, Range B6.

    But then if I do:
    Please Login or Register  to view this content.
    I get the error: Run Time Error 424: Object Required
    Last edited by lordterrin; 12-30-2013 at 04:09 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: using a variable workbook's name to define another variable

    What exacty are you trying to do here?
    Please Login or Register  to view this content.
    As for this,
    Please Login or Register  to view this content.
    the problem is that StepOne is declared as an Worksheet object.

    So again, what are you trying to do there?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: using a variable workbook's name to define another variable

    In the workbook that is active when the macro starts running, the user has selected two things:

    1. The name of the workbook they want to run macros on, and
    2. The name of the macro they want to run

    the variable StepOne needs to be defined as the workbook name that is listed in range B6 of this workbook.
    the variable StepTwo needs to be defined as the macro which is named in range B9 of this workbook.


    The thing is, this entire process works just fine if I manually F8 through the macro and make sure I manually select the right workbook for Excel to read out of before this line hits. I have the code set to Set StepOne = Sheets("Start").Range("B6").Value, which works because I'm manually selecting the correct workbook ahead of time.

    This is why I'm trying to define the workbook (as DWMBook) so that I can use that as a reference when defining these two variables above.

    So the final result is:

    StepOne.Activate 'activates the workbook that the user chose
    and
    Application.Run StepTwo 'runs the named macro on the active work book, which is StepOne.

    Does this make sense?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: using a variable workbook's name to define another variable

    Sorry I'm a bit confused.

    If the user is naming the workbook in B6 and the macro named in B9 is in that workbook wouldn't it be more like this.
    Please Login or Register  to view this content.
    By the way, this will always error, StepOne is declared as a Worksheet Object so you can't set it to refer to a value from a cell.
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: using a variable workbook's name to define another variable

    Quote Originally Posted by Norie View Post
    Sorry I'm a bit confused.

    If the user is naming the workbook in B6 and the macro named in B9 is in that workbook wouldn't it be more like this.
    Please Login or Register  to view this content.
    By the way, this will always error, StepOne is declared as a Worksheet Object so you can't set it to refer to a value from a cell.
    Please Login or Register  to view this content.
    Sorry I've confused you Maybe an image will help?

    ef.png

    From my post above:

    The thing is, this entire process works just fine if I manually F8 through the macro and make sure I manually select the right workbook for Excel to read out of before this line hits. I have the code set to Set StepOne = Sheets("Start").Range("B6").Value, which works because I'm manually selecting the correct workbook ahead of time.

    I need to define StepOne as a workbook - not a string, because if I define it as a string, I can't possibly execute this code:

    Please Login or Register  to view this content.
    The value of StepOne is an open workbook. I need to activate that workbook, THEN run StepTwo on that workbook.

    My sole issue here is that I don't know how to write the syntax to embed the variable DWMBook into the following lines:

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    132

    Re: using a variable workbook's name to define another variable

    The only way so far I can get this to work is by doing the following:

    Please Login or Register  to view this content.
    This makes DWMbook active, then executes the code on that. Since it's the active workbook, I don't have to define anything. But it's not always optimal for me to do it this way. Instead of doing this:

    Please Login or Register  to view this content.
    I'd like to do this:
    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: using a variable workbook's name to define another variable

    Did you try the code I posted?

    By the way, why do you need this?
    Please Login or Register  to view this content.
    You don't need to activate a workbook to run a macro in it, just include the workbook name when running the macro, see the code I posted.

+ 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. [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
  2. Copy variable range from variable workbook to variable range on current workbook
    By Locster79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2013, 10:37 AM
  3. [SOLVED] TypeName limitation: How to distinguish Worksheet Variable from Workbook variable
    By bagullo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-02-2013, 07:35 AM
  4. VBA Define Variable
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-18-2012, 12:55 PM
  5. How to define variable
    By kishore in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2005, 06:06 AM

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