Excel Crashes with this Code

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I'd be grateful if someone could look at this code and let me know if you could see why it is that when I run it, Excel crashes and then reopens:

Code:
Sub btnAddNewCustomer_Click()
If ValidateMandatoryFields() Then
    AddNewCustomer
    Unload Me
End If
End Sub

Private Function ValidateMandatoryFields() As Boolean
    If Trim(Me.txtNewCustomer.Value) = "" Or _
    Trim(Me.txtNewAddress1.Value) = "" Or _
    Trim(Me.txtNewAddress2.Value) = "" Or _
    Trim(Me.txtNewEmail.Value) = "" Or _
    Trim(Me.txtNewPhone.Value) = "" Or _
    Trim(Me.txtNewContactName.Value) = "" Then
MsgBox "Please complete all mandatory fields.", vbExclamation, "Missing Information"
    ValidateMandatoryFields = False
Else
    ValidateMandatoryFields = True
End If
End Function

Sub AddNewCustomer()
Dim ws As Worksheet
Dim tbl As ListObject
Dim newRow As ListRow
Dim response As VbMsgBoxResult
On Error GoTo ErrHandler
    Set ws = ThisWorkbook.Sheets("Customers")
    Set tbl = ws.ListObjects("Customer_List")
    Set newRow = tbl.ListRows.Add
        newRow.Range.Cells(1, tbl.ListColumns("Customer Name").Index).Value = Me.txtNewCustomer.Value
        newRow.Range.Cells(1, tbl.ListColumns("Address 1").Index).Value = Me.txtNewAddress1.Value
        newRow.Range.Cells(1, tbl.ListColumns("Address 2").Index).Value = Me.txtNewAddress2.Value
        newRow.Range.Cells(1, tbl.ListColumns("Email").Index).Value = Me.txtNewEmail.Value
        newRow.Range.Cells(1, tbl.ListColumns("Phone").Index).Value = Me.txtNewPhone.Value
        newRow.Range.Cells(1, tbl.ListColumns("Contact Name").Index).Value = Me.txtNewContactName.Value
        newRow.Range.Cells(1, tbl.ListColumns("Project").Index).Value = Me.txtNewProject.Value
response = MsgBox("Do you wish to add more Customers?", vbQuestion + vbYesNo)
    If response = vbYes Then
        Me.txtNewCustomer.Value = ""
        Me.txtNewAddress1.Value = ""
        Me.txtNewAddress2.Value = ""
        Me.txtNewEmail.Value = ""
        Me.txtNewPhone.Value = ""
        Me.txtNewContactName.Value = ""
        Me.txtNewProject.Value = ""
ElseIf response = vbNo Then
    Unload Me
End If
Exit Sub
ErrHandler:
    MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
End Sub

Any suggestions gratefully received
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I have no problem with your code.
Maybe your sheet has some damage.
I recommend starting in a new book, on a new sheet.
Just copy your table headers and paste them as value and create your "Customer_List" table again on the new sheet.
--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 1
Solution
I have no problem with your code.
Maybe your sheet has some damage.
I recommend starting in a new book, on a new sheet.
Just copy your table headers and paste them as value and create your "Customer_List" table again on the new sheet.
--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
Many thanks for your response
I have rebuilt the page now and it works fine
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top