VBA with CHAR(1)

VBA_uk_85

New Member
Joined
Aug 23, 2015
Messages
13
Hi all.

I have a formula that works on a spreadsheet but char(1) isn't being recognised in VBA.

Worksheet C1 = RIGHT(C2,TRIM(FIND(CHAR(1),SUBSTITUTE(C2,"\",CHAR(1),LEN(C2)-LEN(SUBSTITUTE(C2,"\","")))))+1)
Worksheet C2 = C:\Users\Name\Documents\Work\laugh GIFs.xlsm
The result in C1 = laugh GIFs.xlsm

The idea is to extract the file name by finding the Nth occurrence of "\" and then use that position to use the Right function.

So in VBA i've got..
(bare in mind C2 is now a variant called FileNm)
Code:
[TABLE="width: 554"]
<tbody>[TR]
[TD="class: xl63, width: 554"]Name =  Right(FileNm, Trim(Application.WorksheetFunction.Find(char(1),worksheetFunction.Substitute(char(1), WorksheetFunction.Substitute(FileNm,  "\", char(1), Len(FileNm) -  Len(WorksheetFunction.Substitute(FileNm, "\", ""))))) +  1))[/TD]
[/TR]
</tbody>[/TABLE]

Any ideas??
Thanks in advance :)
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If you just want to return the last part after the backslash \ then try this

Rich (BB code):
Sub VBA_UK()
Dim str As Variant
str = Split(FileNm, "\")
Name = str(UBound(str))
End Sub
 
Last edited:
Upvote 0
If you just want to return the last part after the backslash \ then try this

Rich (BB code):
Sub VBA_UK()
Dim str As Variant
str = Split(FileNm, "\")
Name = str(UBound(str))
End Sub
You can do that with a single line of code by using InStrRev...

Name = Mid(Filename, InStrRev(FileName, "\") + 1)
 
Upvote 0

Forum statistics

Threads
1,218,280
Messages
6,141,503
Members
450,368
Latest member
Rowell Magnaye

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