提问人:Wallenbees 提问时间:10/10/2023 最后编辑:Wallenbees 更新时间:10/14/2023 访问量:80
MkDir 变量失败
MkDir Variable Failing
问:
我正在尝试构建一个工作簿,该工作簿可作为不同用户输入客户端数据的工具,并且我有一个功能可以检查路径是否存在,如果不存在,它将创建它。但是,当它到达代码中的 MkDir 命令时,它会给我一个运行时“76”错误。
我认为这可能与我错误地使用 Environ 变量有关。有人可以看看这个并告诉我我做了什么来打破它吗?- 这在下面解决了 - 但 Mkdir 部分仍然损坏
Dim a As Range
Dim StartingWS As Worksheet
Dim ClientFolder As String
Dim ClientCusip
Dim ExportFile As String
Dim PreparedDate As String
Dim Exports As String
Dim AccountNumber As String
Dim LR As Long
Dim NumOfBars As Integer
Dim PresentStatus As Integer
Dim PercetageCompleted As Integer
Dim k As Long
Dim sFolderPath As String
Dim oFSO As Object
Dim FindFolder As Object
Dim FindCAFolder As Object
Dim SCAFolderPath As String
Dim UserName As String
UserName = Environ("username")
Set StartingWS = ThisWorkbook.Sheets("Starting Page")
'******************* This code Creates the Class Action Folder ************
Set FindCAFolder = CreateObject("Scripting.FileSystemObject")
SCAFolderPath = "C:\Users\" & UserName & "\Desktop\Class Actions\"
If FindCAFolder.FolderExists(SCAFolderPath) Then
Else
MkDir SCAFolderPath
End If
'************* This code creates the folder and sets the export path for the individual spreadsheets**********
ClientCusip = ActiveWorkbook.Worksheets("Starting Page").Range("I11").Value
ClientFolder = ActiveWorkbook.Worksheets("Starting Page").Range("I10").Value
PreparedDate = Format(Now, "mm.yyyy")
Set FindFolder = CreateObject("Scripting.FileSystemObject")
sFolderPath = "C:\Users\" & UserName & "\Desktop\Class Actions\" & ClientFolder & " - " & PreparedDate & "\"
If FindFolder.FolderExists(sFolderPath) Then
Else
MkDir sFolderPath
End If
答:
1赞
Horaciux
10/10/2023
#1
缺少应包含驱动器号和用户文件夹的基本路径
请尝试使用此行,但要根据系统更改驱动器号和文件夹。
sFolderPath = "C:\Users\" & UserName & "\Desktop\Class Actions\" & ClientFolder & " - " & PreparedDate & "\"
0赞
Red Hare
10/11/2023
#2
无论如何,您都应该使用 MakeSureDirectoryPathExists 而不是 MKDir 如果目录不存在,这将创建该目录
Option Explicit
Private Declare PtrSafe Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
Sub TestIt()
Dim ok As Long
ok = MakeSureDirectoryPathExists("E:\zTemp\") ' Do your folder path here
If ok <> 0 Then
MsgBox "Hurray"
End If
End Sub
评论
0赞
Wallenbees
10/11/2023
我尝试使用以下命令使用它: ''' Pathway = MakeSureDirectoryPathExists(“C:\Users\” & UserName & “\OneDrive\Desktop\Class Actions\”) If Pathway <> 0 then else end if''' 但是由于某种原因,它一直在文档文件夹中创建它,并且不会创建类操作子文件夹。
0赞
Red Hare
10/11/2023
执行debug.print “C:\Users\” & UserName & “\OneDrive\Desktop\Class Actions\”以查看您的路径是否正确。您可以手动添加文件夹吗?是权利问题吗?我测试了 MakeSureDirectoryPathExists (“C:\Users\” & Environ(“username”) & “\OneDrive\Desktop\Class Actions\”) 并且它起作用了
0赞
Wallenbees
10/14/2023
#3
我最终找到了一条不同的道路来为我解决这个问题:
Dim oWSHShell As Object
Set oWSHShell = CreateObject("WScript.Shell")
GetDesktop = oWSHShell.SpecialFolders("Desktop")
Set oWSHShell = Nothing
Set FindFolder = CreateObject("Scripting.FileSystemObject")
sFolderPath = GetDesktop & "\Class Actions\"
If FindFolder.FolderExists(sFolderPath) Then
Else
MkDir sFolderPath
End If
公平地说,我认为 Red Hare 也是正确的,但我的用户个人资料中存在某种损坏,使它不适合我。如果您有这个问题,我希望这些解决方案之一有效。
评论
Environ("username")
不返回整个路径,只返回帐户名称。"C:\Users\" & UserName
Environ("USERPROFILE")
Class Actions
Activeworkbook...
StartingWS
fso