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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Are your Employee ID numbers aligned in your actual data as shown in your attachment? That is, for any single Employee ID in Column A, will its counterpart always be found on the same row in Column E?
 
Upvote 0
The Column E and adjacent columns data is presented in Sheet2 column A.

The EMP ID is presented in Column A of sheet 2. Its just for screen shot purpose i mentioned at Column E.
 
Upvote 0
Reproducing the data in XL2BB format

I have 2 sheets 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 EMP 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.

For reference I mentioned

Seach and replacemrexcel.xlsx
ABC
1EMP IDBefore MacroAfter Macro - result in Column C
21101I am {name} and currently working as {designation} and working at {Company}I am John Peter and currently working as Senior Human Resources Recruiter and working at Nokiasys
31102I am {name} and currently working as {designation} and working at {Company}I am Jamuna and currently working as Assistant Human Resources Manager and working at KGD Architecture
41103I am {name} and currently working as {designation} and working at {Company}Based on Column A Sheet 2 and sheet 1 Match - the respective keywords to be replaced.
51104I am {name} and currently working as {designation} and working at {Company}Based on Sheet 2 and sheet 1 Column A Match - the respective keywords to be replaced.
61105I am {name} and currently working as {designation} and working at {Company}Based on Sheet 2 and sheet 1 Column A Match - the respective keywords to be replaced.
71106I am {name} and currently working as {designation} and working at {Company}Based on Sheet 2 and sheet 1 Column A Match - the respective keywords to be replaced.
81107I am {name} and currently working as {designation} and working at {Company}Based on Sheet 2 and sheet 1 Column A Match - the respective keywords to be replaced.
Sheet1



Sheet 2 Column A (Reference to replace the data)
Seach and replacemrexcel.xlsx
ABCD
1EMP ID{name}{Designation}{company}
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
Even though you have pointed out these tables are actually on different sheets, you haven't addressed the real point of Rick's question.
For example, in what you have posted here in post #5, EMP ID 1105 appears on worksheet row 6 in both Sheet1 and Sheet2. It is the same situation for each of the other sample EMP IDs. Rick's question is: Does that actually happen for all EMP IDs in your real data?
 
Upvote 0
Thanks Peter.You are Back.

The Answer is No for Rick Question. The data will not be in same row. It can be anywhere in the column A of sheet1
 
Upvote 0
The Answer is No for Rick Question.
OK, thanks. Give this a try with a copy of your data.

VBA Code:
Sub InsertDetails()
  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))
      s = a(i, 2)
      For j = 2 To ubb2
        s = Replace(s, b(1, j), b(r, j), 1, -1, 1)
      Next j
      a(i, 2) = s
    Next i
    .Range("C2").Resize(UBound(a)).Value = Application.Index(a, 0, 2)
  End With
End Sub
 
Upvote 0
What is the full actual error message?

If you Debug and hover over j and then r in that yellow line, what values show in the pop-up?

Also hover over i somewhere and report that value too please.

Here is my result using your sample data (after mixing up the rows in Sheet2 so they didn't all match).
Pete2020 2020-05-07 1.xlsm
ABC
1EMP IDBefore Macro
21101I am {name} and currently working as {designation} and working at {Company}I am John Peter and currently working as Senior Human Resources Recruiter and working at Nokiasys
31102I am {name} and currently working as {designation} and working at {Company}I am Jamuna and currently working as Assistant Human Resources Manager and working at Human Resource
41103I am {name} and currently working as {designation} and working at {Company}I am Shakambari Nayak and currently working as HR Recruiter and working at Roland & Associates
51104I am {name} and currently working as {designation} and working at {Company}I am Divakar Nayak and currently working as Human Resources Executive and working at IDS Software Pvt Ltd
61105I am {name} and currently working as {designation} and working at {Company}I am Veena J and currently working as Senior HR Generalist and working at KGD Architecture
71106I am {name} and currently working as {designation} and working at {Company}I am Kruthi Or Rmaya and currently working as HR Executives and working at Ish Infotech Private Limited
81107I am {name} and currently working as {designation} and working at {Company}I am Monica and currently working as HR Manager and working at Consulace Business Solutions
Sheet1
 
Upvote 0
My guess is that you have an EMP ID on Sheet1 that does not exist on Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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