excel MkDir变量失败

jfgube3f  于 2023-10-22  发布在  其他
关注(0)|答案(3)|浏览(107)

我试图构建一个工作簿,作为不同用户输入客户端数据的工具,我有一个检查路径是否存在的函数,如果不存在,它将创建它。然而,当它到达代码中的MkDir命令时,它给予我一个运行时“76”错误。
我认为这可能与我错误地使用环境变量有关。有谁能看看这个,告诉我我做了什么把它弄坏了?-这在下面解决了-但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
1tuwyuhd

1tuwyuhd1#

您丢失了应该包括驱动器号和用户文件夹的基本路径
请尝试这一行,但根据您的系统相应地更改驱动器号和文件夹。

sFolderPath = "C:\Users\" & UserName & "\Desktop\Class Actions\" & ClientFolder & " - " & PreparedDate & "\"
xjreopfe

xjreopfe2#

而不是MKDir你应该无论如何使用MakeSureDirectoryPathLists这将创建目录,如果它不存在

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
wz8daaqr

wz8daaqr3#

我最终找到了一条不同的道路,为我解决了这个问题:
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也是正确的,但我的用户配置文件中存在某种腐败,这对我不起作用。如果你有这个问题,我希望这些解决方案之一工作。

相关问题