VBA to delete text before dash

pltaine

New Member
Joined
May 9, 2018
Messages
8
I am trying to trim a lot of data from the form "10am-6pm" and "11.30am-9.30pm" to "6pm" and "9.30pm".

I basically just need the finish time (I already have a macro sorted to trim for the start times). However I cannot work out why my current code isn't working.

So far I have:

Sub LeaveFinishTime()
Dim c As Range
For Each c In Range("RotaSpaceFinish")
If InStr(c.Value, "-") > 0 Then
c.Value = Right(c.Value, (Len(c.Value) - Left(c.Value, InStr(c.Value, "-"))))
End If
Next c

End Sub

I think the logic I have written is to show characters starting from the right, as many characters as the total cell contents minus the number of characters up to and including the dash, starting from the right.

Could someone point out the mistake I have here?

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
Code:
Sub LeaveFinishTime()
   Dim c As Range
   For Each c In Range("RotaSpaceFinish")
      If InStr(c.Value, "-") > 0 Then c.Value = Split(c.Value, "-")(1)
   Next c
End Sub
 
Upvote 0
How about
Code:
Sub LeaveFinishTime()
   Dim c As Range
   For Each c In Range("RotaSpaceFinish")
      If InStr(c.Value, "-") > 0 Then c.Value = Split(c.Value, "-")(1)
   Next c
End Sub

Thanks! This worked very well. What does the 2nd bracket "(1)" mean?
 
Upvote 0
It's the 2nd value in the array. Split starts the arrays at index 0.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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