Change text multiple WBs

Jenawade

Board Regular
Joined
Apr 8, 2002
Messages
231
We have to go through hundreds of workbooks to change one word. I'm hoping someone can help with this. The below is an example of the data. This information is not always in the same row and/or column. There will always be only one worksheet (each WB has a WS with a different name). If the number in G57 in this example (it will not always be in this exact cell, but nearby) starts with either 130620, 130618, 133362, 130604, then the word "BLUE" in cell G68 (may be in a different cell in other WBs) needs to be changed to "BLACK".

I'm hoping this is easy for someone and they can whip me up a macro that lets me pick a folder, then runs through looking for 130620, 130618, 133362, 130604 and when it finds that number, changes BLUE to BLACK.

Excel Workbook
ABCDEFGHIJKLMNP
49***************
50UNIT LABEL REQUIREMENTS*
511. *THE UNIT LABEL SHOULD HAVE CUSTOMER NUMBER:05083789AA*
522. *THE BARCODE SHOULD READ:05083789AA*
533. *THE CUSTOMER CODE IS:55087AG*
544. *THE COUNTRY OF ORIGIN SHOULD BE:MEXICO*
555. *THE QUANTITY SHOULD BE BEFORE THE CUSTOMER NUMBER.*
566. *THE DATE MUST APPEAR ON THE LABEL.*
577. *THE PART NUMBER OF THE LABEL IS:130620 (L24)*
588. THE MEASUREMENT IS:2" X 4"*
59***************
60UNIT CARTON REQUIREMENTS*
611. *THE UNIT CARTON IS:130593*
622. *(DESCRIPTION OF CARTON)7X5X8.5 FOL*
633. *(PLACEMENT OF LABEL.)FLAP*
644. *(CLOSURE PROCEDURE)1/2" TAPE*
655. *(POLYBAG)N/A*
66***************
67MISC. INFORMATION*
68FINISHED KITS TO BE PLACED ONBLUESKIDS****
69CORRUGATED SHIPPER NEEDED**************
70OVERPACK LABELS NEEDED *4 X 6**************
71***************
143598


Thank you!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Well, this attempt doesn't work:
Code:
Sub UpdateLabels()

   FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
            
   On Error Resume Next
   
   For FNum = LBound(FileNameXls) To UBound(FileNameXls)
           
   If Cells.find(What:="130620", after:=sh.Range("A30"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row _
   Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
   
   If Cells.find(What:="130618", after:=sh.Range("A30"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row _
   Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

   If Cells.find(What:="133362", after:=sh.Range("A30"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row _
   Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

   If Cells.find(What:="130604", after:=sh.Range("A30"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row _
   Then Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

   On Error GoTo 0

   Next FNum
        
        End If
End Sub
 
Upvote 0
If Cells.find(What:="130620",...[/code]
I might be afraid of this search mechanism... there is at least one other number on the worksheet of the same "size and shape" as 130620 (as well as the other numbers your code searches for) which might accidentally match the sought after number.
 
Upvote 0
This information is not always in the same row and/or column.
Is the text in Column A on the row the numbers you are looking for always "THE PART NUMBER OF THE LABEL IS"? I'm looking for something that is always on every label and always the same offset from the cells with the numbers you are searching for.
 
Upvote 0
Is the text in Column A on the row the numbers you are looking for always "THE PART NUMBER OF THE LABEL IS"? I'm looking for something that is always on every label and always the same offset from the cells with the numbers you are searching for.

Yes it is :D
 
Upvote 0
I don't understand. This 1st one errors out, telling me I have a next without a for, but there is a for. The 2nd one errors out, telling me I have a loop without a do, but there is do.

Code:
Sub UpdateLabels4()

Dim Match As Boolean

FileNameXls = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)

On Error Resume Next

For i = LBound(FileNameXls) To UBound(FileNameXls)

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130620, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130618, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
  
        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=133362, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130604, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

  Next i

End Sub
Code:
Sub UpdateLabels5()

Dim oWbk As Workbook
Dim sFil As String
Dim sPath As String

sPath = Application.GetOpenFilename(filefilter:="Excel Files, *.xl*", MultiSelect:=True)
ChDir sPath
sFil = Dir("*.xls")
Do While sFil <> ""

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130620, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130618, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart
  
        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=133362, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

        Match = ActiveWorkbook.ActiveSheet.Cells.find(What:=130604, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Match Then
        Cells.Replace What:="BLUE", Replacement:="BLACK", LookAt:=xlPart
        Cells.Replace What:="ORANGE", Replacement:="BLACK", LookAt:=xlPart

oWbk.Close True
sFil = Dir
Loop
End Sub
 
Upvote 0
I don't understand. This 1st one errors out, telling me I have a next without a for, but there is a for. The 2nd one errors out, telling me I have a loop without a do, but there is do.
Sorry I haven't had time to get back to your question yet (probably this afternoon sometime), but a quick look at your code shows you are missing several "End If" statements... these are needed to close off the If..Then blocks of code you started (just putting a blank space under the code block is not enough to close the If..Then block... you must use "End If" to do that. As for the particularly unhelpful message you are getting... VB treats all block closing statements the same when it counts them up to see if they match the number of block opening statements (If, For, Do, etc.)... the first unclosed block it comes to (the For or Do ones in your case) is what it reports in the error message... its a false indicator in this case.
 
Upvote 0
Thanks. :) I had them in an earlier version and an error popped up and highlighted the first End If when I clicked debug, so I took them out. LOL
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,254
Members
452,900
Latest member
LisaGo

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