+ Reply to Thread
Results 1 to 5 of 5

Sheet duplicated based on specific criteria using VBA

  1. #1
    Registered User
    Join Date
    04-19-2021
    Location
    Canada
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Sheet duplicated based on specific criteria using VBA

    Hi,

    I am working on a Grading Form that generates automatically Exam sheets based on a list of students by clicking on a “Start” button. If 10 students are registered, then 10 Exam sheets are generated when clicking on “Start”. If 15 students, then 15 Exam sheets are generated…

    The tricky part that I encounter is that based on the student “age” entered, the Exam Sheet generated should be different: 2 possible sheets can be generated: “Exam Sheet Child” or “Exam Sheet Adult” (Both sheets already exist in the workbook and would need to be duplicated based on the student “age”).

    My problem is that the Exam Sheet are created but do not correspond to the correct “age” criteria. It only does for the first student in the list while other only take the “Exam Sheet Adult”. I’m sure that the problem can be easily solved, but I’m stuck and do not have enough experience in VBA.

    You will find below the corresponding line of code (part of a bigger one) that I wrote and presents a problem:

    With ThisWorkbook
    'Repeat action to add sheet based on number of filled cells in student column
    For i = 10 To LastCell
    If Cells(i, 10).Value = "Children" Then
    'Code to add children sheet with name of student
    Worksheets("Exam Sheet Child").Visible = xlSheetVisible
    Newname = ThisWorkbook.Worksheets("Grading exam - Participants").Cells(i, 6).Value 'find the student name to add to sheet title
    Worksheets("Exam Sheet Child ").Copy after:=.Sheets(Sheets.Count) 'Code to copy template from sheet " Exam Sheet Child " at the end of the workbook
    ActiveSheet.Name = Newname
    Worksheets("Exam Sheet Child ").Visible = xlSheetVeryHidden
    Else
    'Code to add adult sheet with name of student
    Worksheets("Exam Sheet Adult").Visible = xlSheetVisible
    Newname = ThisWorkbook.Worksheets("Grading exam - Participants").Cells(i, 6).Value 'find the student name to add to sheet title
    Worksheets("Exam Sheet Adult").Copy after:=.Sheets(Sheets.Count) 'Code to copy template from sheet " Exam Sheet Adult" at the end of the workbook
    ActiveSheet.Name = Newname
    Worksheets("Exam Sheet Adult").Visible = xlSheetVeryHidden
    End If
    Next
    End With

    Thank you for your help,
    N.

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Sheet duplicated based on specific criteria using VBA

    Hi nicotje & welcome to the forum

    Once you create a new sheet, it is normally activated so when you check "If Cells(i, 10).Value = "Children" Then" it is referring to the newly added sheet. Try to add the sheet name to your cell reference like below ...
    Please Login or Register  to view this content.
    PS: Please edit your post, select the VBA code then click on # from the menu to add code tags for you
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Registered User
    Join Date
    04-19-2021
    Location
    Canada
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Re: Sheet duplicated based on specific criteria using VBA

    Hi nankw83,

    Thank you very much for your advise. It works perfectly now!!

    N.

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Sheet duplicated based on specific criteria using VBA

    Glad to help & thanks for the Rep+

    If that takes care of your question, please take a moment & mark this thread as [SOLVED] from the Thread Tools above your first post.

  5. #5
    Registered User
    Join Date
    04-19-2021
    Location
    Canada
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Re: Sheet duplicated based on specific criteria using VBA

    Understood.

    Sorry, still new to this and not accustomed to the all functioning. I will change the thread to [SOLVED].

    Thanks again,
    N.

+ 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. Replies: 3
    Last Post: 01-31-2019, 08:00 PM
  2. Replies: 11
    Last Post: 07-04-2014, 07:10 AM
  3. [SOLVED] delete duplicated rows based on a criteria in another column
    By melody10 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2013, 06:53 AM
  4. [Solved] Copying Cells from one sheet to another based on specific criteria
    By james.heston in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-30-2013, 09:51 AM
  5. [SOLVED] Extract duplicated values based on an extra criteria
    By Eduard in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-21-2013, 05:50 AM
  6. HELP! Macro to Copy specific cells from one sheet to another based on specific criteria
    By atriscritti in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-11-2012, 11:05 AM
  7. [SOLVED] Copy and paste data from sheet 2 to sheet 1 based on specific criteria on sheet 1
    By VBADUD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2012, 04:18 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