有ABC.xls
文件有宏。現在,當我按Ctrl + M
時,該宏有一個被調用的子。 這個小組將打開一個打開文件對話框,用戶可以在其中選擇一個CSV
文件。所以基本上,這個宏用於處理和保存CSV
文件。 下面是按Ctrl + M
調用的代碼。防止關閉工作簿
Sub runProperChangeSubroutine() ' Assigned to shortcut key CTRL + M.
file_name = ActiveWorkbook.Name ' Gets the file name.
Application.Run file_name & "!ChangeSub"
End Sub
當用戶關閉工作簿,我要測試等每行中的CSV
具有終止字符串的條件。如果該終止字符串不是 ,我應該彈出一個消息框給用戶並阻止關閉工作簿。 所以我做了以下...
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim improperRowNumber As Integer
Dim BlnEventState As Boolean
improperRowNumber = returnImproperRow()
BlnEventState = Application.EnableEvents
Application.EnableEvents = True
If improperRowNumber <> -1 Then
Cancel = True
MsgBox "The row number " & improperRowNumber & " is not ending with '@/#'. Please correct the row before closing."
Else
Call saveCSV
End If
Application.EnableEvents = BlnEventState
End Sub
在接近(在右上角的X標誌,workboox。我不關閉Excel中。關閉)按鈕的點擊,我能夠看到消息但工作臺關閉。如果行沒有正確結束,我希望用戶進行一些編輯。 請建議。
被修改: 作爲上述代碼不工作,我消息框之後使用ThisWorkbook.Saved = False
如下所示...
If improperRowNumber <> -1 Then
MsgBox "The row number " & improperRowNumber & " is not ending with '@/#'. Please correct the row before closing."
Application.DisplayAlerts = False
ThisWorkbook.Saved = False
Cancel = False
Else
現在它顯示「Do you want to save the changes....
」消息框。如果我在消息框上單擊Cancel
按鈕,工作簿未關閉。
是否有自定義消息框文本或按鈕或隱藏此消息框的方法?
你不應該能夠與取消關閉該工作簿=真在Workbook_BeforeClose中。可能你在soubroutine improperRowNumber中有類似EnableEvents = False的東西? – MarcinSzaleniec
不,EnableEvents語句不存在於improperRowNumber子中。有沒有其他的可能性? – NJMR
@MarcinSzaleniec禁用了應用程序事件,msgbox不會顯示出來......但OP的處理程序顯然正在運行。 –