+ Reply to Thread
Results 1 to 3 of 3

Trying to activate an open Workbook with a variable

Hybrid View

msf013 Trying to activate an open... 01-22-2014, 01:32 PM
Fotis1991 Re: Trying to activate an... 01-22-2014, 01:38 PM
Norie Re: Trying to activate an... 01-22-2014, 01:53 PM
  1. #1
    Registered User
    Join Date
    01-22-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1

    Trying to activate an open Workbook with a variable

    Hi all,
    I have a script which opens a user-defined workbook, and later on I need to be able to activate that workbook. I have the filepath for the workbook defined, and I've been able to pull the filename from the path and store it as a variable. However, I get a Runtime Error '9': Subscript out of range notification whenever I try to activate the workbook using the variable I have set. Yet, when I copy and paste the variable contents into my Activate command, the script works.


    Dim fNameAndPath As Variant
    fNameAndPath = Application.GetOpenFilename(FileFilter:="All Files (*.), *.", Title:="Select File To Be Opened")
    If fNameAndPath = False Then Exit Sub
    Workbooks.Open fileName:=fNameAndPath
        'Dim filepath As String
        'filepath = fNameAndPath
           ' Title = Right(filepath, Len(filepath) - InStrRev(filepath, " \ "))
          Dim fso As New FileSystemObject
    Dim fileName As String
    fileName = fso.GetFileName(fNameAndPath)
    
        Windows("fileName").Activate
    Last edited by msf013; 01-22-2014 at 01:39 PM.

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Trying to activate an open Workbook with a variable

    Welcome to the forum.

    We'd like to help you but first..

    Pls take some minutes to read forum rules and specially-in this case- rule#3
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

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

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

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

    Re: Trying to activate an open Workbook with a variable

    You could use a variable to reference the workbook when it's opened.
    Set wbOpen = Workbooks.Open( fileName:=fNameAndPath)
    You can now use the variable wbOpen whenever you want to refer to the workbook the user selected.
    If posting code please use code tags, see here.

+ 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 activate a workbook using a global variable?
    By Erikje in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2013, 04:51 PM
  2. [SOLVED] Activate an open workbook
    By jimbolhp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2012, 03:00 PM
  3. Activate Open Workbook
    By Launchnet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2010, 11:42 PM
  4. Need to activate an open workbook
    By TrafficGuy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-06-2008, 04:52 PM
  5. how to Activate the previous Open, workbook..?
    By acegap in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2006, 11:20 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