excel vb 我想新建个文件夹,并在该文件夹下面新建一个文件,保存。老提示出错,请高手赐教,代码如下:
Sub新建文件夹新建文件()DimsTAsStringsT=Format(Time,"hh")Workbooks.AddIfDir(ThisWorkbook.Path&"...
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 展开
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 展开
2个回答
展开全部
正确答案:
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的& "\ " 有空格
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的& "\ " 有空格
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |