Trim content of a column

G

Guest

I have a worksheet that contains multiple columns. The first Column has a
file path.
I would like to trim the path to only display the file name.


For example:
A
1 C:/test/folder/File1
2 C:/test/folder/File2
3 C:/test/folder/File3
4 C:/test/folder/File4
5 C:/test/folder/File5

Would like to display:

A
1 File1
2 File2
3 File3
4 File4
5 File5
 
M

Myrna Larson

Is the path always the same (wishful thinking, I suppose!)? If so, just use
search and replace: Find What is the path, including final backslash, leave
Replace With box empty.
 
O

Otto Moehrbach

This little macro will do that. I assumed your data started in A1 and went
down. HTH Otto
Sub FindFileName()
Dim c As Long
Dim TheRng As Range
Dim i As Range
Set TheRng = Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each i In TheRng
For c = 1 To 50
If Not InStr(Right(i.Value, c), "\") = 0 Then Exit For
Next c
i.Value = Right(i.Value, c - 1)
Next i
End Sub
 
D

Dave Peterson

How about:

Select the range to fix
edit|replace
what: */
with: (leave blank)
replace all

Are you sure you have a slash and not a backslash?
Maybe you should replace *\ with (leave blank).
 
M

Myrna Larson

Hi, Otto:

I think the InstrRev function would eliminate the inner loop, i.e.

Sub FindFileName()
Dim c As Long
Dim TheRng As Range
Dim i As Range

Set TheRng = Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each i In TheRng
c = InstrRev(i.Value, "\")
If c <> 0 then i.value = Mid$(i.Value, c + 1)
Next i
End Sub
 
O

Otto Moehrbach

Myrna
Thank you for that. I have to show my ignorance and admit that I didn't
know about that function. Will this learning thing never end?? Otto
 
O

Otto Moehrbach

Myrna
If I may pick your brain for a minute, what is the function of the
dollar sign ($) after "Mid"? Thanks for your time. Otto
 
O

Otto Moehrbach

Dave
I just have to say, that is neat! Otto
Dave Peterson said:
How about:

Select the range to fix
edit|replace
what: */
with: (leave blank)
replace all

Are you sure you have a slash and not a backslash?
Maybe you should replace *\ with (leave blank).
 
C

Chip Pearson

The $ character causes the function to return a String rather than a
Variant. It results in a (very slight) performance improvement.
 
O

Otto Moehrbach

Thanks Chip. Otto
Chip Pearson said:
The $ character causes the function to return a String rather than a
Variant. It results in a (very slight) performance improvement.
 
D

Dave Peterson

I always do a save before I play with wildcards--just in case it doesn't do what
I want!
 
D

dan dungan

Hi Otto and Myrna,

I must confess my ignorance because when I stepped through either of
these procedures, I could not see the file name--only the complete
path.

When I ran them, nothing changed in the spreadsheet.

I put
C:/test/folder/File1 in A1
C:/test/folder/File2 in A2
C:/test/folder/File3 in A3
C:/test/folder/File4 in A4
C:/test/folder/File5 in A5

Please show me what I'm missing.
Thanks,
Dan
 
R

Roger Govier

Hi Dan

I think Otto used a back slash "\" in his code, which Myrna repeated.
You had a forward slash "/" in your example
Amend Myrna's code to use this and all will be well

Sub FindFileName()
Dim c As Long
Dim TheRng As Range
Dim i As Range

Set TheRng = Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each i In TheRng
c = InstrRev(i.Value, "/")
If c <> 0 then i.value = Mid$(i.Value, c + 1)
Next i
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top