英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论