+ Reply to Thread
Results 1 to 20 of 20

Looks at 2 fields and If one doesnt contain certain characters then set to 0.

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    I have the following in Excel

    Row I K
    6 Job_Family_desc NewGrade
    7 TAL – Test1 8
    8 TAL – Test1 8
    9 TAL – Test1 8

    For TAL you can only have a grade of 5 to 9
    If someone enters 1 against NewGrade, that’s incorrect.

    At the end of the process the uses clicks a macro button and then
    Go to K7.
    If I7 LIKE “TAL*” AND K7 IN (0,5,6,7,8,9) THEN Next
    ELSE SET K7 = 0
    IF I7 LIKE “BAL*” AND K7 IN (0,1,2,3,4,5,6,7,8,9) THEN Next
    ELSE SET K7 = 0
    IF I7 LIKE “SAL*” AND K7 IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) THEN Next
    ELSE SET K7 = 0
    NEXT got to K8

    Any help would be greatly appreciated

    Debbie

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    it's not clear for me, attach a sample file with data and desired result
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Ive tried to add an example in a spreadsheet
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Where are data ? where result ? explain better please

  5. #5
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    This was my original plan and one Ive been working on (Based on rel data and not the example)

    
    Sub ProtectWorksheet_Initiated_By_School()
    '
    
    'Go back to sheet 1 from the instructions sheet
        Sheets(1).Select 'This selects sheet 1 no matter what it is called
    
    'Brand new section. we dont want them to protect if they havent done everything neccessary
    
        Dim i As Long, lrow As Long
    
    With Worksheets(1)
        lrow = .Range("H" & .Rows.Count).End(xlUp).Row 'H is the row of JobFamily    
        'For To Clause. When the counter i is 7 (The first actual value. 
        'to the number of the row we are on.
        'The final value of the of the i counter in the above loop will be after 1row.
        ' i starts on row 7 and finishes on the end row
        
        For i = 7 To lrow ' + 1
        'If job title is not null and New Grade is 0 then
                If .Range("H" & i).Value <> "" And .Range("K" & i).Value = "0" Then
                MsgBox ("Please complete all the new posts")
                Exit Sub
                
    
        'And now we have to do the last checks to make sure that the New Grade is correct for the Job Family Desc
        'This is an issue if the New grade is changed BEFORE the job family description is changed.
             ElseIf .Range("I" & i).Value Like "BPS*" And UBound(Filter(Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("BPS has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "CAT*" And UBound(Filter(Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("CAT has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "CAM*" And UBound(Filter(Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("CAM has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "CAH*" And UBound(Filter(Array("6", "7", "8", "9", "10", "11", "12", "13", "14"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("CAH has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "SCI*" And UBound(Filter(Array("4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("SCI has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "SPD*" And UBound(Filter(Array("8", "9", "10", "11", "12"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("SPD has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "SUS*" And UBound(Filter(Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("SUS has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "TAL*" And UBound(Filter(Array("5", "6", "7", "8", "9"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("TAL has an incorrect grade. Please amend and continue")
            Exit Sub
            ElseIf .Range("I" & i).Value Like "TRA*" And UBound(Filter(Array("6", "7", "8", "9", "10", "11", "12", "13", "14"), .Range("J" & i).Value)) < 0 Then
            MsgBox ("TRA has an incorrect grade. Please amend and continue")
            Exit Sub
    The bit in the middle isn the bit that doest work

    'And now we have to do the last checks to make sure that the New Grade is correct for the Job Family Desc


    If i is Like BPS and K is NOT in 1,2,3,4,5,6,7,8,9,10,11,12,13,14 then

    Message box 'BPS is incorrect' and end the IF statement
    If i is Like CAT and K is NOT in 1,2,3,4,5,6,7,8,9,10 then
    Message box 'CAT is incorrect' and end the statement

    Repeat through all the logic

    go through all the rows until there is no more data.

  6. #6
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    I cant explain it any better than I have.

    I cant provide the result.

    The example data is in columns I and K as specified.

    The basics are. How do I write

    Go to K7.
    If I7 LIKE “TAL*” AND K7 IN (0,5,6,7,8,9) THEN Next
    ELSE SET K7 = 0
    IF I7 LIKE “BAL*” AND K7 IN (0,1,2,3,4,5,6,7,8,9) THEN Next
    ELSE SET K7 = 0
    IF I7 LIKE “SAL*” AND K7 IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) THEN Next
    ELSE SET K7 = 0
    NEXT got to K8

  7. #7
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    sub macro()
    Range("K7").select
    if not strings.left(Range("I7"),3) = "TAL" then
       if not Range("K7") = 0 or not Range("K7") = 5 or not Range("K7") = 6 then
          Range("K7") = 0
       end if
    end if
    end sub

  8. #8
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Looking good!!

    Ill see if I can do something with that

    Thank you

  9. #9
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Ive not been able to implement it. I dont understand the first IF NOT because Im wanting

    IF its TAL but not 0,5,6,7,8,9 then SET to 0

    Ive tried doing the following

    Sub macroTesting_NewGrade()
    Range("K7").Select
    If Strings.Left(Range("I7"), 3) = "TAL" And Not Range("K7") = 0 Or Not Range("K7") = 5 Or Not Range("K7") = 6 Or Not Range("K7") = 7 Or Not Range("K7") = 8 Or Not Range("K7") = 9 Then
     Range("K7") = 0
       End If
    End If
    End Sub
    But Im getting an error

    End If without block IF

    Im not used to VBA cod I work in SQL Mainly so Im really struggling with this.

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    yeah you are right, you have end if written twice, you need:
    Sub macroTesting_NewGrade()
    Range("K7").Select
    If Strings.Left(Range("I7"), 3) = "TAL" And Not Range("K7") = 0 Or Not Range("K7") = 5 Or Not Range("K7") = 6 Or Not Range("K7") = 7 Or Not Range("K7") = 8 Or Not Range("K7") = 9 Then
     Range("K7") = 0
    End If
    End Sub
    I'm not sure if that will work though, I don't think it will interpret the and and or's as you want it to. To be on the safe side I would put the two statements on different lines:

    Sub macroTesting_NewGrade()
    Range("K7").Select
    If Strings.Left(Range("I7"), 3) = "TAL" then
    if Not Range("K7") = 0 Or Not Range("K7") = 5 Or Not Range("K7") = 6 Or Not Range("K7") = 7 Or Not Range("K7") = 8 Or Not Range("K7") = 9 Then
     Range("K7") = 0
    End If
    end if
    End Sub

  11. #11
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Ive got this together

    
      For i = 7 To lrow
        'If Job_Family_Desc = AA use the AA range as a validation list
                If .Range("I" & i).Value Like "BPS*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
    
                ElseIf .Range("I" & i).Value Like "CAH*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K7") = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAM*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Then Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAT*" And Not Range("K7") = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K7") = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Then
                Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SCI*" And Not Range("K" & i) = 0 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SUS*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SPD*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Then
                Range("K" & i) = 0
    
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TAL*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TRA*" And Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                       
        'If Job_Family_Description Is empty then the file is done so we can come out of this IF THEN ELSE Condition
                ElseIf .Range("I" & i).Value = "" Then
                
            End If
        Next i
    End With
    but it doesnt actually work properly.

    I have 3 rows all correct

    TAL 8
    TAL 8
    TAL 8

    It looks at the firt bit of the code the BPS and sets 8 to 0. Thn it starts again and does the sae for all 3 values.

    I was expecting it to eep going until it found the TAL row, then finding it was correct to go onto the next row.

    Iv done these kind of loops before so I dont quite kno whats gone wrong

  12. #12
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    I don't think you can use the and/or commands in an if statement like that. You need two separate statements, one for the TAL part and one for the numbers:
    Sub macroTesting_NewGrade()
    Range("K7").Select
    If Strings.Left(Range("I7"), 3) = "TAL" then
    if Not Range("K7") = 0 Or Not Range("K7") = 5 Or Not Range("K7") = 6 Or Not Range("K7") = 7 Or Not Range("K7") = 8 Or Not Range("K7") = 9 Then
     Range("K7") = 0
    End If
    end if
    End Sub
    Note - I haven't been through all the code, I don't think you posted it all so it doesn't compile as it is in your post. This would be the first thing to try but there might be other problems..

  13. #13
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Unfortunately is doesnt seem to like that, but the only line in red is the second like. The If NOT after the IF. everything else seems happy
    For i = 7 To lrow
        'If Job_Family_Desc = AA use the AA range as a validation list
                If .Range("I" & i).Value Like "BPS*" Then
                If Not Range("K" & i) = 0 Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
    
                ElseIf .Range("I" & i).Value Like "CAH*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K7") = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAM*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Then Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAT*" Then
                If Not Range("K7") = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K7") = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Then
                Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SCI*" Then
                If Not Range("K" & i) = 0 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SUS*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SPD*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Then
                Range("K" & i) = 0
    
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TAL*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TRA*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                       
        'If Job_Family_Description Is empty then the file is done so we can come out of this IF THEN ELSE Condition
                ElseIf .Range("I" & i).Value = "" Then
                
            End If
            End If
        Next i
    End With

  14. #14
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    I can see a couple of issues with the code posted:

    -The line in red is missing "or not" between "Range("K" & i) = 0" and "Range("K" & i) = 1".
    -Your end if statements do not match up to the if statements correctly, your code should have this format:
    for a = 1 to lrow
       if Range("I" & i).value like "BPS*" then
          if not Range("K" & i) = 0 or not Range("K"& i) =1 or not Range("K" & i) = 2 then 
             'do this code
          end if
       elseif Range("I" & i).value like "CAH*" then
          'and so on
       end if
    next

  15. #15
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Whops you were right about the or not bit. Ive corrected that. I cant quite see the other issue your taking about based on the small example compared to the one Ive got

    Ive currently got an error at the end on Next i (Next without for is the error)

    Is it possible to show where I have gone wrong based on the example below? Im basically, in a field and checking one field against the other for each group. When Ive either set it to 0 or found no problems I continue to the next field until I have no rows left

    
    For i = 7 To lrow
        'If Job_Family_Desc = AA use the AA range as a validation list
                If .Range("I" & i).Value Like "BPS*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
    
                ElseIf .Range("I" & i).Value Like "CAH*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K7") = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAM*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Then Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "CAT*" Then
                If Not Range("K7") = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K7") = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Then
                Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SCI*" Then
                If Not Range("K" & i) = 0 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SUS*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "SPD*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Then
                Range("K" & i) = 0
    
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TAL*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Then
                Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                ElseIf .Range("I" & i).Value Like "TRA*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                       
        'If Job_Family_Description Is empty then the file is done so we can come out of this IF THEN ELSE Condition
                ElseIf .Range("I" & i).Value = "" Then
                
            End If
            End If
        Next i 'Next without for
    End With

  16. #16
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    you need to change the if statements in your code to the format I posted. You don't have the same number of "end ifs" as you do "ifs"

    Option Explicit
    
    Sub macro1()
    Dim i, lrow
    lrow = 14 'not sure what this should be
    With Sheets(1) 'not sure what this should be.
        For i = 7 To lrow
            If .Range("I" & i).Value Like "BPS*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                            'And when you have done it you want to go down to the next row
                            Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAH*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K7") = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                            'And when you have done it you want to go down to the next row
                            Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAM*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Then
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAT*" Then
                If Not Range("K7") = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K7") = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Then
                    Range("K" & i) = 0
                     'And when you have done it you want to go down to the next row
                     Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SCI*" Then
                If Not Range("K" & i) = 0 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SUS*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SPD*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "TAL*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "TRA*" Then
                If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                     Range("K" & i) = 0
                     'And when you have done it you want to go down to the next row
                     Selection.Offset(1, 0).Select
                End If
            End If
        Next i
    End With
    
    End Sub

  17. #17
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Ah ha, so I needed to end if all the new If Nots! Sorted. So its working again but still not doing what I want. It does get further than last time though. Last time it would only go to the first IF eery time)

    My column i are all TAL. and My column k are all 8 (3 rows)

    It continues till it gets to TAL, but then it changes the field to 0 even though 8 is valid. So it still looks like its resetting everything back to 0 even what the grades are valid. Hmmmmm. So close through

    Dim i As Long, lrow As Long
    
        With Worksheets(1)
        'H Is the job family description
        lrow = .Range("K" & .Rows.Count).End(xlUp).Row
        
        'For To Clause. When the counter i is at 2 (The first actual value. Remember the first few rows are taken up with the button
        'to the number of the row we are on.
        'The final value of the of the i counter in the above loop will be after 1row.
        ' i starts on row 8 and the 1row
    
        
        For i = 7 To lrow
        'If Job_Family_Desc = AA use the AA range as a validation list
                If .Range("I" & i).Value Like "BPS*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                    End If
                    
                ElseIf .Range("I" & i).Value Like "CAH*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K7") = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                     Range("K" & i) = 0
                        
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                   End If
                   
                ElseIf .Range("I" & i).Value Like "CAM*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Then
                    Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                   End If
                   
                ElseIf .Range("I" & i).Value Like "CAT*" Then
                    If Not Range("K7") = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K7") = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Then
                    Range("K" & i) = 0
    
                   
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                   End If
                   
                ElseIf .Range("I" & i).Value Like "SCI*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                     End If
                ElseIf .Range("I" & i).Value Like "SUS*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 1 Or Not Range("K" & i) = 2 Or Not Range("K" & i) = 3 Or Not Range("K7") = 4 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                   End If
                        
                ElseIf .Range("I" & i).Value Like "SPD*" Then
                     If Not Range("K" & i) = 0 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Then
                    Range("K" & i) = 0
    
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                    End If
                    
                ElseIf .Range("I" & i).Value Like "TAL*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 5 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Then
                    Range("K" & i) = 0
                
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                        
                    End If
                       
                ElseIf .Range("I" & i).Value Like "TRA*" Then
                    If Not Range("K" & i) = 0 Or Not Range("K" & i) = 6 Or Not Range("K" & i) = 7 Or Not Range("K" & i) = 8 Or Not Range("K" & i) = 9 Or Not Range("K" & i) = 10 Or Not Range("K" & i) = 11 Or Not Range("K" & i) = 12 Or Not Range("K" & i) = 13 Or Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                    
                     End If
                       
        'If Job_Family_Description Is empty then the file is done so we can come out of this IF THEN ELSE Condition
                ElseIf .Range("I" & i).Value = "" Then
                
            End If
            'End If
        Next i 'Next without for
    End With

  18. #18
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    How about this?
    Option Explicit
    
    Sub macro1()
    Dim i, lrow
    lrow = 14 'not sure what this should be
    With Sheets(1) 'not sure what this should be.
        Fand i = 7 To lrow
            If .Range("I" & i).Value Like "BPS*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 1 And Not Range("K" & i) = 2 And Not Range("K" & i) = 3 And Not Range("K7") = 4 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 And Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                            'And when you have done it you want to go down to the next row
                            Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAH*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K7") = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 And Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                            'And when you have done it you want to go down to the next row
                            Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAM*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 1 And Not Range("K" & i) = 2 And Not Range("K" & i) = 3 And Not Range("K7") = 4 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 Then
                        'And when you have done it you want to go down to the next row
                        Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "CAT*" Then
                If Not Range("K7") = 0 And Not Range("K" & i) = 1 And Not Range("K" & i) = 2 And Not Range("K7") = 3 And Not Range("K7") = 4 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 Then
                    Range("K" & i) = 0
                     'And when you have done it you want to go down to the next row
                     Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SCI*" Then
                If Not Range("K" & i) = 0 And Not Range("K7") = 4 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 And Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SUS*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 1 And Not Range("K" & i) = 2 And Not Range("K" & i) = 3 And Not Range("K7") = 4 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 And Not Range("K" & i) = 14 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "SPD*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "TAL*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 5 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 Then
                    Range("K" & i) = 0
                    'And when you have done it you want to go down to the next row
                    Selection.Offset(1, 0).Select
                End If
            ElseIf .Range("I" & i).Value Like "TRA*" Then
                If Not Range("K" & i) = 0 And Not Range("K" & i) = 6 And Not Range("K" & i) = 7 And Not Range("K" & i) = 8 And Not Range("K" & i) = 9 And Not Range("K" & i) = 10 And Not Range("K" & i) = 11 And Not Range("K" & i) = 12 And Not Range("K" & i) = 13 And Not Range("K" & i) = 14 Then
                     Range("K" & i) = 0
                     'And when you have done it you want to go down to the next row
                     Selection.Offset(1, 0).Select
                End If
            End If
        Next i
    End With
    
    End Sub

  19. #19
    Registered User
    Join Date
    03-08-2013
    Location
    Derbyshire
    MS-Off Ver
    Excel 2003
    Posts
    46

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    That it!!!

    It now working. Brilliant. Now I need to figure out the best place to put it. Ive got quite bit of logic to check issues in the script already. Its whether to do it on the event of column i changing, or whether to do it right at the end when the user is finalising the spreadsheet.

    Ill hve a think about that. Thak you again!!!!

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,593

    Re: Looks at 2 fields and If one doesnt contain certain characters then set to 0.

    Try this
    Sub test()
        Dim r As Range
        For Each r In Range("i7", Range("i" & Rows.Count).End(xlUp))
            Select Case True
                Case r.Value Like "TAL*", r.Value Like "BAL*"
                    If r(, 3).Value Like "#" Then r(, 3).Value = 0
                Case r.Value Like "SAL*"
                    If (r(, 3).Value Like "#") + (r(, 3).Value Like "1[0-4]") Then
                        r(, 3).Value = 0
                    End If
            End Select
        Next
    End Sub
    Last edited by jindon; 05-29-2013 at 07:51 AM. Reason: Re-read your conditions, so changed

+ 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