Appending text to a certain section in a cell (VBA)

rimrattlerla11

New Member
Joined
Jan 27, 2016
Messages
17
I have a large dataset in a csv file in which I am trying to append text whenever it comes across the words "PREFERRED CLASSROOM PKG." in the cell. Currently the word "null" is found where I want the text to appear.

The PREFERRED CLASSROOM PKG. text appears in the beginning or somewhere in the middle of the cell.
Each cell of the worksheet is in a format similar to below:

[{"code":null,"long_name":null,"name":"PREFERRED CLASSROOM PKG.101A","msrp":100,"includes": ]},"code":null,"long_name":null,"name":"ABCDEF","msrp":80,"includes":[]},.......]

OR

[{"code":null,"long_name":null,"name":"ABCDEF","msrp":80,"includes": ]},"code":null,"long_name":null,"name":"PREFERRED CLASSROOM PKG.101A","msrp":100,"includes":[]},.......]



The desired output inside each cell will be replacing the word null with the following two things:

1. Appending the four characters that come after the words "PREFERRED CLASSROOM PKG." to the "code" section surrounded by " " ("101A")
2. Appending the words Classroom Group and the four character code to the "long_name" section surrounded by " ". ("Classroom Group 101A")

[{"code":"101A","long_name":"Classroom Group 101A","name":"PREFERRED CLASSROOM PKG.101A","msrp":100,"includes": ]},"code":null,"long_name":null,"name":"ABCDEF","msrp":80,"includes":[]},.......]

OR

[{"code":null,"long_name":null,"name":"ABCDEF","msrp":80,"includes": ]},"code":"101A","long_name":"Classroom Group 101A","name":"PREFERRED CLASSROOM PKG.101A","msrp":100,"includes":[]},.......]

Please let me know if anything was unclear.
I'm out of my element with VBA, and I'm fairly certain I need some VBA code in order to make this happen.

Thanks in advance for any help you can provide!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this macro in a copy of your workbook. Post back if you need details of how to implement it.

I have assumed data starts in cell A2 and I've written the results starting in B2. You can try changing the code (see red items) if that is not the correct layout or post back with those details.
Rich (BB code):
Sub Replace_Text()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long
  Dim s As String, sSub1 As String, sSub2 As String
  
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(.+)(null)(.+?)(null)(.+?PREFERRED CLASSROOM PKG.+)"
  For i = 1 To UBound(a)
    s = a(i, 1)
    sSub1 = Mid(s, InStr(1, s, "PREFERRED CLASSROOM PKG.") + 24, 4)
    sSub2 = "Classroom Group " & sSub1
    a(i, 1) = RX.Replace(s, "$1""" & sSub1 & """$3""" & sSub2 & """$5")
  Next i
  Range("B2").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
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