Removing selected text from a cell

excel_2009

Active Member
Joined
Sep 14, 2009
Messages
318
Hi excel gurus,

I was wondering if someone could help me

i have vast amounts of cells that contain information similar to the following (but the structure of the cells remains the same) e.g:

Books/Subjects/Children's Books/Harry Potter
Books/Subjects/Fiction Books/ Top Gear

All i want to do is simply delete anything after the 3rd "/" so that i end up with the following:

Books/Subjects/Children's Books/
Books/Subjects/Fiction Books/

(keeping in mind there are vast amounts of "subjects" e.g history/science etc i.e. it's not just for the above two cells)

Can someone please help me :)??

Thank you
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
assuming data starts in A1, try the following

=LEFT(A1,FIND("~",SUBSTITUTE(A1,"/","~",3)))
 
Upvote 0
excel_2009,


Sample data before the macro:


Excel Workbook
AB
1Books/Subjects/Children's Books/Harry Potter
2Books/Subjects/Fiction Books/ Top Gear
3
Sheet1





After the macro:


Excel Workbook
AB
1Books/Subjects/Children's Books/Harry PotterBooks/Subjects/Children's Books/
2Books/Subjects/Fiction Books/ Top GearBooks/Subjects/Fiction Books/
3
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub DeleteAfter3()
' hiker95, 09/06/2011
' http://www.mrexcel.com/forum/showthread.php?t=576837
Dim A(), r As Long, Sp, s As Long, H As String
A = Range("A1").CurrentRegion.Resize(, 1)
For r = 1 To UBound(A)
  If InStr(A(r, 1), "/") > 2 Then
    Sp = Split(A(r, 1), "/")
    H = ""
    For s = LBound(Sp) To 2
      H = H & Sp(s) & "/"
    Next s
    A(r, 1) = H
  End If
Next r
Range("B1:B" & UBound(A)) = A
Columns(2).AutoFit
End Sub


Then run the DeleteAfter3 macro.
 
Upvote 0
excel_2009,


Slight update to the macro (same screenshots as above):


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub DeleteAfter3()
' hiker95, 09/06/2011
' http://www.mrexcel.com/forum/showthread.php?t=576837
Dim A(), r As Long, Sp, s As Long, H As String
A = Range("A1").CurrentRegion.Resize(, 1)
For r = 1 To UBound(A)
  If Len(A(r, 1)) - Len(Application.Substitute(A(r, 1), "/", "")) > 2 Then
    Sp = Split(A(r, 1), "/")
    H = ""
    For s = LBound(Sp) To 2
      H = H & Sp(s) & "/"
    Next s
    A(r, 1) = H
  End If
Next r
Range("B1:B" & UBound(A)) = A
Columns(2).AutoFit
End Sub


Then run the updated DeleteAfter3 macro.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,901
Members
452,948
Latest member
Dupuhini

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