Insert a Character into a Word

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello All, i have sentence in D column and range is dynamic, it is mixed of alphanumeric. I need a VBA to look into the words with 7 characters, copy the first character of the word and insert as fifth character. This makes all the 7 character words into 8 character words in the sentence.

Example: Q374 WE333RE333 N123154 QW258 A878968. Expected output is Q374 WE333RE333 N123N154 QW258 A878A968.

Thank you.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
VBA Code:
Sub test()
  Dim myArr As Variant, tmp As Variant
  myArr = Intersect(Columns("D"), ActiveSheet.UsedRange)
  For i = LBound(myArr) To UBound(myArr)
    tmp = Split(myArr(i, 1), " ")
    For j = LBound(tmp) To UBound(tmp)
      If Len(tmp(j)) = 7 Then
        tmp(j) = Left(tmp(j), 4) & Left(tmp(j), 1) & Right(tmp(j), 3)
      End If
    Next
    myArr(i, 1) = Join(tmp, " ")
  Next
  Intersect(Columns("D"), ActiveSheet.UsedRange).Resize(UBound(myArr)) = myArr
End Sub
 
Upvote 0
A better short hand would be:
VBA Code:
Sub test()
  Dim myArr As Variant, tmp As Variant
  With ActiveSheet
  myArr = Intersect(.Columns("D"), .UsedRange)
  For i = LBound(myArr) To UBound(myArr)
    tmp = Split(myArr(i, 1), " ")
    For j = LBound(tmp) To UBound(tmp)
      If Len(tmp(j)) = 7 Then
        tmp(j) = Left(tmp(j), 4) & Left(tmp(j), 1) & Right(tmp(j), 3)
      End If
    Next
    myArr(i, 1) = Join(tmp, " ")
  Next
  Intersect(.Columns("D"), .UsedRange) = myArr
  End With
End Sub
 
Upvote 0
Solution
A better short hand would be:
VBA Code:
Sub test()
  Dim myArr As Variant, tmp As Variant
  With ActiveSheet
  myArr = Intersect(.Columns("D"), .UsedRange)
  For i = LBound(myArr) To UBound(myArr)
    tmp = Split(myArr(i, 1), " ")
    For j = LBound(tmp) To UBound(tmp)
      If Len(tmp(j)) = 7 Then
        tmp(j) = Left(tmp(j), 4) & Left(tmp(j), 1) & Right(tmp(j), 3)
      End If
    Next
    myArr(i, 1) = Join(tmp, " ")
  Next
  Intersect(.Columns("D"), .UsedRange) = myArr
  End With
End Sub
Hi, it is working as expected. Thanks for your efforts.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,112
Members
452,302
Latest member
TaMere

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