2013-12-20 31 views
0

甚至出現我有下面的代碼有問題:MSGBOX當它不應該Excel VBA中

month = Me.monthcbg.Value 
year = Me.yrcbg.Value 

If Len(Dir("L:\NAI_Groups\IS Monthly Operations Meeting\" & year, vbDirectory)) = 0 Then 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year 
End If 

If Len(Dir("L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month, vbDirectory)) = 0 Then 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month & "\1_LOP" 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month & "\2_Plants_Operations" 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month & "\3_Logistics_Purchasing_IT" 
    MkDir "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month & "\4_Development_Changes" 
ElseIf Len(Dir("L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month, vbDirectory)) <> 0 Then 
    mes = MsgBox("A presentation already exists, would you like to overwrite the existing presentation?", vbYesNo, "Presentation Exists") 
     If mes = vbNo Then 
      Exit Sub 
     ElseIf mes = vbYes Then 
      On Error Resume Next 
      Kill "L:\NAI_Groups\IS Monthly Operations Meeting\" & year & "\" & month & "\1_LOP\" & month & "\" & year & ".pptx" 
      MsgBox "Old Presentation Deleted" 
      Set Error = 0 
     End If 
End If 

月,年的尺寸爲字符串。第一個問題是,即使沒有現有目錄,msgbox也會始終顯示,第二個問題是即使選擇了vbyes,代碼也不會創建該目錄。

感謝

+0

那麼它不會創建它,因爲它沒有開始。雖然我討厭這樣循環,但可以在vbYes if語句的末尾添加'GoTo 0'來啓動代碼。 – engineersmnky

+0

你不應該使用'month/year'作爲變量。它們是vba –

+1

中的保留名稱,第二個問題是即使選擇了vbyes,代碼也不會創建該目錄。「它不會創建它,因爲您沒有要求它。我沒有在那裏看到一個'MKDIR'聲明。 –

回答

0

我實際上沒有看到與代碼中的問題,但我已經洗乾淨了一下

Dim root_dir As String 
Dim sub_dir(3) AS String 

month_str = Me.monthcbg.Value 
year_str = Me.yrcbg.Value 

root_dir = "L:\NAI_Groups\IS Monthly Operations Meeting\" & year_str 
sub_dir(0) = "\1_LOP" 
sub_dir(1) = "\2_Plants_Operations" 
sub_dir(2) = "\3_Logistics_Purchasing_IT" 
sub_dir(3) = "\4_Development_Changes" 

If Len(Dir(root_dir, vbDirectory)) = 0 Then MkDir root_dir 


If Len(Dir(root_dir "\" & month_str, vbDirectory)) = 0 Then 
    MkDir root_dir & "\" & month_str 
    For i =0 to UBound(sub_dir) -1 
     MkDir root_dir & "\" & month_str & "\" & sub_dir(i) 
    Next i 
Else 
    mes = MsgBox("A presentation already exists, would you like to overwrite the existing presentation?", vbYesNo, "Presentation Exists") 
     If mes = vbNo Then 
      Exit Sub 
     ElseIf mes = vbYes Then 
      On Error Resume Next 
      Kill root_dir & "\" & month_str & "\1_LOP\" & month_str & "\" & year_str & ".pptx" 
      MsgBox "Old Presentation Deleted" 
      Set Error = 0 
      Goto 0 
     End If 
End If