VBA to leave only content in string after last “/“ character.

dugweje

New Member
Joined
Feb 28, 2022
Messages
5
Office Version
  1. 2016
Hello.

I have an excel file I download that has columns with cell contents that look like this: /great/chicken/sautéed/AA41. I want only the “AA41” left with everything beforehand deleted. The columns are H:1-H:2000. For extra reference there are always 9 “/“ characters so it would always be the only content after the 9th “/“ that is wanted. Thank you.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
You can use Flash Fill functionality to get the last substring.

1. In Cell I1, type AA41
2. Goto Cell I2.
3. Press Ctrl+E
4. All following cells will filled with required substring.

Book1
ABCDEFGHIJ
1/great/chicken/sautéed/AA41AA41
2/actual/chicken/sautéed/AA4215AA4215
3/great/word/sautéed/AA43AA43
4/good/mask/sautéed/AA44AA44
5/great/chicken/sautéed/AA45AA45
6/great/chicken/sautéed/AA46AA46
7/great/chicken/sautéed/AA47AA47
8/great/chicken/saut/AA48AA48
9/great/chicken/sautéed/AA49AA49
10/great/chicken/sautéed/AA50AA50
11/great/chicken/sautéed/AA51AA51
12/great/chicken/sautéed/AA52AA52
13/great/chicken/sautéed/BCCBCC
14/great/chicken/camel/AA54AA54
15/great/chicken/sautéed/AA55AA55
16/great/chicken/sautéed/AA56AA56
17/great/chicken/sautéed/AA57AA57
18/great/chicken/sautéed/AA58AA58
19/great/chicken/sautéed/AA59AA59
20/great/chicken/sautéed/specialspecial
21/great/chicken/sautéed/AA61AA61
22/great/chicken/sautéed/AA62AA62
23/great/chicken/sautéed/AA63AA63
24/great/chicken/sautéed/AA64AA64
25/great/chicken/sautéed/AA65AA65
26/great/chicken/sautéed/AA66AA66
27/great/chicken/sautéed/AA67AA67
28/great/chicken/sautéed/AA68AA68
29
30
31
Sheet1


You can check this to learn Flash Fill.
 
Upvote 0
You can use Flash Fill functionality to get the last substring.

1. In Cell I1, type AA41
2. Goto Cell I2.
3. Press Ctrl+E
4. All following cells will filled with required substring.

Book1
ABCDEFGHIJ
1/great/chicken/sautéed/AA41AA41
2/actual/chicken/sautéed/AA4215AA4215
3/great/word/sautéed/AA43AA43
4/good/mask/sautéed/AA44AA44
5/great/chicken/sautéed/AA45AA45
6/great/chicken/sautéed/AA46AA46
7/great/chicken/sautéed/AA47AA47
8/great/chicken/saut/AA48AA48
9/great/chicken/sautéed/AA49AA49
10/great/chicken/sautéed/AA50AA50
11/great/chicken/sautéed/AA51AA51
12/great/chicken/sautéed/AA52AA52
13/great/chicken/sautéed/BCCBCC
14/great/chicken/camel/AA54AA54
15/great/chicken/sautéed/AA55AA55
16/great/chicken/sautéed/AA56AA56
17/great/chicken/sautéed/AA57AA57
18/great/chicken/sautéed/AA58AA58
19/great/chicken/sautéed/AA59AA59
20/great/chicken/sautéed/specialspecial
21/great/chicken/sautéed/AA61AA61
22/great/chicken/sautéed/AA62AA62
23/great/chicken/sautéed/AA63AA63
24/great/chicken/sautéed/AA64AA64
25/great/chicken/sautéed/AA65AA65
26/great/chicken/sautéed/AA66AA66
27/great/chicken/sautéed/AA67AA67
28/great/chicken/sautéed/AA68AA68
29
30
31
Sheet1


You can check this to learn Flash Fill.
Thank you but I prefer a macro/VBA to do this. Plus I don’t want to make another column I only want to edit the H column to include the last string.
 
Upvote 0
Or:
I1 then drag down:
Code:
=TRIM(RIGHT(SUBSTITUTE(H1,"/",REPT(" ",100)),100))
 
Upvote 0
Hi,

Check below VBA:

VBA Code:
Sub extractData()
Application.ScreenUpdating = False
Dim rowno As Integer
For rowno = 1 To ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row
    ActiveSheet.Range("H" & rowno) = Mid(ActiveSheet.Range("H" & rowno), InStr(1, WorksheetFunction.Substitute(ActiveSheet.Range("H" & rowno), "/", "%", 4), "%") + 1, 99)
Next
Application.ScreenUpdating = true
End Sub
 
Upvote 0
Assuming the criteria are met:
The columns are H:1-H:2000. For extra reference there are always 9 “/“ characters so it would always be the only content after the 9th “/“ that is wanted.
try:
VBA Code:
Sub dugweje()
Dim c As Range
Application.ScreenUpdating = False
For Each c In Range("H1:H2000")
    On Error Resume Next
    c = Split(c, "/")(9)
    On Error GoTo 0
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
How about:

VBA Code:
Sub TestEndofString()
'
    Application.ScreenUpdating = False
'
    Dim ArrayRow        As Long
    Dim Column_H_Array  As Variant
'
    Column_H_Array = Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row)
'
    For ArrayRow = 1 To UBound(Column_H_Array)
        Column_H_Array(ArrayRow, 1) = Right(Column_H_Array(ArrayRow, 1), Len(Column_H_Array(ArrayRow, 1)) - InStrRev(Column_H_Array(ArrayRow, 1), "/"))
    Next
'
    Range("H1:H" & Range("H" & Rows.Count).End(xlUp).Row) = Column_H_Array
'
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
I want only the “AA41” left with everything beforehand deleted.
I don’t want to make another column I only want to edit the H column to include the last string.
Try this with a copy of your data.,

VBA Code:
Sub LastBit()
  Range("H1:H2000").Replace "*/", "", xlPart
End Sub
 
Upvote 0
This worked perfectly! thank you so much.
Assuming the criteria are met:

try:
VBA Code:
Sub dugweje()
Dim c As Range
Application.ScreenUpdating = False
For Each c In Range("H1:H2000")
    On Error Resume Next
    c = Split(c, "/")(9)
    On Error GoTo 0
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0
This worked perfectly!
I agree, but did wonder if you investigated the option that processing them all at once in a single code line instead of cycling through one at a time? :)
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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