+ Reply to Thread
Results 1 to 5 of 5

using a string variable for sheet name

  1. #1
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    using a string variable for sheet name

    I am trying to use a variable for the name in the Sheets command so that I can put in the names of the CSV file that I have captured in a variable.

    Here is the macro that I recorded using Excel 2007:
    Please Login or Register  to view this content.
    I would like to replace "Heil15 - Wood Stats" with a String variable called FileName1. Here is what I would like to ultimately get to:

    Please Login or Register  to view this content.
    I keep getting: run time error '9', subscript out of Range error. I have searched and searched but can't find an answer so that I can use a String variable in the code. Can somone (re)direct me?

    Thanks.
    Last edited by leetslla; 08-15-2009 at 11:39 PM. Reason: Question resolved

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: using a string variable for sheet name

    Welcome to the forum.

    The error means that there is either no sheet named copyFrom in the active workbook, or no open workbook named copyTo.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: using a string variable for sheet name

    Quote Originally Posted by shg View Post
    Welcome to the forum.

    The error means that there is either no sheet named copyFrom in the active workbook, or no open workbook named copyTo.
    I have assigned CopyTo and CopyFrom as variables. The value they have in the "Watch" window is "Heil15 - Wood Stats", but I get an error when I try to make whatever is inside of the ( ) of Sheets(whatever) a variable.

    Is there any way to make the contents inside of Sheets.(whatever) a variable?

    Right now typing
    Please Login or Register  to view this content.
    recognizes the sheet in the CSV file. When I say
    Please Login or Register  to view this content.
    and write
    Please Login or Register  to view this content.
    , I get the error.

    Is there a solution to making the name of the sheet a variable and working as I want it to? Or is there a different way around the problem?

    Thanks.
    Last edited by leetslla; 08-13-2009 at 11:36 PM. Reason: typo

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: using a string variable for sheet name

    Not
    Please Login or Register  to view this content.
    but
    Sheets(CopyTo)
    Post a workbook if you're still having a problem.

  5. #5
    Registered User
    Join Date
    08-13-2009
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: using a string variable for sheet name

    SHG,

    Thanks for your input because it helped me realize my error. I was referencing an active workbook that did not have the sheet that I was needing--as you stated. Once I referenced the correct workbook, then the variable name worked in Sheets(CopyTo)...

+ 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