Delete everything after the second space in each cell

DBrian

New Member
Joined
Jan 5, 2012
Messages
16
I have a column containing an address in each row. I need something in VBA that will delete everything after the second space in each address, including the second space. The closest thing I could find was this formula:

=LEFT(A1,Find(" ",A1)-1)

This deletes everything after the first space in a cell but I need something to include in a VBA routine. Any suggestions are greatly appreciated.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
The formula =TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ",255),2), 255)) will do what you want.
If I were to write a generealized VBA routine, I'd have the routine put that formula in cells, then convert to constants.

Code:
With Selection.Offset(0,1)
    .FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE(A1, "" "", REPT("" "",255),2), 255))"
    .Value = .Value
End With
 
Upvote 0
I have a column containing an address in each row. I need something in VBA that will delete everything after the second space in each address, including the second space. The closest thing I could find was this formula:

=LEFT(A1,Find(" ",A1)-1)

This deletes everything after the first space in a cell but I need something to include in a VBA routine. Any suggestions are greatly appreciated.
So you are looking for VB code to return everything before the second space in cell A1, correct?

CellVal = Range("A1").Value
TextBeforeSecondSpace = Left(CellVal, InStr(InStr(CellVal, " ") + 1, CellVal, " ") - 1)
 
Upvote 0
The formula =TRIM(LEFT(SUBSTITUTE(A1, " ", REPT(" ",255),2), 255)) will do what you want.
If I were to write a generealized VBA routine, I'd have the routine put that formula in cells, then convert to constants.

Rich (BB code):
With Selection.Offset(0,1)
    .FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE(A1, "" "", REPT("" "",255),2), 255))"
    .Value = .Value
End With
You meant to use the .Formula property instead of the .FormulaR1C1 property, correct?

Another way which will also work...
Rich (BB code):
With Selection.Offset(0, 1)
    .Formula = "=LEFT(A1,FIND("" "",A1,FIND("" "",A1)+1)-1)"
    .Value = .Value
End With
 
Upvote 0
I intented to use the FormulaR1C1 property, but forgot to convert the formula.
Code:
With Selection.Offset(0,1)
    .FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE(RC[-1], "" "", REPT("" "",255),2), 255))"
    .Value = .Value
End With
 
Upvote 0
I intented to use the FormulaR1C1 property, but forgot to convert the formula.
Code:
With Selection.Offset(0,1)
    .FormulaR1C1 = "=TRIM(LEFT(SUBSTITUTE(RC[-1], "" "", REPT("" "",255),2), 255))"
    .Value = .Value
End With
Just curious as to why you used the FormulaR1C2, which required you to convert the formula you had to R1C1 notation, when assigning the formula you had to the Formula property would also work. Or am I missing some underlying "something" here that make one work better than the other?
 
Upvote 0
The formula was put in Selection.Offset(0,1). Using A1 with .Formula would have converted column A rather than Selection.

But in general, for some reason, in VBA, my thought process about relative addressing works better with R1C1 notation. The back of my mind thinks that Range("C1:C3").Formula = "=A1+1" will put "=A1+1" in C3. Yes, I know better, but using R1C1 notation calms the back of my mind.
 
Upvote 0
The formula was put in Selection.Offset(0,1). Using A1 with .Formula would have converted column A rather than Selection.
That is a good point (which I completely overlooked).

But in general, for some reason, in VBA, my thought process about relative addressing works better with R1C1 notation. The back of my mind thinks that Range("C1:C3").Formula = "=A1+1" will put "=A1+1" in C3. Yes, I know better, but using R1C1 notation calms the back of my mind.
Plus, as you noted above, it guarantees that the point you made above would not have to be watched out for. Thanks for responding.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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