+ Reply to Thread
Results 1 to 5 of 5

Create New worksheets with conditional logic gives only the first worksheet

  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Create New worksheets with conditional logic gives only the first worksheet

    I have the following data

    Please Login or Register  to view this content.
    I need to create a new sheet for only the first instance of 3, 5, 17 in the Parent Ids column

    The 3 in the Parent Id column equals the 3 in the Id column which has a Title of "Relationships"

    The 5 in the Parent Id column equals the 5 in the Id column which has a Title of "Interorganizational"

    The 17 in the Parent Id column equals the 17 in the Id column which has a Title of "Services"

    The list is dynamic so it is changing but the relationships remain the same

    The following only gives me the first new tab "Relationships" then nothing else

    Please Login or Register  to view this content.
    Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Create New worksheets with conditional logic gives only the first worksheet

    Hi capson,
    try this
    Please Login or Register  to view this content.

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Create New worksheets with conditional logic gives only the first worksheet

    In addition to the very effective nilem's code, here is the explanation for your original code :

    When you make statement like :
    If Cells(i, 2) <> vbNullString Then --> this refer to activesheet!Cells(i,2)

    if current activesheet is Sheet1 then this refer to Sheet1!Cells(i,2)
    if current activesheet is Sheet2 then this refer to Sheet2!Cells(i,2)

    Now, if you add a new sheet (using Worksheets.Add() method), the focus is changed to this new added sheet and it become the activesheet.

    So when you first run the code,
    If Cells(i, 2) <> vbNullString Then --> this refer to Codes!Cells(i,2)

    but as soon as you add a new worksheet ("Relationships" sheet in this example), the focus changed to this sheet, and this "Relationships" become the activesheet.

    Now, if you continue the loop :
    If Cells(i, 2) <> vbNullString Then --> this refer to Relationships!Cells(i,2), not Codes!Cells(i,2) anymore


    So you must make the code always to refer to sheet "Codes", like this :
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Create New worksheets with conditional logic gives only the first worksheet

    Thank nilem, the code worked perfectly

    Thank you Karedog for your helpful explanation

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Create New worksheets with conditional logic gives only the first worksheet

    You are welcome, glad I can help, and thanks too for the rep. points.


    Regards

+ 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. To create folders as per worksheet names and create worksheets under them
    By Anchal in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2013, 03:01 AM
  2. Loop through one worksheet and create new worksheets
    By dd1313 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2012, 07:56 PM
  3. Conditional logic based on name of worksheet
    By jartzh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-24-2009, 04:49 PM
  4. I need to compare two worksheets and create a new worksheet
    By Rajeev Ganesh in forum Excel General
    Replies: 0
    Last Post: 08-02-2006, 09:20 PM
  5. [SOLVED] Create a list in one worksheet of the other worksheets' names
    By Kelli in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  6. Create a list in one worksheet of the other worksheets' names
    By Kelli in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

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