VBA Find Function

Whylucky

New Member
Joined
Sep 24, 2013
Messages
39
So I am trying to code a macro that will search through a selected range of cells for key letters, for instance this cell may contain any combination of B, C, Te, Tc, RH, or LH. I would preferably like to search with capitalization being a factor but it is not a deal breaker. Below is a sample of what i have if the cell has a B, C it works for B but ignores the C i need it t o recognize both, any help or suggestions?

Code:
 If InStr(1, ActiveCell.Text, "B") Then        Range("O" + CStr(ActiveCell.Row)).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
        
    If InStr(ActiveCell.Text, "C") Then
        Range("P" + CStr(ActiveCell.Row)).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
When you find B you change the active cell, so when the code is looking for C it isn't looking in the same cell as it was for C.

Try this.
Code:
 If InStr(1, ActiveCell.Text, "B") Then 
        With Range("O" & ActiveCell.Row).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
        
    If InStr(ActiveCell.Text, "C") Then
        With Range("P" & ActiveCell.Row)).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
 
Upvote 0
I have taken the end ifs away as you have already closed the if statements maybe this is where you was going wrong


Code:
If InStr(1, ActiveCell.Text, "B") Then        Range("O" + CStr(ActiveCell.Row)).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        
    If InStr(ActiveCell.Text, "C") Then Range("P" + CStr(ActiveCell.Row)).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
 
Upvote 0
Is there anyway that you know of, once the cells have been highlighted to find the maximum of them? i know this could be a new thread but it is a direct relation to the above code
 
Upvote 0
The maximum of what exactly?
 
Upvote 0
so the code you helped fix earlier (thank you by the way) is going through and highlighting cells on a case by case basis, i then need to find maximum of the highlighted cells (because that for certain case)
 
Upvote 0
Perhaps something like this.

Code:
    If InStr(1, ActiveCell.Text, "B") Then 
        With Range("O" & ActiveCell.Row).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
            
        End With

        If MaxValue = 0 Or Range("O" & ActiveCell.Row)>MaxValue Then
            MaxValue = Range("O" & ActiveCell.Row).Value
        End If

    End If
        
    If InStr(ActiveCell.Text, "C") Then
        With Range("P" & ActiveCell.Row)).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        If MaxValue = 0 Or Range("P" & ActiveCell.Row)>MaxValue Then
            MaxValue = Range("P" & ActiveCell.Row).Value
        End If

    End If
 
Upvote 0

Forum statistics

Threads
1,216,131
Messages
6,129,066
Members
449,485
Latest member
greggy

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