Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro-Enabled Template - SaveAs .csv - Quit/Close results in Type Mismatch Error

Status
Not open for further replies.

ryplew

Technical User
Jun 3, 2015
18
US
I have a command button inside of a macro-enabled template that when clicked, does some events, saves as a .csv file, then quits/closes. I am receiving a Type Mismatch error. I've removed all extraneous code, leaving only the portions that I see as being responsible for the error. the file saves successfully, then the error pops up. Once you click Debug on the error, the editor flashes on the screen for a moment, then Excel closed. Any thoughts?

Code:
Sub cmdProcess_click()

    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:="\\v2\imports\CUPRC Templates\test - " & Format(Time, "h-mm-ssampm"), FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbook.Saved = True
    Application.DisplayAlerts = True
    
    If Workbooks.Count < 2 Then
        Application.Quit
        Exit Sub
        Else
        ActiveWorkbook.Close
        Exit Sub
    End If

End Sub
 
Do you have any other event procedures that are fired when you close workbook/application?
Change VBE settings, general tab, to "break on all errors" or "break in class module". This will allow to mark a line with error.
Add breakpoints in the lines that terninate the code (Application.Quit, ActiveWorkbook.Close), when the code breaks, debug line by line.
If ActiveWorkbook is the workbook with code, you could replace it by ThisWorkbook.

combo
 
No other procedures. My example above, I've removed everything that I could without affecting the problem.

I did a couple more tests this morning and may have found the cause. It seems I was receiving this error upon pressing the command button while still having a cursor inside of a cell (the cell being active or open - not sure what this is called). However, if after typing in a cell, I then click off of that cell, then click the command button, the error does not occur.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top