Help with VBA code

markster

Well-known Member
Joined
May 23, 2002
Messages
579
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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: 12

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Will the reference number always have the same number of characters?
 
Upvote 0
Will the number of underscores always be the same?
 
Upvote 0
Sorry for the delay - yes the number of underscores will always be the same
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Are you using a Mac or a PC?
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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