VBA - delete text b/w first and second space in a string of text - apply to selected range

mgerner

New Member
Joined
Jun 16, 2018
Messages
5
I'm using the following formula to return everything in the referenced cell except the text between the first and second space. The word b/w the first and second space has to be taken out. I have several hundred cells that I need to apply this to in different workbooks.

=CONCATENATE(LEFT(A1,(FIND(" ",A1,1)-1))," ",MID(A1,FIND(" ",A1,FIND(" ",A1)+1)+1,256))

I was wanting to be able to select a range of cells on the sheet that I need to apply this formula to, and then have a macro loop through all the cells that I have selected and make the change.

For example,

If a cell has "John Smith California Unites States", I need it to change to "John California United States"

It will always be the word between the first and second space that I need to take out.


I started to do something like below, but I don't know if this is the right approach or not...Any help would be greatly appreciated.


Code:
Sub deleteword()

Dim rng As Range, cell As Range
Set rng = selection
For Each cell In rng

ActiveCell.Formula = ???

Next cell

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Will the format of your cells always be space-delimited sub strings? If yes, this should do what you want. Be sure to select all the cells you want to modify first, then run the macro.
Code:
Sub DeleteBtwFirstnSecondSpace()
'select cells first then run this macro
Dim c As Range, S As Variant
Application.ScreenUpdating = False
For Each c In Selection
    S = Split(c.Value, " ")
    If UBound(S) > 0 Then
        c.Value = Replace(c.Value, S(1) & " ", "")
    End If
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option, using you original equation
Code:
Sub DeleteWord()
Selection.Value = Evaluate(Replace("if({1},CONCATENATE(LEFT(@,(FIND("" "",@,1)-1)),"" "",MID(@,FIND("" "",@,FIND("" "",@)+1)+1,256)),@)", "@", Selection.Address))
End Sub
 
Upvote 0
This works perfectly. Thank you!! This is going to save me so much time! They will always be space-delimited strings
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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