2017-02-28 23 views
4

刪除最後一個名字,我在VBA是新我正在下面的任務如何從一個路徑VBA

nPath = "Root\zTrash - No longer needed\NOC\NOC" 

我想從nPath刪除\NOC因此只有Root\zTrash - No longer needed應顯示。

我使用這個代碼:

=(Left(nPath, InStrRev(nPath, "\") - 1)) 

,但我發現只有NOC

+0

順便說一句,你的標題是不正確的,因爲它說*「去掉最後一個元素從一個路徑「*,它似乎是一個確切的副本http://stackoverflow.com/questions/42462625/how-to-remove-the-last-element-from-a-path-in-vba/42462687 #42462687 –

+0

[如何從VBA中的路徑中刪除最後一個元素]幾乎完全重複(http://stackoverflow.com/questions/42462625/how-to-remove-the-last-element-from-a-path -in-VBA) –

回答

2

這個怎麼樣?

Left(nPath, InStr(nPath, "\NOC") - 1) 


您正在使用 InStrRev找到 最後 \ - 這不是你想要的。使用 InStr\NOC找到 \NOC

1

第一實例可以做到在兩個階段:

Dim newString as String 
newString = Left(nPath, InStrRev(nPath, "\") - 1) 
newString = Left(newString , InStrRev(newString , "\") - 1) 
相關問題