【问题标题】:Make directory, if it doesnt exist, from cell values and save workbook从单元格值创建目录(如果不存在)并保存工作簿
【发布时间】:2019-04-30 08:40:28
【问题描述】:

我正在尝试编写一个脚本,它从 2 个单元格中获取值,如果还没有目录,则创建一个目录,并使用另一个单元格的值保存文件。

我从这个线程中的代码开始:Save excel Workbook in a new created folder with the Same names 并将代码调整为我认为它们需要用于我的目的,但我是 VBA 的新手......我的代码现在看起来像这样:

Sub SAVE()
Dim strFilename, strDirname, strPathname, strDefpath, strYear As String
 On Error Resume Next ' If directory exist goto next line

strFilename = Range("B2").Value 'New file name
strDirname = Range("B3").Value ' New directory name
strYear = Range("B4").Value 'New year name
strDefpath = "P:\PPE\Urenverwerking" 'Default path name

If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub
If IsEmpty(strYear) Then Exit Sub

MkDir strDefpath & "\" & strYear & "\" & strDirname
strPathname = strDefpath & "\" & strYear & "\" & strDirname & "\" & strFilename 'create total string

ActiveWorkbook.SaveAs Filename:=strPathname & ".xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub

这导致代码正在运行(等待光标显示),但没有任何反应,而我希望在 P:\PPE\Urenverwerking\2019 中有一个新目录,其中包含一个名为单元格 B2 的文件。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我相信问题出在这里:

    MkDir strDefpath & "\" & strYear & "\" & strDirname
    

    您想创建一个子目录,而子目录本身甚至不存在。

    尝试以下方法:

    MkDir strDefpath & "\" & strYear
    MkDir strDefpath & "\" & strYear & "\" & strDirname
    

    【讨论】:

      猜你喜欢
      • 2013-02-27
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多