Consulting

Results 1 to 3 of 3

Thread: Sleeper: Problem with sheet search

  1. #1
    VBAX Regular
    Joined
    Mar 2005
    Posts
    31
    Location

    Sleeper: Problem with sheet search

    Here I am again with a question.
    Still the same old project.
    So I attach a string to the variable City and another string to the variable Land.
    How can I activate the sheet with the name Land - City ?
    I tried:
    1)
    Variable3 = Land & " - " & City
    Sheets(Variable3).Activate
    2)
    Sheets(Land & " - " & City).activate
    On Option 1 he gives a "Subscript out of range" error
    Option 2 is even impossible due to the " " .
    How can I fix this problem?

  2. #2
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    'Subscript out of range' is telling you that there is no sheet with that name.

    Try sticking in a
    Msgbox Land & " - " & City
    as the second line to see if you are getting the correct name.

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by IVY440
    Here I am again with a question.
    Still the same old project.
    So I attach a string to the variable City and another string to the variable Land.
    How can I activate the sheet with the name Land - City ?
    I tried:
    1)
    Variable3 = Land & " - " & City
    Sheets(Variable3).Activate
    2)
    Sheets(Land & " - " & City).activate
    On Option 1 he gives a "Subscript out of range" error
    Option 2 is even impossible due to the " " .
    How can I fix this problem?
    There is nothing fundamentally wrong with the name you are trying to construct for a sheet. Personally, I do not recommend that there be any blanks in any name for anything, but Excel does allow blanks in sheet names. I assume that you have loaded the string variable City and the string variable Land with appropriate text; something like

    City = "Miami"
    Land = "NoMans"

    and are looking for the sheet with name = "NoMans - Miami"

    After you try BlueCactus's suggestion; if that does not resolve you problem, loop through all sheets in VBA and display their names. Perhaps there is something amiss that is not obvious when you simply look at them as tabs.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •