vba - 查找分配给主驱动器的字母

标签 vba excel

我正在尝试找到像 Environ 这样的函数来查找我的企业中的主驱动器已映射到特定 PC 上的驱动器。

使用文件路径“G:\Eworking\SET\Operations\file”我知道我的电脑已被映射,因此该文件路径位于 G 驱动器内,但其他文件路径可能会以不同方式映射,因此我想确定它是哪个.

我之前尝试过 if else 方法来遍历字母表并执行 if Dir([filepath]) then (见下文),但我想知道是否有更好的方法这个?

Sub LoopThroughDrives()
sFilePath As String

sFilePath = ":\Eworking\SET\Operations\file"

If Dir("A" & sFilePath) > 0 Then
    msgbox ("It's in drive A")
    Else
        If Dir("B" & sFilePath) > 0 Then
            msgbox ("It's in drive B")
            Else
                If Dir("C" & sFilePath) > 0 Then
                    msgbox ("It's in drive C")
                    Else
                        '...........................
                        'All other letters of the alphabet, checking each possibility
                        '...........................
                End If
        End If
End If

End Sub

最佳答案

您可以像这样获取驱动器号和路径:

For Each d In CreateObject("Scripting.FileSystemObject").Drives
    Debug.Print d.DriveLetter, d.Path, d.ShareName
Next

关于vba - 查找分配给主驱动器的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39210888/

相关文章:

excel - 我正在尝试使用来自事件表(VBA)的值创建数组

c# - 跟踪添加和删除的工作表

mysql - 从 csv 文件生成 UPDATE SQL 语句

excel - Excel中计算字符串前的空格数

excel - 使用 ADO 从 Excel 读取数据的空值

VBA:跳出深度嵌套的 If 语句

VBA清除所选段落的格式

python - 从工作簿复制列,粘贴到第二个工作簿的第二张工作表中,openPyXL

动态删除一系列 Excel 单元格中的空白

excel - 为什么我收到 'type mismatch' 错误?