2013-12-20 4 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

Ну это не создает, потому что это не начало. Хотя я ненавижу цикл таким образом, вы можете добавить 'GoTo 0' в конце инструкции vbYes if, чтобы запустить код. – 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 
Смежные вопросы