Cell Values

Jenawade

Board Regular
Joined
Apr 8, 2002
Messages
231
The macro below selects a range, searches for 130620, 130618, 133362,130604, and if found, changes any instance of "BLUE" or "ORANGE" to "BLACK;" however, the user noticed that if the cell that contains 1 of those 4 numbers contains ONLY that number, "BLUE" or "ORANGE" are not updated - but if the cell contains any text or numbers along with 1 of those 4 numbers, it works. Can anyone help me tweak this so it will work when any of those 4 numbers are within the range, whether alone or as part of a string?

Code:
Sub UpdateLabels()

Dim fn As Variant, f As Integer

    fn = Application.GetOpenFilename("Excel-files,*.xls", _
        1, "Select One Or More Files To Open", , True)
    If TypeName(fn) = "Boolean" Then Exit Sub
    For f = 1 To UBound(fn)
        Debug.Print "Selected file #" & f & ": " & fn(f)
        Workbooks.Open fn(f)

               On Error Resume Next
               
               With Range("A40:Z90")
               
               r = .find(130620, LookIn:=xlValues)
               s = .find(130618, LookIn:=xlValues)
               t = .find(133362, LookIn:=xlValues)
               u = .find(130604, LookIn:=xlValues)

               If Not r = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               End With

       ActiveWorkbook.Close savechanges:=True

   Next f

MsgBox "Completed", vbInformation
    
End Sub

Thanks!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The macro below selects a range, searches for 130620, 130618, 133362,130604, and if found, changes any instance of "BLUE" or "ORANGE" to "BLACK;" however, the user noticed that if the cell that contains 1 of those 4 numbers contains ONLY that number, "BLUE" or "ORANGE" are not updated - but if the cell contains any text or numbers along with 1 of those 4 numbers, it works. Can anyone help me tweak this so it will work when any of those 4 numbers are within the range, whether alone or as part of a string?

Code:
Sub UpdateLabels()

Dim fn As Variant, f As Integer

    fn = Application.GetOpenFilename("Excel-files,*.xls", _
        1, "Select One Or More Files To Open", , True)
    If TypeName(fn) = "Boolean" Then Exit Sub
    For f = 1 To UBound(fn)
        Debug.Print "Selected file #" & f & ": " & fn(f)
        Workbooks.Open fn(f)

               On Error Resume Next
               
               With Range("A40:Z90")
               
               r = .find(130620, LookIn:=xlValues)
               s = .find(130618, LookIn:=xlValues)
               t = .find(133362, LookIn:=xlValues)
               u = .find(130604, LookIn:=xlValues)

               If Not r = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               End With

       ActiveWorkbook.Close savechanges:=True

   Next f

MsgBox "Completed", vbInformation
    
End Sub

Thanks!

Maybe:

Code:
Sub UpdateLabels()

Dim fn As Variant, f As Integer

    fn = Application.GetOpenFilename("Excel-files,*.xls", _
        1, "Select One Or More Files To Open", , True)
    If TypeName(fn) = "Boolean" Then Exit Sub
    For f = 1 To UBound(fn)
        Debug.Print "Selected file #" & f & ": " & fn(f)
        Workbooks.Open fn(f)

               On Error Resume Next
               
               With Range("A40:Z90")
               
               r = .Find(130620, LookIn:=xlValues)
               s = .Find(130618, LookIn:=xlValues)
               t = .Find(133362, LookIn:=xlValues)
               u = .Find(130604, LookIn:=xlValues)

               If Not r = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlWhole
               If Not s = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlWhole
               If Not t = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlWhole
               If Not u = "" Then .Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlWhole
               If Not r = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlWhole
               If Not s = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlWhole
               If Not t = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlWhole
               If Not u = "" Then .Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlWhole
               If Not r = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlWhole
               If Not s = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlWhole
               If Not t = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlWhole
               If Not u = "" Then .Replace What:="GREEN", Replacement:="BLACK", LookAt:=xlWhole
               If Not r = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlWhole
               If Not s = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlWhole
               If Not t = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlWhole
               If Not u = "" Then .Replace What:="RED", Replacement:="BLACK", LookAt:=xlWhole
               
               
               End With

       ActiveWorkbook.Close savechanges:=True

   Next f

MsgBox "Completed", vbInformation
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
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