VBA – 从string中提取最后一个单词,并从提取中创build新的string

我有select客户的这个function

Function GetFolder() As String Dim fldr As FileDialog Set fldr = Application.FileDialog(msoFileDialogFolderPicker) With fldr .Title = "Select" .AllowMultiSelect = False If .Show = 0 Then MsgBox ("Canceled") Exit Function Else: sItem = .SelectedItems(1) GoTo NextCode End If End With NextCode: GetFolder = sItem Set fldr = Nothing End Function 

用户select了宝马文件夹或丰田
所以sItem = "T:\DOCUMENTATION\BMW"
sItem = "T:\DOCUMENTATION\TOYOTA"

我需要从string( sItem )中提取哪个客户( BMWToyota ),并创build一个新的提取stringsItemCustomer

所以结果将是sItemCustomer = "BMW"

你能帮我做这个吗?

SPLIT是你需要的,或者更多的工作, INSTRINSTRREVMIDRIGHT

split(sItem,"\")(2)

要做到“正确”,

 Function pop(strInput As String, Optional strDelim As String = "\") As String Dim a() As String a = Split(strInput, strDelim) pop = a(UBound(a)) Erase a End Function 

正如Nathan_Sav所述,另一种方法是:

 sItemCustomer = Right(sItem, Len(sItem) - InStrRev(sItem, "\"))