您好,我正在尝试创建一个将报告保存到特定文件夹的宏。我们的文件夹每天都会生成,例如C://Report_Type/2017/11/10
(其中/10
是我要保存文件的文件夹)。
代码我提示用户提供日期以便找到该文件夹,然后根据指定的名称保存文件。
然而,当我运行宏时,它会将文件保存在C://Report_Type/2017/11
根文件夹中,忽略基于userinput的日期。有人可以帮助我弄错了吗?
解释起来有点复杂,但是如果你检查代码那么它就有意义了。
Sub PSSaveFile()
Dim myVal2 As Variant
Dim myDate As String
Dim mFilePath As String
myVal2 = InputBox("Please enter today's date in mm\dd format")
myDate = Date - 1
mFilePath = "\\sample\sample_emea\sec_REPORTS\APPS\Reports\Regional\sample_security_app\2017\" & myVal2
ActiveWorkbook.SaveAs FileName:=mFilePath & "SampleLogs-" & myDate & "-12352_checked"
End Sub
提前感谢您的帮助!
答案 0 :(得分:3)
为什么要这么复杂?我的意思是为什么要问你可以自动获得的日期?
这是你正在尝试的( UNTESTED )?
Sub PSSaveFile()
Dim FilePath_A As String, FilePath_B As String, FilePath_C As String
Dim sFile As String
FilePath_A = "\\sample\sample_emea\sec_REPORTS\APPS\Reports\Regional\sample_security_app\2017\"
FilePath_B = Format(Date, "mm\dd")
FilePath_C = "\SampleLogs-" & Replace(Date - 1, "/", "-") & "-12352_checked.xlsx"
sFile = FilePath_A & FilePath_B & FilePath_C
ActiveWorkbook.SaveAs sFile, 51
End Sub
少数事情
\ / : * ? " < > |
之类的特殊字符,因此我们在上面的代码中使用Replace(Date - 1, "/", "-")
是的,这是一个更简单的方法,不幸的是,使用宏的用户经常检查积压,因此她审查的日期并不总是今天或昨天#39 ; s date因此用户输入功能 - Rhyfelwr 14分钟前
由于您使用的是InputBox,因此您可能需要使用此
Sub PSSaveFile()
Dim FilePath_A As String, FilePath_B As String, FilePath_C As String
Dim sFile As String
Dim Ret As Variant
Ret = InputBox("Please enter date in mm\dd format")
If Ret = "" Then Exit Sub
FilePath_A = "\\sample\sample_emea\sec_REPORTS\APPS\Reports\Regional\sample_security_app\2017\"
FilePath_B = Ret
'~~> Check if the folder path exists
If FileFolderExists(FilePath_A & FilePath_B) Then
FilePath_C = "\SampleLogs-" & Replace(Date - 1, "/", "-") & "-12352_checked.xlsx"
sFile = FilePath_A & FilePath_B & FilePath_C
ActiveWorkbook.SaveAs sFile, 51
Else
MsgBox "The folder path " & FilePath_A & FilePath_B & " doesn't exist"
End If
End Sub
Public Function FileFolderExists(strFullPath As String) As Boolean
On Error GoTo Whoa
If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
Whoa:
On Error GoTo 0
End Function