VBA code for scan cell and do line break before specific letter sequence.

JAZ91

New Member
Joined
Sep 28, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a macro I would like to make but I am not very proficient in VBA coding and are having a hard time finding anything similar that i could tweak to work.

What I would like to do is to scan a column and then do a line break before a specific sequence of letters.
Secondarily I would also (if possible) like to add "-" between the equipment type.

Here is a little explanation of the data and to add some context.
The first 3 numbers and the first letter designate the process module. The next letter(s) are the type of equipment. The next numbers are the specific unit.
When all this is combined it makes up a TAG number. The letters DI/DO/AO/AI are the signal type. And then the remaining part is the RTU and the number of that specific RTU along with the IO channel which the TAG number is connected up to.

Finally, if I could then wrap the cells so i have 2 lines with final data it would be perfect.
This goal is to make cable labels easily from an imported file from the control system I'm working with.


i.e. my data looks like this.

120AV199_DI RTU12.2.4 / 01.05

240AFT209_DO RTU15.3.8 / 02.06

770AV405_AO RTU10.7.2 / 05.09

820ALSH304_AI RTU10.5.9 / 07.03

i.e. how I want the result to look

120A-V-199_DI
RTU12.2.4 / 01.05

240A-FT-209_DO
RTU15.3.8 / 02.06

770A-V-405_AO
RTU10.7.2 / 05.09

820A-LSH-304_AI
RTU10.5.9 / 07.03
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try This:

VBA Code:
Sub format_data()
    Dim rng As Range, cel As Range

    Set rng = Selection

    For Each cel In rng
        'Add line break before "_"
        cel.Value = Replace(cel.Value, "_", vbCrLf & "_")
        'Add "-" between equipment type
        cel.Value = Replace(cel.Value, "AV", "A-V")
        cel.Value = Replace(cel.Value, "FT", "F-T")
        cel.Value = Replace(cel.Value, "LSH", "L-S-H")
        'Wrap the text in the cell
        cel.WrapText = True
    Next cel
End Sub

You can run this macro by selecting the column of data you want to format and then running the macro.

Note that this code uses the Replace function to add the line break and the "-" characters in the appropriate places. The WrapText property is set to True to wrap the text in the cell.

You can add more Replace function if you have more equipment type, also if you have different format of the data you can edit the Replace function accordingly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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