从VBA中的完整文件名中提取路径 [英] Extract the path from a full file-name in VBA

查看:2190
本文介绍了从VBA中的完整文件名中提取路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我在VBA中新增了下面的代码,我的代码不起作用, Dim nPath1()As String 
nPath1()= Split(nPath,\)

'以整数形式声明路径
Dim path As Integer
'获取数组长度
path = UBound(nPath1())
Dim lastname As String
'For Loop
For i = 0 To path-1
lastname + = nPath1 i)+\
Next i

上面的代码不工作;我的路径字符串是 Root\zTrash - 不再需要\NOC\NOC ,我想要的是 Root\zTrash - 不再需要\NOC 。 / p>

解决方案

如果要从路径中删除最后一个项目,可以这样做:

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




  • InStrRev 找到最后一次出现的位置 \


  • Left 截断字符串直到该位置


  • -1 是因为您还要删除最后一个 \



I m new in VBA and below is my code which is not working, can any one of u can help?

Dim nPath1() As String
nPath1() = Split(nPath, "\")       

'Declare path as integer
 Dim path As Integer
'Getting array length
 path = UBound(nPath1())
 Dim lastname As String
 'For Loop
 For i = 0 To path-1
     lastname += nPath1(i)+"\"
 Next i

The above code is not working; my path string is Root\zTrash - No longer needed\NOC\NOC and what I want is Root\zTrash - No longer needed\NOC.

解决方案

If you want to remove just the last item from your path, you can do it this way:

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

  • InStrRev finds the position of the last occurrence of \

  • Left truncates the string until that position

  • The -1 is because you want also to remove that last \

这篇关于从VBA中的完整文件名中提取路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆