+ Reply to Thread
Results 1 to 4 of 4

How do I activate a workbook using a global variable?

  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    WA
    MS-Off Ver
    Excel 2010
    Posts
    4

    How do I activate a workbook using a global variable?

    Activate workbook using a variable?


    Can anyone tell me why the line below is not working?



    Dim ScorecardFile As String
    Sub StartOpen()
    ScorecardFile = "C:\Scorecard\TestingNew\PopPVs.xlsx"
    Application.Run "SoftcodeFileOpen"
    End Sub
    Sub SoftcodeFileOpen()
    Workbooks.Open Filename:=ScorecardFile
    Windows("PopRatings.xlsx").Activate
    Windows(ScorecardFile).Activate '************This line causes the error below
    End Sub



    Error:

    Run-time error: 9

    Subscript out of range



    I've also tried the following and it gets the same error:

    Windows("ScorecardFile").Activate



    Can anyone tell me how to activate workbooks using a varibale like I open the workbook above?

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: How do I activate a workbook using a global variable?

    Activate without the filepath:

    Please Login or Register  to view this content.
    Gary's Student

  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,197

    Re: How do I activate a workbook using a global variable?

    Hi, Erikje,

    thereīs no need to use a modal-wide variable to pass the the string, it could be done by passing the string as a parameter (this would avoid anybody starting the procedure on itīs own when no value has been assigned to the variable which would result in a run-time error.

    Please Login or Register  to view this content.
    Thereīs also no need to jump between workbooks (btw: the window will only display the name of the workbook, not the full path and name, so only the last part of the string should be used using InStrRev to find the last \).

    Please add Code-Tags to your code for better readability.

    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
    02-27-2013
    Location
    WA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: How do I activate a workbook using a global variable?

    Thanks a lot for your 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