+ Reply to Thread
Results 1 to 4 of 4

Referring to a worksheet list within a macro

  1. #1
    Registered User
    Join Date
    08-11-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    2

    Referring to a worksheet list within a macro

    Hi all,

    I was hoping someone could help me with a query. I've got a working macro that loops through all the worksheets in a workbook, but excludes certain sheets that I specify in the code. This is done with the following:

    IF worksheet.name <> "Pricing Source" AND worksheet.name <> "Control" THEN...

    I'd like to know how to do two things. Firstly, how do you write out a list of names rather than having loads of 'AND's, i.e. something like:

    If worksheet.name <> ("Pricing Source", "Control", "Name3", "Name4") THEN...

    Secondly, and what will probably be more tricky, I was hoping to find out how to reference a list in a worksheet within the workbook. So, for example, say I have a list in column A of a sheet called 'Pricing Source'. This list corresponds to the names of all the worksheets in the workbook that I would like the macro to loop though. Is there a simple way to reference the list within th macro, so that it will work something like:

    IF worksheetname IN (list of names in column A of 'Pricing Source') THEN...

    To complicate things just a tiny bit, I don't want the first row of column A to be included in the list, and the list is likely to grow longer or shorter over time, so it can't be a static reference of certain cells.

    Any help would be much appreciated!

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Referring to a worksheet list within a macro

    Please Login or Register  to view this content.

  3. #3
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Referring to a worksheet list within a macro

    The first thing I'd suggest is trying the Select Case structure for branching and control. This will allow you to flexibly make decisions based on the name of each sheet:
    Please Login or Register  to view this content.
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  4. #4
    Registered User
    Join Date
    08-11-2011
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Referring to a worksheet list within a macro

    Hi,

    Thanks very much to both of you for your replies. I have to admit that I'm rather inexperienced with macros. I'm working with a piece of code that I found elsewhere, which I've changed ever so slightly and which now works perfectly for me (apart from what I've mentioned above). Unfortunately, I'm a bit unsure how to actually incorporate your suggestions into the code. I've given it a go, but it isn't working for me.

    Perhaps it would help if I showed you the full code. The part I wanted to edit starts " If sh.Name <> DestSh.Name..."

    Please Login or Register  to view this content.
    Last edited by ganders; 08-23-2011 at 10:46 AM.

+ 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