+ Reply to Thread
Results 1 to 9 of 9

Correct Syntax for Named Worksheet

  1. #1
    Registered User
    Join Date
    07-29-2014
    Location
    CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Correct Syntax for Named Worksheet

    I have named a worksheet as "TestSheet".

    I want to write a formula to delete:

    Dim ws as Worksheet
    If ws.Name = "TestSheet" Then ws.Delete

    What is correct syntax to do this?

    Thank you.

  2. #2
    Registered User
    Join Date
    07-29-2014
    Location
    CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Correct Syntax for Named Worksheet

    Just to further clarify, by "named worksheet" I mean naming it in VB Editor under Excel Objects Properties. I do not mean the name given on the tab.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Correct Syntax for Named Worksheet

    Hi and welcome to the forum,

    See if this does what you want:
    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    07-29-2014
    Location
    CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Correct Syntax for Named Worksheet

    Quote Originally Posted by MarvinP View Post
    Hi and welcome to the forum,

    See if this does what you want:
    Please Login or Register  to view this content.
    Not exactly what I was looking for. I omitted important details in my OP which I subsequently posted. By "named" sheet I am referring to the CodeName which is defined in VBE Properties as opposed to the name on the sheet tab.

    How would I modify the code to the refer to the CodeName?

    Thanks.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Correct Syntax for Named Worksheet

    I don't understand your question.

    You want the name of a sheet but in the VBA code?? I think my code above should work for what I'm thinking. We need a workbook example to help understand the problem

  6. #6
    Registered User
    Join Date
    07-29-2014
    Location
    CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Correct Syntax for Named Worksheet

    From what I understand, a worksheet can be identified by either the Name on the tab (e.g. Settings) given it by the User, or it can be identified by the (Name) assigned to it by Excel (e.g. Sheet1, Sheet2, etc.).

    You can rename the (Name) from Sheet1 to say TestSheet in VB Editor so that you don't have to remember that it was Sheet1.

    For the tab Name (e.g. Settings), your code works.

    However, if I want to use its (Name) (e.g. Sheet1) how would I write it.

    For instance, I tried
    If Worksheets(ShCtr).Name = "Sheet1" Then
    Worksheets(ShCtr).Delete

    but this didn't work.

    thanks.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Correct Syntax for Named Worksheet

    Rather than looping to find the sheet, how about

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    07-29-2014
    Location
    CA, USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Correct Syntax for Named Worksheet

    I finally figured it out. It is the .CodeName property. What I was looking for is:

    Dim ws As Worksheet

    For Each ws In Worksheets
    If ws.CodeName = "TestSheet" Then ws.Delete
    Next ws

    Thanks for you help!

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Correct Syntax for Named Worksheet

    For code name, the non-looping code would be

    Please Login or Register  to view this content.

+ 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. [SOLVED] Correct syntax for referencing a separate worksheet in a formula when.
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-20-2014, 03:01 PM
  2. Named Range syntax when working abstracted from worksheet cells
    By HRCalcNStuf in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-03-2012, 05:48 PM
  3. Help getting my syntax correct
    By MMS6300 in forum Excel - New Users/Basics
    Replies: 8
    Last Post: 02-20-2009, 02:26 PM
  4. looking for correct syntax
    By adsxvii in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-10-2009, 01:40 PM
  5. [SOLVED] Correct syntax for Active Worksheet
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-03-2005, 04:05 PM

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