Insert dashes within character string, within an array.

pgiering

New Member
Joined
Oct 20, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
ISO VBA command to insert a dash (hyphen) as the 5th character from the left, and 6th character from the right, in a string of varying length, within an array.

For example...
Column Z contains the following list of values:
MSEAVIMA0013S
MSEAVIRM0016S
MOLPVIRM0017S
MOLS130012S
MSEAVIRM0011S
MOL21300040S
MSEAVIMA0008S

I would like to run a macro which converted them to:
MSEA-VIMA-0013S
MSEA-VIRM-0016S
MOLP-VIRM-0017S
MOLS-13-0012S
MSEA-VIRM-0011S
MOL2-130-0040S
MSEA-VIMA-0008S

The rows may vary (e.g. - it might be 2-8, or it might be 3-49, etc.), so I would plan to select the range before running the macro.

Thank you!

Paul
 
If you are interested, here is another macro that you could try after selecting the range you want processed.

VBA Code:
Sub Insert_Dashes()
  Dim c As Range
  
  With CreateObject("VBScript.RegExp")
    .Pattern = "(^.{4})(.+)(.{5}$)"
    For Each c In Selection
      If .test(c.Value) Then c.Value = .Replace(c.Value, "$1-$2-$3")
    Next c
  End With
End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,614
Messages
6,120,530
Members
448,969
Latest member
mirek8991

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