Returning a value in a cell under specific conditions

chadski778

Active Member
Joined
Mar 14, 2010
Messages
297
I have a selection of data in four columns (F-I). I would like to return a value in column J (text - "SDS sec 3") if the value in column H contains an "R" AND the value in column F is >=1 or if the value in column H contains "43" AND the value in column F >=0.1. I would also like to format the entire row (4 columns) if the conditions are met. See example below.

Excel Workbook
ABCD
2BEFORE
3
4
5Actual%Generic NameEC HazardCAS Number
674.92Highly refined mineral oil (IP346N/CVarious
774.92Highly refined mineral oil (IP346N/C64742-62-7
874.92Highly refined mineral oil (IP346N/C64742-65-0
910Highly refined mineral oil (IP346N/C64742-54-7 or 92062-09-4
105.852Highly refined mineral oil (IP346N/C64742-65-0
113.99Polyolefin polyamine succinimide polyolR53Polymer
121.064Calcium alkaryl sulphonateR53Not available
131.064Zinc dialkyldithiophosphateR41-51/5368649-42-3
141.06Olefin co-polymerN/CConfidential
150.99Highly refined mineral oil (IP346N/CNot available
160.99PolyalkylmethacrylateN/CNot available
170.931Calcium alkaryl sulphonateR43-53Not available
180.399Borated polyolefin polyamine succinimideR53Not available
190.133Polyolefin polyamine succinimide molybdenum cmplxN/CPolymer
200.0532PolyalkarylamineR51/5368411-46-1
210.0266Branched alkanesR38-65-51/53Polymer
220.01Alkyl methacrylate (as impurity)R36/37/38-50/53142-90-5
Declarations


Excel Workbook
ABCDE
25AFTER
26
27Actual%Generic NameEC HazardCAS Number
2874.92Highly refined mineral oil (IP346N/CVarious
2974.92Highly refined mineral oil (IP346N/C64742-62-7
3074.92Highly refined mineral oil (IP346N/C64742-65-0
3110Highly refined mineral oil (IP346N/C64742-54-7 or 92062-09-4
325.852Highly refined mineral oil (IP346N/C64742-65-0
333.99Polyolefin polyamine succinimide polyolR53PolymerSDS sec 3
341.064Calcium alkaryl sulphonateR53Not availableSDS sec 3
351.064Zinc dialkyldithiophosphateR41-51/5368649-42-3SDS sec 3
361.06Olefin co-polymerN/CConfidential
370.99Highly refined mineral oil (IP346N/CNot available
380.99PolyalkylmethacrylateN/CNot available
390.931Calcium alkaryl sulphonateR43-53Not availableSDS sec 3
400.399Borated polyolefin polyamine succinimideR53Not available
410.133Polyolefin polyamine succinimide molybdenum cmplxN/CPolymer
420.0532PolyalkarylamineR51/5368411-46-1
430.0266Branched alkanesR38-65-51/53Polymer
440.01Alkyl methacrylate (as impurity)R36/37/38-50/53142-90-5
Declarations


Thanks

Chad
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

Maybe someting like

Code:
Sub b_test()

fr = Cells(Rows.Count, 6).End(xlUp).Row

For i = fr To 1 Step -1
    On Error Resume Next
    a = Application.WorksheetFunction.Search("r", Range("h" & i))
    On Error GoTo 0
    If a > 0 And Range("f" & i) > 1 Then
        Rows(i).Interior.Color = 255
        Range("J" & i) = "SDS sec 3"
    End If
    a = 0
Next i


For i = fr To 1 Step -1
    On Error Resume Next
    a = Application.WorksheetFunction.Search("43", Range("h" & i))
    On Error GoTo 0
    If a > 0 And Range("f" & i) > 0.1 Then
        Rows(i).Interior.Color = 255
        Range("J" & i) = "SDS sec 3"
    End If
    a = 0
Next i

End Sub
 
Upvote 0
An alternative with formulas

Try this in J5 (assuming headers in F4:I4)

=IF(OR(AND(F5>=1,ISNUMBER(SEARCH("R",H5))),AND(F5>=0.1,ISNUMBER(SEARCH(43,H5)))),"SDS sec 3","")

copy down

To highlight you can use:

Select F5:J21
Home > Conditional Format > New Rule > Use a formula to...

insert this formula
=$J5="SDS sec 3"

Format button
Fill --> pick a color
ok, ok

HTH

M.
 
Upvote 0
Sub b_test() works well but it has highlighted the "r" in EC Hazard (title of data column). Could you modify by making the condition "r" and "integer" in the same string?
Also would it be possible to format just the 5 columns and not the entire row?

Thanks
 
Upvote 0
maybe
Code:
Sub b_1_test()
fr = Cells(Rows.Count, 6).End(xlUp).Row

For i = fr To 5 Step -1
' 5 if headlines in row 4
    On Error Resume Next
    a = Application.WorksheetFunction.Search("r", Range("h" & i))
    b = Application.WorksheetFunction.Search("43", Range("h" & i))
    On Error GoTo 0
    If (a > 0 And Range("f" & i) > 1) Or (b > 0 And Range("f" & i) > 0.1) Then
        Range("f" & i).Resize(1, 5).Interior.ColorIndex = 36
        Range("J" & i) = "SDS sec 3"
    End If
    a = 0
    b = 0
Next i

End Sub
 
Upvote 0
Is there a way you could use the 'find' function to make the macro only treat data below the headings "%Actual", "Generic Name", "EC Hazard" and "CAS Number"?
 
Upvote 0
Is there a way you could use the 'find' function to make the macro only treat data below the headings "%Actual", "Generic Name", "EC Hazard" and "CAS Number"?

Hi chadski,

If you prefer a macro ok, but let me ask: have you tried my formulas in #3?

M.
 
Upvote 0
try this
Code:
Sub b_2_test()
fr = Cells(Rows.Count, 6).End(xlUp).Row

For x = fr To 1 Step -1
    If Range("f" & x) = "Actual%" Then y = x + 1
Next x
    

For i = fr To y Step -1

    On Error Resume Next
    a = Application.WorksheetFunction.Search("r", Range("h" & i))
    b = Application.WorksheetFunction.Search("43", Range("h" & i))
    On Error GoTo 0
    If (a > 0 And Range("f" & i) > 1) Or (b > 0 And Range("f" & i) > 0.1) Then
        Range("f" & i).Resize(1, 5).Interior.ColorIndex = 36
        Range("J" & i) = "SDS sec 3"
    End If
    a = 0
    b = 0
Next i

End Sub
 
Upvote 0
Marcelo, hi i need a macro because i want to use it in another bigger macro. Thanks for your help. It is almost there!
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top