Remove everything after second last space

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
hi all,

I need a macro for removing everything after second last space from a string. example data is provided below.

My name is khan.
this is not an obvious mistake.
could you please come here

after running macro it should be like:

My name
this is not an
could you please

Please not that formula is not required.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
My name is khan.
this is not an obvious mistake.
could you please come here
after running macro it should be like:
My name
this is not an
could you please
this macro does it
Sub Macro4()
'
' Macro4 Macro
' Macro recorded 04/01/2019 by bob
'
'
Dim space(50)
My name rrow = 19
this is not an For j = 1 To 3
could you please For k = 1 To 99
mylen = Len(Cells(j, 1))
If Mid(Cells(j, 1), k, 1) = " " Then Sum = Sum + 1: space(Sum) = k
If k = mylen Then GoTo 50 Else GoTo 100
50 critspace = space(Sum - 1)
rrow = rrow + 1
Cells(rrow, 1) = Mid(Cells(j, 1), 1, critspace)
100 Next k
Sum = 0
Next j
End Sub

<colgroup><col><col span="12"></colgroup><tbody>
</tbody>
 
Upvote 0
Another option
Code:
Sub Deepk()
   Dim Cl As Range
   Dim Splt As Variant
   Dim i As Long
   
   For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
      Splt = Split(Cl.Value)
      For i = 0 To UBound(Splt) - 2
         Cl.Offset(, 1).Value = Trim(Cl.Offset(, 1).Value & " " & Splt(i))
      Next i
   Next Cl
End Sub
 
Upvote 0
Thank you friends. both solutions are working:)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Glad we could help & thanks for the feedback

hi Fluff,

would it be possible that changes occur in same column not in the adjacent column.

Look forward hearing from you. Thank you.
 
Upvote 0
Try
Code:
Sub Deepk()
   Dim Cl As Range
   Dim Splt As Variant
   Dim i As Long
   
   For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
      Splt = Split(Cl.Value)
      Cl.ClearContents
      For i = 0 To UBound(Splt) - 2
         Cl.Value = Trim(Cl.Value & " " & Splt(i))
      Next i
   Next Cl
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,823
Members
449,127
Latest member
Cyko

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