路径名中的变量和获取路径未找到错误

时间:2013-01-02 18:38:58

标签: excel vba

我把头发拉了出来。 mkdir与年份(整数)一起使用,但不添加月份(字符串)。

year = 2013
month = MonthName(1)

'I have also tried just putting "January" in there as well

这有效:

Path1 = "\\TEST\" & year & "\" 

但这不是:

Path1 = "\\TEST\" & year & "\" & month & "\"

连连呢?谢谢!

最终修复:

将Path1拆分为:

Path1: "\\TEST\" & year & "\"

Path2:  "\\TEST\" & year & "\" & month & "\"

检查:

If Len(Dir(Path1, vbDirectory)) = 0 Then
        MkDir Path1
End If
If Len(Dir(Path2, vbDirectory)) = 0 Then
        MkDir Path2
End If

1 个答案:

答案 0 :(得分:2)

除非"\\TEST\" & year & "\" & month & "\"已存在,否则无法通过MkDir创建"\\TEST\" & year & "\"

扩展您发布的代码,您可以这样做:

Path1 = "\\TEST\" & year & "\"
If DIR$(Path1, vbDirectory) <> "" then mkdir Path1
Path1 = "\\TEST\" & year & "\" & month & "\"
If DIR$(Path1, vbDirectory) <> "" then mkdir Path1