excel vb 我想新建个文件夹,并在该文件夹下面新建一个文件,保存。老提示出错,请高手赐教,代码如下:
发布网友
发布时间:2022-05-31 13:02
我来回答
共2个回答
热心网友
时间:2023-10-15 13:22
正确答案:
Sub 新建文件夹_新建文件()
Dim sT As String
sT = Format(Time, "hh")
Workbooks.Add
If Dir(ThisWorkbook.Path & "\" & sT, vbDirectory) = "" Then
MkDir ThisWorkbook.Path & "\ " & sT
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\ " & sT & "\" & Format(Date, "yyyy-mm-dd") & ".xls"
End If
End Sub
错误的原因:
1、Format(Date, "year-mm-dd")的格式中“年”应该为Format(Date, "yyyy-mm-dd")
2、& " \ sT \ " & 中,sT是赋值的,这样写变为文本了,应该为"\ " & sT & "\" &
3、书写中有N多的空格,程序不会自动认识 MkDir ThisWorkbook.Path & "\ " & sT的& "\ " 有空格
热心网友
时间:2023-10-15 13:22
Dim q
q = WorksheetFunction.Text(Now, "yyyymmddhhmmss")
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\_bak." & q
Application.Quit