+ Reply to Thread
Results 1 to 12 of 12

Repeat code in the same sheet for a different list

  1. #1
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Repeat code in the same sheet for a different list

    Column D, Row 8 - 13 = list of yes, no, and N/A
    Cell J8 = 3 seperate judgments: Pass, Fail, and one or more items needs resolved.

    My question is how do I repeat the code in the same sheet for another list of yes, no, and N/A

    Private Sub Worksheet_Change(ByVal Target As Range)

    '********************************************************
    ' Update the Range Below to "Look for updates", in those
    ' cells. The Check will be run each time a cell in that
    ' range is updated.
    '********************************************************

    If Not Intersect(Target, Range("D8:D13")) Is Nothing Then
    Call DetermineStatus
    End If
    End Sub


    Sub DetermineStatus()

    Dim rng As Range
    Dim cell As Range
    Dim OutputCell As Range

    Dim Pass As Boolean
    Dim Fail As Boolean
    Dim Progress As Boolean

    '*********************************************
    ' Set the range to Look for Yes and No here:

    Set rng = Range("D8:D13")
    '***************^^^^^^^************************

    '**********************************************
    ' Set the cell to output the Status here:

    Set OutputCell = Range("j8")
    '**********************^^^^^*******************

    Pass = False
    Fail = False
    Progress = False

    For Each cell In rng

    If cell.Value = "Yes" Or cell.Value = "yes" Then

    If Fail Then
    Progress = True
    End If

    Pass = True
    End If

    If cell.Value = "No" Or cell.Value = "no" Then

    If Pass Then
    Progress = True
    End If

    Fail = True
    End If

    Next cell

    If Progress Then
    'Set the output text for mixed Yes and No answers here:
    OutputCell.Value = "One or more items still need attention"

    ElseIf Pass Then
    'Set the output text for a all Yes here:
    OutputCell.Value = "Pass"

    ElseIf Fail Then
    'Set the output text for all No here:
    OutputCell.Value = "Fail"

    End If

    End Sub

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Repeat code in the same sheet for a different list

    Change your code to accept two passed ranges, and just change what ranges you pass the code

    Please Login or Register  to view this content.
    But your code would be better like so:

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 04-01-2015 at 02:19 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    Great! Thank you!

  4. #4
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    Another quick question:

    I want to change the judgment to a symbol.

    The legend is as follows:

    Cell G128 is Judgment "●" Meaning "Pass"
    Cell G129 is Judgment "X" Meaning "Fail"
    Cell G130 is Judgment "▲" Meaning "One or more items still need attention"

    This is current: OutputCell.Value = "Pass"
    Needs to be: OutputCell.Value = Cell(G128)

  5. #5
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Repeat code in the same sheet for a different list

    Change your sub to this:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    The first sub you sent me worked great!

    Sub DetermineStatus(rng As Range, OutputCell As Range)
    Dim strOut As String

    strOut = "One or more items still need attention"


    If Application.CountIf(rng, "Yes") = 0 Then 'Case insensitive
    strOut = "Fail"
    ElseIf Application.CountIf(rng, "No") = 0 Then
    strOut = "Pass"
    End If

    Application.EnableEvents = False
    OutputCell.Value = strOut
    Application.EnableEvents = True
    End Sub

    This sub however, errors out. Not sure how to fix it.

    Sub DetermineStatus(rng As Range, OutputCell As Range)
    Dim strOut As String

    If Application.CountIf(rng, "Yes") = 0 Then 'Case insensitive
    Range("G128").Copy OutputCell
    ElseIf Application.CountIf(rng, "No") = 0 Then
    Range("G130").Copy OutputCell
    Else
    Range("G129").Copy OutputCell
    End If

    Application.EnableEvents = True
    End Sub

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Repeat code in the same sheet for a different list

    Is your sheet protected?

  8. #8
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    No it is not protected at all.

  9. #9
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Repeat code in the same sheet for a different list

    Could you post a version of your workbook, sanitized if needed?

  10. #10
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    Sample 1 is the first Sub you posted.

    I have replaced "Fail" with the character code ChrW(9679) and "Pass" with an "X". I am still searching for the Character code for triangle.... I see it is 0070 in the character map but doesn't translate to VBA.

    Sample 2 is the second Sub that errors out.
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,942

    Re: Repeat code in the same sheet for a different list

    Merged cells require different handling:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    08-04-2010
    Location
    Columbus, OH
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Repeat code in the same sheet for a different list

    Got ya.

    Well this worked like a charm.

    Thank you so much for your time!!

+ 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] code populates list boxes in active sheet, but does not populate boxes in subsequent sheet
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-13-2014, 10:26 AM
  2. [SOLVED] List of Codes on one sheet, Billing data on another sheet, highlight code if in list
    By children in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-18-2013, 05:03 PM
  3. How to repeat vba code a number of times depending on items in an excel list
    By qaliq in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2013, 08:13 AM
  4. Code to write formula in 2 cells, copy, paste values, repeat next row until end of sheet
    By Oly Steel Man in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-14-2013, 11:13 AM
  5. Create new sheet from list of customers code
    By pauldaddyadams in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-13-2011, 09:29 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