Help with VBA code

markster

Well-known Member
Joined
May 23, 2002
Messages
507
Good evening

I have a report of few thousand lines that I'm trying to sort in a better way.

Column E has Customer last name from E7 e.g. Smith

Column K contains reference number date from K7 - the reference number is in the following format: TH_G0_S10_H_13084129

I'm looking for some macro code that will do the following

1. Add the customer last name to the reference number is column K so the reference number will look like this: Smith_TH_G0_S10
2. Remove the H_ number from the reference number column and add to column O so column O will read H_13084129

Any help you can give would be much appreciated.

Thanks in advance.
Mark
 

Attachments

  • 1615314473959.png
    1615314473959.png
    1.4 KB · Views: 10

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,311
Will the reference number always have the same number of characters?
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,311
Will the number of underscores always be the same?
 

markster

Well-known Member
Joined
May 23, 2002
Messages
507

ADVERTISEMENT

Sorry for the delay - yes the number of underscores will always be the same
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,311
Try:
VBA Code:
Sub SplitValue()
    Application.ScreenUpdating = False
    Dim v As Variant, dic As Object, vName As Variant
    v = Range("E7", Range("E" & Rows.Count).End(xlUp)).Resize(, 7).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(v, 1)
        vName = Split(v(i, 7), "_")
        dic.Add Key:=v(i, 1) & "_" & vName(0) & "_" & vName(1) & "_" & vName(2), Item:=vName(3) & "_" & vName(4)
    Next i
    Range("K7").Resize(dic.Count).Value = Application.Transpose(dic.keys)
    Range("O7").Resize(dic.Count).Value = Application.Transpose(dic.items)
End Sub
 

markster

Well-known Member
Joined
May 23, 2002
Messages
507

ADVERTISEMENT

Hey mate

Thanks for this. Just tried it and I got an error Runtime error 429 - active X component can't create object. I clicked on the debugger and the bit of the code that was highlighted as an error was Set dic = CreateObject Scripting Dictionary. Any ideas?

Thanks again mate.
Mark
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub markster()
   Dim Ary1 As Variant, Ary2 As Variant, Ary3 As Variant
   Dim r As Long
   
   r = Range("E" & Rows.Count).End(xlUp).Row
   Ary1 = Range("E7:E" & r).Value2
   Ary2 = Range("K7:K" & r).Value2
   ReDim Ary3(1 To UBound(Ary1), 1 To 1)
   For r = 1 To UBound(Ary2)
      Ary3(r, 1) = Split(Ary2(r, 1), "_", 4)(3)
      Ary2(r, 1) = Ary1(r, 1) & "_" & Replace(Ary2(r, 1), "_" & Ary3(r, 1), "")
   Next r
   Range("K7").Resize(r - 1).Value = Ary2
   Range("O7").Resize(r - 1).Value = Ary3
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,130,170
Messages
5,640,568
Members
417,151
Latest member
ChickenTenderer

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
Top