With Range?

Jenawade

Board Regular
Joined
Apr 8, 2002
Messages
231
Can someone please help me fine tune this macro? In item #7, as shown below (which is not always in the same place, it may be shifted up or down a fews rows and/or a column in the various workbooks) I'm looing for an instance of 130620, 130618, 133362, 130604. If that exists, then any instance of either Blue or Orange next to "finished kits to be placed" below, needs to be replaced with Black (this is also not always in the same place). The macro I have now will do the work, but, it looks for ANY instance of those 4 part numbers and changes ANY instance of Blue or Orange to Black. In a test the end user performed, he had a file with "blue" in the description which was changed to black.

Code:
Sub test()

    Dim strPath As String
    Dim strFile As String
    Dim wkb As Workbook
    Dim wks As Worksheet
    
    strPath = "H:\Quality\Shared\Chrysler p.s. from Garrett for test"  'Change the path to your folder
    
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    
    strFile = Dir(strPath & "*.xls")
    
    Do While Len(strFile) > 0
        Set wkb = Workbooks.Open(strPath & strFile)
        For Each wks In wkb.Worksheets
     
               On Error Resume Next
               
               Range("A46:Z90").Select
               
               r = Cells.find(130620, LookIn:=xlValues)
               s = Cells.find(130618, LookIn:=xlValues)
               t = Cells.find(133362, LookIn:=xlValues)
               u = Cells.find(130604, LookIn:=xlValues)

               If Not r = "" Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
               If Not r = "" Then Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not s = "" Then Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not t = "" Then Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
               If Not u = "" Then Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
          
        Next wks
        wkb.Close savechanges:=True
        strFile = Dir
    Loop
    
    MsgBox "Completed", vbInformation
    
End Sub

Excel Workbook
ABCDEFGHIJKLM
45UNIT LABEL REQUIREMENTS
461. *THE UNIT LABEL SHOULD HAVE CUSTOMER NUMBER:52128420AA
472. *THE BARCODE SHOULD READ:52128420AA***
483. *THE CUSTOMER CODE IS:55087AG***
494. * THE COUNTRY OF ORIGIN SHOULD BE:MEXICO
505. *THE QUANTITY SHOULD BE BEFORE THE CUSTOMER NUMBER.**
516. *THE DATE MUST APPEAR ON THE LABEL.******
527. *THE PART NUMBER OF THE LABEL IS:133362 l-23*
538. THE MEASUREMENT IS:2X3
136102



Excel Workbook
ABCDEFGHIJKLMNO
63MISC. INFORMATION
64FINISHED KITS TO BE PLACED ONBLACKSKID
65NO CORRUGATED SHIPPER NEEDED
66NO OVERPACK LABELS NEEDED
136102
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
you only want to search in Range("A46:Z90")?
At the moment, your searching and your replacing takes place over the whole sheet because you've used Cells as the qualifier, instead use the range you want to restrict searching/replacing to:

Code:
with Range("A46:Z90")[INDENT] r = [B][COLOR=Red].[/COLOR][/B]find(130620, LookIn:=xlValues)
s = [B][COLOR=Red].[/COLOR][/B]find(130618, LookIn:=xlValues)
'...
If Not r = "" Then [B][COLOR=Red].[/COLOR][/B]Replace What:="BLU etc.
[/INDENT]end with
 
Upvote 0
I added in the range at the last minute to see if that would do it, but it didn't work. I only know enough about VBA to get myself into trouble often!
 
Upvote 0

Forum statistics

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