Search and Replace Text strings based on Column Header

Pete2020

Board Regular
Joined
Apr 25, 2020
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
I have 2 columns data in

Sheet 1 -Column A is a Emp ID and Column B is a text with identified text Keywords to be replaced.
Sheet 2- Column A has Emp ID which is a primary key to sheet 1 to compare

Once the column A ID is matched in both sheets, then Column B to Column K based on header name(keyword to search in column B), the respective keywords in each cell are to be replaced.

I have around 10k data Looking for a Macro or a VB function to search and replace.

I am very thankful for creating this wonderful platform and helping us
 

Attachments

  • Search and Replace.JPG
    Search and Replace.JPG
    143 KB · Views: 25

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This is my sheet2 data

Seach and replacemrexcel.xlsx
ABCDE
1EMP IDnamedesignationcompany
21101John PeterSenior Human Resources RecruiterNokiasys
31102JamunaAssistant Human Resources ManagerHuman Resource
41103Shakambari NayakHR RecruiterRoland & Associates
51104Divakar NayakHuman Resources ExecutiveIDS Software Pvt Ltd
61105Veena JSenior HR GeneralistKGD Architecture
71106Kruthi Or RmayaHR ExecutivesIsh Infotech Private Limited
81107MonicaHR ManagerConsulace Business Solutions
91108abhay punjabiHuman Resources RecruiterVertisystem
101109RaviHR PartnerUdaya Enterprises
111110Shubhra TripathySenior HR AssociateAdodis Technologies Pvt. Ltd.
Sheet2
 
Upvote 0
Sheet 1 data

Seach and replacemrexcel.xlsx
AB
1EMP IDBefore Macro
21101I am name and currently working as designation and working at Company
31102I am name and currently working as designation and working at Company
41103I am name and currently working as designation and working at Company
51104I am name and currently working as designation and working at Company
61105I am name and currently working as designation and working at Company
71106I am name and currently working as designation and working at Company
81107I am name and currently working as designation and working at Company
91108I am name and currently working as designation and working at Company
101109I am name and currently working as designation and working at Company
111110I am name and currently working as designation and working at Company
121111I am name and currently working as designation and working at Company
131112I am name and currently working as designation and working at Company
Sheet1
 
Upvote 0
My guess is that you have an EMP ID on Sheet1 that does not exist on Sheet2
As per this comment. Sheet1 has EMP ID 1111 and 1112 but Sheet2 does not have these. How can it do replacements for these when they do not exist in Sheet2?
 
Upvote 0
A further point: Your original samples had curly braces {} around the headings in Sheet2 and the place markers in Sheet1 but these appear to have gone. This could lead to some unexpected results.

Example just to demonstrate
Sheet1
I am name but sometimes I am misnamed

Sheet2 replacement for name is John

Without the curly braces, the result would be
I am John but sometimes I am misJohnd
 
Upvote 0
Peter.

I deleted 1111 and 1112 for the time being from sheet 1

Now sheet 1 and sheet 2 EMP ID is matched. Also keywords changed to {} Curly brackets
Cross checked whether keywords is exactly matched from sheet and sheet 2 - They are fine

Now,I run the macro - but macro showing an error
s = Replace(s, b(1, j), b(r, j), 1, -1, 1)
 
Upvote 0
After going through i observed said few IDs not matched with sheet 1 and Sheet 2.

Please add "Not found" in Macro when EMP ID is not matched aganist Sheet 2

I am very Thankful to you, because you are teaching me well.

Good Learning.

Great excel mates on Mr.Excel forum
 
Upvote 0
After going through i observed said few IDs not matched with sheet 1 and Sheet 2.

Please add "Not found" in Macro when EMP ID is not matched aganist Sheet 2
Try

VBA Code:
Sub InsertDetails_v02()
  Dim d As Object
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, r As Long, ubb2 As Long
  Dim s As String
  
  b = Sheets("Sheet2").Range("A1").CurrentRegion.Value
  ubb2 = UBound(b, 2)
  Set d = CreateObject("Scripting.Dictionary")
  For i = 2 To UBound(b)
    d(b(i, 1)) = i
  Next i
  With Sheets("Sheet1")
    a = .Range("A2", .Range("B" & .Rows.Count).End(xlUp)).Value
    For i = 1 To UBound(a)
      r = d(a(i, 1))
      If r = 0 Then
        s = "Not Found"
      Else
        s = a(i, 2)
        For j = 2 To ubb2
          s = Replace(s, b(1, j), b(r, j), 1, -1, 1)
        Next j
      End If
      a(i, 2) = s
    Next i
    .Range("C2").Resize(UBound(a)).Value = Application.Index(a, 0, 2)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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