+ Reply to Thread
Results 1 to 12 of 12

Open Workbook using a variable

  1. #1
    Registered User
    Join Date
    06-07-2010
    Location
    Blantyre, Malawi
    MS-Off Ver
    Excel 2003
    Posts
    20

    Question Open Workbook using a variable

    Hi,

    I am a VBA newbie and am learning the hard way. Can anyone please help?

    I am trying to run a programme with all the code in 1 workbook (CodeWbk.xls) and all the data in another (DataWbk.xls)
    I need to be able to get the user to input the name of the data file he is using and then both save this and then use it as a variable so as to know which xls has the data. My codewbk.xls then should, using vba, open the correct file.

    I am struggling to open a workbook with a variable. The workbook open command just doesn't seem to work for me no matter what I do. (probably just doing wrong things)

    Also where and how would I set a global variable so that this workbook name (Datawbk.xls) is available to all modules.

    Regards,

    Alan

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Open Workbook using a variable

    You might want to have a look at the GetOpenFilename Method.

    If you declare a variable outside a procedure as Public it will be available to all modules.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Open Workbook using a variable

    Does this help?

    Please Login or Register  to view this content.
    hth
    Ajay

  4. #4
    Registered User
    Join Date
    06-07-2010
    Location
    Blantyre, Malawi
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Open Workbook using a variable

    Thanks Ajay,
    This part works. My problem is that when I try to refer to this workbook I get Run time error 13 - Type mismatch.
    One example is listed here:

    Workbooks(Src_wbk).sheets("Control").select
    Thanks for the assistance.

    Regards,

    Alan

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Open Workbook using a variable

    It would just be something like:

    Please Login or Register  to view this content.

    It's worth noting that it's almost never necessary to use Select when writing VBA code.

    For example this:

    Please Login or Register  to view this content.

    Can just be written as:

    Please Login or Register  to view this content.

    Dom

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open Workbook using a variable

    Your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us help you!

    Read this to understand why we ask you to do this

    http://www.thecodecage.com/forumz/me...-variable.html
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Registered User
    Join Date
    06-07-2010
    Location
    Blantyre, Malawi
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Open Workbook using a variable

    I really am sorry. I had not thought through the whole issue as outlined at Excel guru.

    Will not do it again.

    Alan

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Open Workbook using a variable

    Thanks for taking the time to read and understand the rule .

  9. #9
    Registered User
    Join Date
    06-07-2010
    Location
    Blantyre, Malawi
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Open Workbook using a variable

    Hi,
    Sorry again - got the code tags right this time.



    Thanks to all who have helped so far. I am obviously still doing something dumb. Any ideas.

    When I run this test routine it works fine up to the point where I “Set File_Name=Workbooks.Open(File_Name). Up to this point the variable File_Name reads correctly as evidenced by the MsgBox.

    When the Workbooks.Open runs it automatically runs the Workbook Deactivate procedure. The system then “loses” the values in the public variables.
    I have tried removing the Workbook deactivate procedure altogether and still get the same result – my variable loses its value.
    As a result the MsgBox line gets “Run time error 438 – Object doesn’t support this property or method”
    The following line “ File_Name.Sheets("Control").Cells(1, 26).Select “ gets the error “Run time error 1004 – Select method of range class failed”

    I neglected to tell you that I am using Excel 2003 – could this be the issue?


    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,008

    Re: Open Workbook using a variable

    When you run this line:
    Please Login or Register  to view this content.
    File_Name is no longer a String, but a Workbook object (I really suggest you don't reuse the same variable like that) so this line:
    Please Login or Register  to view this content.
    makes no sense. You would need:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  11. #11
    Registered User
    Join Date
    06-07-2010
    Location
    Blantyre, Malawi
    MS-Off Ver
    Excel 2003
    Posts
    20

    Re: Open Workbook using a variable

    Thanks Romperstomper,
    How would one then handle the next line.

    I need to be able to direct the programmes to use sheets in one or another workbook by referring to a variable.

    Regardsw,

    Alan

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,008

    Re: Open Workbook using a variable

    The same way you have it in your code (except you'll need to replace those weird quotation marks round the "A1" with proper ones)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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