excel vb 我想新建个文件夹,并在该文件夹下面新建一个文件,保存。老提示出错,请高手赐教,代码如下:

Sub 新建文件夹 新建文件()
Dim sT As String
sT = Format(Time, "hh")
Workbooks.Add
If Dir(ThisWorkbook.Path & "\" & sT, vbDirectory) = "" Then
MkDir ThisWorkbook.Path & "\ " & sT
ActiveWorkbook.SaveAs ThisWorkbook.Path & " \ sT \ " & Format(Date, "year-mm-dd") & ".xlsx"
End If

End Sub

正确答案:
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的& "\ " 有空格
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-28
Dim q
q = WorksheetFunction.Text(Now, "yyyymmddhhmmss")
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\_bak." & q
Application.Quit
相似回答