Worksheet Name To Variable

  • Hi Sorry, I feel like this is an obvious one and I should know it but why wont this work?



    Code
    Sitenameshort = "_Bmth"
    
    
        ShtName1 = "Weekly" & Sitenameshort
       
        shtName = ShtName1.Name 'This line doesnt work
        
        Set Nws = Sheets(shtName)


    It Errors on the marked line.


    Thanks


    James

  • Re: Worksheet Name To Variable


    If your goal is to set nws to be sheets("weekly_Bmth")...


    You posted:


    Code
    shtName = ShtName1.Name 'This line doesnt work


    I believe the ".name" is the offending item.


    try:

    Code
    shtName = ShtName1


    ShtName1 is already returning a text variable...



    Of course this all seems redundant anyway since you could simply:

    Code
    nws = sheets(ShtName)


    perhaps I am missing what you are trying to do?

  • Re: Worksheet Name To Variable


    Hi guys


    Sorry, I probably didnt make myself clear.


    ShtName1 is the codename (i.e. Weekly_Bmth)


    therefore:


    shtName = ShtName1.Name is because I want shtName to be whatever the tab name (i.e. the .name) is of the worksheet with the codename above.


    James

  • Re: Worksheet Name To Variable


    OK - I think I understand you....but as written, ShtName1 is retuning a concatenated text string - not a sheet object.


    DO you need:

    Code
    shtName = sheets(ShtName1).Name


    To return the sheet object?

  • Re: Worksheet Name To Variable


    That doesn't seem to work i'm afraid.


    Here is the total code as maybe that will make things clearer:


  • Re: Worksheet Name To Variable


    Hi Guys,


    Unfortunately, due to the nature of the program. It is likely that people will try and rename the sheet tabs. So therefore I try to work with codenames to avoid errors when they change something. So I think i need to do it this way.


    Any suggestions?


    Thanks


    James

  • Re: Worksheet Name To Variable


    Hi Guys!


    That's a real shame I cant do that. I can work around it but It would have tidyed things up in my code.


    Thanks for trying though!


    James

  • Re: Worksheet Name To Variable


    Does this approach not work for you?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!