Adding a space at every fourth letter using VBA

Chris101

New Member
Joined
Feb 17, 2023
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I have a list of circuit ID's that need to be formatted the same way. When I receive the circuit ID's they need to be in the format - "MMEC.123456..ATI" . The circuit IDs are now arriving as "MMEC123456..ATI". There is no "." after the letter "C". I would like to place a "." after the fourth letter of every circuit ID in a column. There will always be a "." after the fourth letter.

I attached a screen shot of the data for review, if necessary.

I can spend hours looking at the various solutions, but still get nowhere. If anyone would be gracious enough to assist me with my issue, I would greatly appreciate it.

Thank you in advance for your time and knowledge.

Best Regards,
Chris
 

Attachments

  • Circuit ID's.png
    Circuit ID's.png
    171.6 KB · Views: 14

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
There's probably simpler ways of doing it, but try this on a copy of your data.
VBA Code:
Sub AddDot()
    With Sheets("DB Load").Range("S2:S" & Sheets("DB Load").Cells(Rows.Count, "S").End(xlUp).Row)
        .Value = Evaluate("LEFT(" & .Address(, , , 1) & ",4)&"".""&RIGHT(" & .Address(, , , 1) & ",LEN(" & .Address(, , , 1) & ")-4)")
    End With
End Sub
 
Upvote 0
Solution
Kevin9999,

I want to thank you for your assistance. It worked perfectly.

I think I would have spent a week trying to figure out what you did and probably still not being able to get it correct.

What do you recommend as a good learning tool for a beginner to learn VBA? favorite Book, YouTube channel, class, etc.? I spent a long time on YouTube looking for your solution and could not find any that worked.

Again, I really appreciate you sharing your knowledge and time with me. Thank You!

Best Regards,
Chris
 
Upvote 0
Actually Chris, the following code is safer - insofar as the previous code will add yet another period if you run it a second time, whereas the code below will not.
VBA Code:
Sub AddDot()
    With Sheets("DB Load").Range("S2:S" & Sheets("DB Load").Cells(Rows.Count, "S").End(xlUp).Row)
        .Value = Evaluate("if(mid(" & .Address(, , , 1) & ",5,1) <> ""."",LEFT(" & .Address(, , , 1) & ",4)&"".""&RIGHT(" & .Address(, , , 1) & ",LEN(" & .Address(, , , 1) & ")-4)," & .Address(, , , 1) & ")")
    End With
End Sub
 
Upvote 0
Actually Chris, the following code is safer - insofar as the previous code will add yet another period if you run it a second time, whereas the code below will not.
VBA Code:
Sub AddDot()
    With Sheets("DB Load").Range("S2:S" & Sheets("DB Load").Cells(Rows.Count, "S").End(xlUp).Row)
        .Value = Evaluate("if(mid(" & .Address(, , , 1) & ",5,1) <> ""."",LEFT(" & .Address(, , , 1) & ",4)&"".""&RIGHT(" & .Address(, , , 1) & ",LEN(" & .Address(, , , 1) & ")-4)," & .Address(, , , 1) & ")")
    End With
End Sub
I tried the safer code and I received the error #VALUE!
 
Upvote 0
I am sure I did something wrong. I copied and pasted it maybe that was the problem.
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,090
Latest member
fragment

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