Deleting Portions of Data within a Cell

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Hello All...

I have an example, which I can't find any example of VBA to get a solution.
If I have a cell contents of: ACE-1D / F-R3. I'm trying to find code that will take away the "/F-R3" so just the "ACE-1D" remains.
This is one example, the next cell may have ACI-5 / 19...and so on.
So basically, I wish to keep everything to the left of the forward slash, and do away with everything else.
Thanks for the help
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
HTML:
For Each cl In Range("C7", Range("C" & Rows.Count).End(xlUp))
   cl.Value = Split(cl.Value, " /")(0)
Next cl

So getting back to this topic, the above is working well. However, every once-in-a-while, there is not a space in fromt of the "/".
So I'm messing around with an ElseIf statement:
Code:
For Each cl In Range("E7", Range("E" & Rows.Count).End(xlUp))
    ElseIf cl.Value = "/" Then
    cl.Value = " /"
        Else InStr(1, cl.Value, " /", 0) > 0 Then
        cl.Value = Split(cl.Value, " /")(0)
    End If
Next cl
My thought is I'd search each cl, and if there is not a space in front of the "/", then change it for a space, and continue with the code.
It's not working...
So where am I going wrong on an ElseIf?
Thanks for the help
 
Last edited:
Upvote 0
Try
Code:
For Each Cl In Range("E7", Range("E" & Rows.Count).End(xlUp))
   Cl.Value = Trim(Split(Cl.Value, "/")(0))
Next Cl
 
Upvote 0
Try
Code:
For Each Cl In Range("E7", Range("E" & Rows.Count).End(xlUp))
   Cl.Value = Trim(Split(Cl.Value, "/")(0))
Next Cl

Oh...your are suggesting two loops, one if the cl.value has a space, and another loop for if the cl.value does not have a space...correct?
 
Upvote 0
No, just that one loop.
It will split on the / & then uses trim to remove any trailing spaces.
 
Upvote 0
Try
Code:
For Each Cl In Range("E7", Range("E" & Rows.Count).End(xlUp))
   Cl.Value = Trim(Split(Cl.Value, "/")(0))
Next Cl

OK, this is working nicely, thank you for your time
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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