excel - 根据用户选择将变量设置为子文件夹名称
问题描述
我的代码提示用户选择一个文件。我想将变量设置为文件位置的文件夹名称,但文件位于子文件夹中。
我有这个代码来打开文件。
fileAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv), _
*.csv", Title:="Select a file")
If fileAndPath = False Then Exit Sub
这是文件路径 C:\Store Location\Employees\Contact Information\Phone Numbers\11373
我想提取 11373 部分并将其存储为变量
解决方案
好的,所以你想为 getOpenFileName 设置一个默认文件夹。在 getOpenFileName 之前加上ChDir "D:\Test"
. 如果驱动器不是 C:,则在 ChDir 前面加上 ChDrive "DriveLetter:"
Sub test()
ChDrive "D:"
Application.DefaultFilePath = "D:\Test"
fileAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.csv),*.csv", Title:="Select a file")
If fileAndPath = False Then
Exit Sub
End If
folderPath = Left(fileAndPath, InStrRev(fileAndPath, "\") - 1)
MsgBox (Mid(folderPath, InStrRev(folderPath, "\") + 1))
End Sub
推荐阅读
- ios - 无法自动合成 'Encodable',因为 'Error' 不符合 'Encodable'
- selenium-chromedriver - 在调用 FindElement 之前确定 Angular 是否存在
- apache-spark - 结构化流式传输后,ORC 文件只能通过文件夹中的通配符读取
- sql - 有没有办法从具有随机值的表列创建范围
- java - 在具有两个对象的数组中,如何在 (1) 处获得越界异常?
- php - 如何从 Google People API 检索属于特定组或标签的联系人列表
- postman - 如何使用“to.be”在邮递员中使用响应断言?
- python - 如何在循环中不断更新张量的值
- java - 我正在使用 json 从数据库中获取本地日期时间
- mocha.js - 创建 mocha 测试套件以启动应用程序一次