【发布时间】: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 的文件。
【问题讨论】: