在同一文件夹中获取工作簿的文件名。

huangapple go评论62阅读模式
英文:

Get Filename of workbook in same folder

问题

I need to get the filename of a workbook in the same folder, but I'm running into problems using ThisWorkbook.Path to define the folder structure.

I've tried the below, but I get

Runtime Error 52 - Bad file name or number

Sub WorksheetImport()

Dim vWBFolder As String
Dim vWBName As String

vWBFolder = ThisWorkbook.Path
vWBName = Dir(vWBFolder & "*.csv*")

MsgBox vWBName

End Sub

It works if I define the folder manually e.g. - vWBFolder = "D:\\TestFolder\\"


The below answer is correct. Adding the \ should have fixed the issue.

Looks like my organization recently started backing up certain folders to OneDrive. This appears to have caused the error. Once the folder was excluded from OneDrive backup it worked as expected.

英文:

I need to get the filename of a workbook in the same folder, but I'm running into problems using ThisWorkbook.Path to define the folder structure.

I've tried the below, but I get

>Runtime Error 52 - Bad file name or number

Sub WorksheetImport()

Dim vWBFolder As String
Dim vWBName As String

vWBFolder = ThisWorkbook.Path
vWBName = Dir(vWBFolder & "*.csv*")

MsgBox vWBName

End Sub

It works if I define the folder manually e.g. - vWBFolder = "D:\\TestFolder\\"


The below answer is correct. Adding the \ should have fixed the issue.

Looks like my organization recently started backing up certain folders to OneDrive. This appears to have caused the error. Once the folder was excluded from OneDrive backup it worked as expected.

答案1

得分: 0

ThisWorkbook.Path末尾没有斜杠,所以您只需要添加一个斜杠:

vWBName = Dir(vWBFolder & "\" & "*.csv*")
英文:

ThisWorkbook.Path doesn't have a slash at the end of it, so you just need to add a slash:

vWBName = Dir(vWBFolder & "\" & "*.csv*")

答案2

得分: 0

请你可以这样使用:

Sub 工作表导入()

    Dim vWB文件夹 As String
    Dim vWB名称 As String
    
    ' 定义路径
    文件夹路径 = ThisWorkbook.Path & "\"
    
    ' 获取活动工作簿的路径
    文件夹路径 = Application.ActiveWorkbook.Path & "\"
    
    ' 获取文件夹中的第一个csv文件
    vWB名称 = Dir(文件夹路径 & "*.csv*")
    
    ' 弹出消息框显示文件名
    MsgBox vWB名称
    
End Sub
英文:
    Could you please use like this

Sub WorksheetImport()
    
    Dim vWBFolder As String
    Dim vWBName As String
    
    
    Path = ThisWorkbook.Path & "\"
    
    folderPath = Application.ActiveWorkbook.Path & "\"
    
    
    'vWBFolder = ThisWorkbook.Path
    vWBName = Dir(folderPath & "*.csv*")
    
    MsgBox vWBName
    
    End Sub

huangapple
  • 本文由 发表于 2023年3月7日 12:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定