简体   繁体   中英

Find the position of the last specified character in a string in excel vba

我想知道以下代码中最后一个“ \\”的位置:

Application.ActiveWorkbook.Path

You use INSTR to find the location of one string within the other.

For example:
C:\\MyPC\\SomeOtherPath - the backslash is in position 3 as shown in this code.
INSTR("C:\\MyPC\\SomeOtherPath", "\\")

This doesn't give you what you want though - you want the reverse of this. Luckily VBA gives that with INSTRREV :

INSTRREV("C:\\MyPC\\SomeOtherPath", "\\")

This returns the position of the last backslash - in position 8.

You can use this code:

Path = Application.ActiveWorkbook.Path
CharacterSearch = "\"
x = InStr(Path, CharacterSearch)
Do Until InStr(x + 1, Path, CharacterSearch) = 0
x = InStr(x + 1, Path, CharacterSearch)
Loop
Debug.Print x

Where you loop to find all \\ until InStr is 0, ie,until the last backlash is found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM