Results 1 to 4 of 4

Thread: VBA Pass string variable to refer to worksheet

  1. #1
    New Member
    Join Date
    Apr 2012
    Posts
    50
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy VBA Pass string variable to refer to worksheet

    Hi All.

    I am trying to refer to a worksheet by a string which is value of a cell.

    This is my code for this part:

    Code:
        month = Sheet15.Range("K1").Value                           'This is the string with the name of the worksheet (User selectable)
        Set y = Worksheets("month")                                   'Here I want to set which sheet we will run the code on
        brlastrow = y.Range("A" & Rows.Count).End(xlUp).Row         'here I want to find the lastrow on that sheet
        Set bigrange = y.Range("C2:C" & brlastrow)                  'Here I would set the range for my loop
    On the Set y row it craps out with subscript out of range error.

    How can I set y to the name of the worksheet that is month?
    I was going with the same syntax we use for referring to named ranges. Am I missing something?

    Here are my Variables

    Dim month As String
    Dim y As Worksheet
    Dim brlastrow As Integer
    Dim bigrange As Range

  2. #2
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,283
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: VBA Pass string variable to refer to worksheet

    Remove the " from month, otherwise the macro thinks you are looking for a sheet called "month"
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  3. #3
    New Member
    Join Date
    Apr 2012
    Posts
    50
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: VBA Pass string variable to refer to worksheet

    Thanks Fluff!

    This helped.
    Admittedly it was a rookie mistake
    "Need a brew....."

  4. #4
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    28,283
    Post Thanks / Like
    Mentioned
    471 Post(s)
    Tagged
    47 Thread(s)

    Default Re: VBA Pass string variable to refer to worksheet

    You're welcome & thanks for the feedback
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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