VBA 4th space

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good evening,

I am trying to find a VBA Function that will get everything after the 4th space. I know how to do this with a formula but I am learning VBA. I found something on the internet that finds the position but I am not good enough to modify it. Any help would be appreciated!

1 8:00 am 2 PARK AVE & WOOD AVEPARK AVE & WOOD AVE
2 8:04 am 4 TAFT AVE & PARK AVETAFT AVE & PARK AVE
3 8:11 am 2 MADISON AVE & NORTH AVEMADISON AVE & NORTH AVE
4 8:15 am 5 CAPITOL AVE & MAIN STCAPITOL AVE & MAIN ST
5 8:18 am 7 GARFIELD AVE & MADISON AVEGARFIELD AVE & MADISON AVE
6 8:21 am 2 FAIRVIEW AVE & WAYNE STFAIRVIEW AVE & WAYNE ST
7 8:26 am 6 DEWHIRST ST & JEWETT AVEDEWHIRST ST & JEWETT AVE
8 8:29 am 5 ARDMORE ST & PLANKTON STARDMORE ST & PLANKTON ST
9 8:31 am 2 PARK AVE & ROOSTER RIVER BLVDPARK AVE & ROOSTER RIVER BLVD
10 8:34 am 7 DERAMO PL & MADISON AVEDERAMO PL & MADISON AVE

I found this code that finds the position on the 4th space.

VBA Code:
Function FindN(s As String, sFindWhat As String, N As Integer) As Integer
Dim J As Integer
Application.Volatile
FindN = 0
    For J = 1 To N
        FindN = InStr(FindN + 1, s, sFindWhat)
        If FindN = 0 Then Exit For
    Next
End Function
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,​
notice even under VBA I prefer to use an Excel formula for such basic need …​
Anyway an useless VBA function as required :​
VBA Code:
Function AfterSpace4th$(Rg As Range)
     Dim S$(), C%
         S$ = Split(Rg.Text)
    If UBound(S) > 3 Then
         For C = 0 To 3:  S(C) = False:  Next
         AfterSpace4th = Join$(Filter(S, False, False))
    End If
End Function
In cell B1 :​
Excel Formula:
=AfterSpace4th(A1)
 
Upvote 0
A VBA demonstration extracting the text after the 4th space from column A to column B at once :​
VBA Code:
Sub Demo1()
    With [A1].CurrentRegion.Columns
        .Item(2).Value2 = Evaluate(Replace("IFERROR(MID(#,FIND("" "",#,12)+1,99),"""")", "#", .Item(1).Address))
    End With
End Sub
 
Upvote 0
Give this VBA function a try...
VBA Code:
Function After4thSpace(S As String) As String
  After4thSpace = Split(S & "    ", " ", 5)(4)
End Function

Edit Note: There are 4 spaces in the quote after the & symbol
 
Upvote 0
Thank you both! I appreciate your help and guidance! All solutions worked great!
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,990
Members
449,480
Latest member
yesitisasport

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