2012-01-19 88 views
0

當出現錯誤時,我想返回到我的用戶表單並更改表單中輸入的信息並將其傳遞到宏,然後繼續:如果Len(Dir( sFilePath & newfol,vbDirectory))= 0 ...VBA - Excel - On Error goto userform

If Len(Dir(sFilePath & newfol, vbDirectory)) = 0 Then 
      MkDir sFilePath & newfol & "\" 
      On Error GoTo UserForm1 
     Else 
      MsgBox ("Folder already exists please re enter new folder name") 
      'End 
      UserForm1.Show 
      MoveAndSave_Reports 
End If 

上面給我的錯誤信息:當您使用「標籤上沒有 「對錯誤轉到UserForm1」

回答

3

定義:編譯錯誤錯誤轉到「語句,您告訴程序,當它遇到錯誤時,跳到當前過程中的特定行。例如:

On Error GoTo ErrorHandler 
x = 10/0 
msgbox "x = infinity!" 

ErrorHandler: 
msgbox "Cannot divide by zero" 

在這個例子中,當代碼擊中錯誤(在我的「上的錯誤」語句),它將停止它在做什麼,並開始在的ErrorHandler標籤執行代碼(這是一個標籤,因爲最後的冒號)。運行這段代碼,你永遠不會看到消息框中顯示x = infinity。通過嘗試除以零來達到錯誤後,它將跳到ErrorHandler標籤並給我一條消息,說我不能被零除。

Chip Pearson對基本錯誤處理有很好的介紹here

+0

作爲Craig所說的附加內容,製作一個標籤,然後編寫一些代碼來調用用戶表單並完成需要完成的工作。 – 2012-01-19 03:28:43