+ Reply to Thread
Results 1 to 6 of 6

adapt code to various criteria column

Hybrid View

marreco adapt code to various... 01-05-2013, 01:48 PM
jaslake Re: adapt code to various... 01-05-2013, 02:16 PM
marreco Re: adapt code to various... 01-05-2013, 02:30 PM
jaslake Re: adapt code to various... 01-05-2013, 02:37 PM
marreco Re: adapt code to various... 01-05-2013, 02:47 PM
jaslake Re: adapt code to various... 01-05-2013, 02:51 PM
  1. #1
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    adapt code to various criteria column

    Hi.

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)If Target.Column = 2 Then
    Call I_wish_it_was_only_in_Worksheet_Change_
    Else
    Exit Sub
    End If
    End Sub
    Hi.
    could someone help me adapt the code so that column "B", and "Crash," consider "Near-miss" and "Non-Compliance", like the "OR" of Excel, and the result happens in columns "T", "U", "V" and "W"?


    tried the code below but is not working.
    Sub I_wish_it_was_only_in_Worksheet_Change_()
    
    
    Dim i As Long, n As Long
    n = Range("B65536").End(xlUp).Row
    For i = 2 To n
    If Range("B" & i).Value = "" Then
    Range("T:T, U:U, V:V, W:W" & i).Value = ""
    ElseIf Range("B" & i).Value = "Crash" Or "Near-miss" Or "Non-Compliance" Then
    Range("T:T, U:U, V:V, W:W" & i).Value = ""
    Else
    Range("T:T, U:U, V:V, W:W" & i).Value = "NA"
    End If
    Next i
    End Sub
    Cross-Post
    http://www.mrexcel.com/forum/excel-q...ra-result.html
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: adapt code to various criteria column

    Hi marreco

    Try this
    Option Explicit
    
    Sub I_wish_it_was_only_in_Worksheet_Change_()
        Dim i As Long, n As Long
        Dim myCells As Range
    
        n = Range("B65536").End(xlUp).Row
        For i = 2 To n
            If Range("B" & i).Value = "" Or Range("B" & i).Value = "Crash" _
                    Or Range("B" & i).Value = "Near-miss" Or Range("B" & i).Value = "Non-Compliange" Then
                Set myCells = Union(Range("T" & i), Range("U" & i), Range("V" & i), Range("W" & i))
                Application.EnableEvents = False
                myCells.Value = ""
                Application.EnableEvents = True
            Else
                Set myCells = Union(Range("T" & i), Range("U" & i), Range("V" & i), Range("W" & i))
                Application.EnableEvents = False
                myCells.Value = "N/A"
                Application.EnableEvents = True
            End If
        Next i
    End Sub
    or this
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
        Dim n As Long
        Dim i As Long
        Dim myCells As Range
        n = Range("B65536").End(xlUp).Row
        If Not Intersect(Target, Range("B2:B" & n)) Is Nothing Then
            i = Target.Row
            If Target.Value = "" Or Target.Value = "Crash" _
                    Or Target.Value = "Near-miss" Or Target.Value = "Non-Compliange" Then
                Set myCells = Union(Range("T" & i), Range("U" & i), Range("V" & i), Range("W" & i))
                Application.EnableEvents = False
                myCells.Value = ""
                Application.EnableEvents = True
            Else
                Set myCells = Union(Range("T" & i), Range("U" & i), Range("V" & i), Range("W" & i))
                Application.EnableEvents = False
                myCells.Value = "N/A"
                Application.EnableEvents = True
            End If
    
            Exit Sub
        End If
    End Sub
    Last edited by jaslake; 01-05-2013 at 02:26 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: adapt code to various criteria column

    Hi.

    I'm trying to run the code but nothing happens!

    Thank you!!

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: adapt code to various criteria column

    Hi marreco

    The code is in the attached and appears to work for me.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: adapt code to various criteria column

    Hi.
    It was great!

    Very many thanks!

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: adapt code to various criteria column

    You're welcome...glad I could help. Thanks for the Rep.

+ 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