Formula or VBA

Brentsa

Board Regular
Joined
Oct 3, 2013
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Good day,

I'm a newbie to VBA but have gotten help previously with a VBA formula it it makes everything so much easier. so I was wondering if I could get help.

In column C of my spreadsheet I have display names:

Mrs MAT Schnetler
Mr J Smith
Ms AD Adams
Mrs ASDLK Botha

I have used the following three formulas:
=RIGHT(C1,LEN(C1)-FIND(" ",C1)) - to get rid of the salutations
=LEFT(D2,FIND(" ",D2,1)-1) - to get rid of surname
=SUBSTITUTE(TRIM(MID(E1,1,1)&" "&MID(E1,2,1)&" "&MID(E1,3,1)&" "&MID(E1,4,1))," "," ") - to add a space between the initial letters.

so my results are:
M A T
J
A D
A S D L K

However I ended up creating three extra columns D,E.F in my spreadsheet then copy and pasting my final result in Column F and paste special as Values in column D then delete column E and F.

So I would like to either combine all three formulas or better yet create a VBA string so that it can do it all.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,966
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Try this with VBA
VBA Code:
Sub Extract2()
Dim i As Long, Lr As Long, A As String, B As String, C As String, L As Long, D As String, E As String, j As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
For i = 1 To Lr
A = Right(Range("C" & i).Value, Len(Range("C" & i).Value) - Application.WorksheetFunction.Find(" ", Range("C" & i).Value))
B = Left(A, Application.WorksheetFunction.Find(" ", A) - 1)
L = Len(B)
E = Left(B, 1)
For j = 2 To L
C = Mid(B, j, 1)
D = Replace(C, C, " " & C)
E = E & D
Next j
Debug.Print E
Next i
End Sub

And this with Formula
Excel Formula:
=SUBSTITUTE(TRIM(MID(LEFT(RIGHT(C1,LEN(C1)-FIND(" ",C1)),FIND(" ",RIGHT(C1,LEN(C1)-FIND(" ",C1)),1)-1),1,1)&" "&MID(LEFT(RIGHT(C1,LEN(C1)-FIND(" ",C1)),FIND(" ",RIGHT(C1,LEN(C1)-FIND(" ",C1)),1)-1),2,1)&" "&MID(LEFT(RIGHT(C1,LEN(C1)-FIND(" ",C1)),FIND(" ",RIGHT(C1,LEN(C1)-FIND(" ",C1)),1)-1),3,1)&" "&MID(LEFT(RIGHT(C1,LEN(C1)-FIND(" ",C1)),FIND(" ",RIGHT(C1,LEN(C1)-FIND(" ",C1)),1)-1),4,1))," "," ")
 
Solution

Brentsa

Board Regular
Joined
Oct 3, 2013
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Formula works perfectly.

VBA code does not but it could be something that I am doing..
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,966
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Sorry. I forgot to add result to Excel window it shows results at immediate window Try this
VBA Code:
Sub Extract2()
Dim i As Long, Lr As Long, A As String, B As String, C As String, L As Long, D As String, E As String, j As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
For i = 1 To Lr
A = Right(Range("C" & i).Value, Len(Range("C" & i).Value) - Application.WorksheetFunction.Find(" ", Range("C" & i).Value))
B = Left(A, Application.WorksheetFunction.Find(" ", A) - 1)
L = Len(B)
E = Left(B, 1)
For j = 2 To L
C = Mid(B, j, 1)
D = Replace(C, C, " " & C)
E = E & D
Next j
Range("D" & i).Value = E
Next i
End Sub
 

Brentsa

Board Regular
Joined
Oct 3, 2013
Messages
87
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Sorry. I forgot to add result to Excel window it shows results at immediate window Try this
VBA Code:
Sub Extract2()
Dim i As Long, Lr As Long, A As String, B As String, C As String, L As Long, D As String, E As String, j As Long
Lr = Range("C" & Rows.Count).End(xlUp).Row
For i = 1 To Lr
A = Right(Range("C" & i).Value, Len(Range("C" & i).Value) - Application.WorksheetFunction.Find(" ", Range("C" & i).Value))
B = Left(A, Application.WorksheetFunction.Find(" ", A) - 1)
L = Len(B)
E = Left(B, 1)
For j = 2 To L
C = Mid(B, j, 1)
D = Replace(C, C, " " & C)
E = E & D
Next j
Range("D" & i).Value = E
Next i
End Sub
the first entry in is line 2 does this make a difference?
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,092
Office Version
  1. 2013
Platform
  1. Windows
Hi
What about
VBA Code:
Sub test()
    Dim a, c As Variant
    Dim i, ii As Long
    a = Range(Range("C1"), Range("C" & Rows.Count).End(xlUp))
    For i = 1 To UBound(a)
        c = Split(a(i, 1))
        For ii = 1 To Len(c(1)) * 2 Step 2
            a(i, 1) = WorksheetFunction.Substitute(c(1), Mid(c(1), ii, 1), Mid(c(1), ii, 1) & " ")
            c(1) = a(i, 1)
        Next
    Next
    Range("D1").Resize(UBound(a)) = a
End Sub
 

Brentsa

Board Regular
Joined
Oct 3, 2013
Messages
87
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Hi
What about
VBA Code:
Sub test()
    Dim a, c As Variant
    Dim i, ii As Long
    a = Range(Range("C1"), Range("C" & Rows.Count).End(xlUp))
    For i = 1 To UBound(a)
        c = Split(a(i, 1))
        For ii = 1 To Len(c(1)) * 2 Step 2
            a(i, 1) = WorksheetFunction.Substitute(c(1), Mid(c(1), ii, 1), Mid(c(1), ii, 1) & " ")
            c(1) = a(i, 1)
        Next
    Next
    Range("D1").Resize(UBound(a)) = a
End Sub
this vba works
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,092
Office Version
  1. 2013
Platform
  1. Windows
Glad I could help
Thank you for the feedback
Be happy & safe
 

Forum statistics

Threads
1,136,844
Messages
5,678,086
Members
419,741
Latest member
BKN336

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
Top