Macro to replace text based on reference list

Anglais428v2

New Member
Joined
Jun 19, 2020
Messages
27
Office Version
  1. 365
Platform
  1. Windows
I have a list of company names in range G2:G30000. I want to replace certain suffix (e.g. Inc, GmBH etc.) from their names. I have a list of suffix in range B2:B500 (cell B2 is '& Co' for example)

I have a macro that works well but for only the first instance and I need it to be dynamic:

Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="& CO*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.End(xlUp).Select
Range("G2").Select

Note I am using the asterix after the text & CO as I want to replace any text to the right of '& CO'.
What I need is the above put into a loop and replace the "What= "& CO*" with the cell reference B2 ('& CO' is housed within cell B2). I then want the function to loop and go to cell B3 (which contains '(Pty) Ltd'), do the replace and so on until cell B500.

Any advise?

Thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I have a list of company names in range G2:G30000. I want to replace certain suffix (e.g. Inc, GmBH etc.) from their names. I have a list of suffix in range B2:B500 (cell B2 is '& Co' for example)

I have a macro that works well but for only the first instance and I need it to be dynamic:

Range("G2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="& CO*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Selection.End(xlUp).Select
Range("G2").Select

Note I am using the asterix after the text & CO as I want to replace any text to the right of '& CO'.
What I need is the above put into a loop and replace the "What= "& CO*" with the cell reference B2 ('& CO' is housed within cell B2). I then want the function to loop and go to cell B3 (which contains '(Pty) Ltd'), do the replace and so on until cell B500.

Any advise?

Thanks
Can you please provide a good set of sample data using XL2BB?

Thanks
 
Upvote 0
Column B:

Suffix (to be replaced)
& Co
, Inc
, Incorporated
ltd
GmbH

Column G
Company name (original)Desired result
Microsoft, IncMicrosoft
Porsche GmbHPorsche
1E ltd1E
ACO Ahlmann SE & Co KGACO Ahlmann SE
 
Upvote 0
Try this on a copy of your data.

NOTE the changes that you need to make.

VBA Code:
Public Sub subReplaceText()
Dim arrToReplace() As Variant
Dim arrReplaceBy() As Variant
Dim WsData As Worksheet
Dim i As Integer
Dim ii As Integer

  Set WsData = Worksheets("Data")
  ' CHANGE THIS LINE (Worksheet Name) AS APPROPRIATE.
  
  WsData.Activate

  With WsData
  
    arrToReplace = .Range("G2:G" & Cells(Rows.Count, 7).End(xlUp).Row)
  ' CHANGE THIS LINE (Range) AS APPROPRIATE.
  
    arrReplaceBy = .Range("B2:B" & Cells(Rows.Count, 2).End(xlUp).Row)
    ' CHANGE THIS LINE (Range) AS APPROPRIATE.
  
  End With
  
  For i = 1 To UBound(arrToReplace)
  
    For ii = 1 To UBound(arrReplaceBy)
    
      If Right(arrToReplace(i, 1), Len(arrReplaceBy(ii, 1))) = arrReplaceBy(ii, 1) Then
            
        arrToReplace(i, 1) = Trim(Left(arrToReplace(i, 1), Len(arrToReplace(i, 1)) - Len(arrReplaceBy(ii, 1))))
    
      End If
    
    Next ii
      
  Next i
  
  WsData.Range("H2").Resize(UBound(arrToReplace), 1).Value = arrToReplace
  ' CHANGE THIS RANGE (H2 - Start of destination range) AS APPROPRIATE.
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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