如何在路径中获取最后一个文件夹时解决运行时错误9,下标超出范围

问题描述 投票:-1回答:1

尝试使用数组检索路径中的最后一个文件夹,它正在运行,但在脚本执行结束时返回运行时错误9,下标超出范围。 Screenshot of the line where error occuring`

Dim a As Variant
i = 5
For n = LBound(files) To UBound(files)
    a = Split(files(n), "\")
    FnameInLoop = a(UBound(a) - 1)
    Cells(i, 1).Value = FnameInLoop
    i = i + 1
    Next n`
arrays excel vba
1个回答
0
投票

@MathieuGuindon解决了,谢谢你的输入产生了成功的结果,只是添加了检查值是否为null,如下:

`' extract last folder in path
Dim a As Variant
i = 5
For n = LBound(files) To UBound(files)
    a = Split(files(n), "\")
    If Not Fnameinloop = "" Then
    Fnameinloop = a(UBound(a) - 1)
    Cells(i, 1).Value = Fnameinloop
    End If
    i = i + 1
    Next n
` 
© www.soinside.com 2019 - 2024. All rights reserved.