Hi All,
I'm fairly new to VBA, and have been struggling with this code. When I input a NEW [Company] in my form I want the name to be added the bottom of the 'Customers' table. A single column table @ Data!A2:A12
Everything works great until it tries to actually write the value into the cell. When it tries, I get "Method 'Value' of object 'Range' failed". and when I either End or Debug excel crashes.
If I choose an existing [Company] in my form and just add new contact info then I have no issue.
Code where I get the bug
Full code
I'm fairly new to VBA, and have been struggling with this code. When I input a NEW [Company] in my form I want the name to be added the bottom of the 'Customers' table. A single column table @ Data!A2:A12
Everything works great until it tries to actually write the value into the cell. When it tries, I get "Method 'Value' of object 'Range' failed". and when I either End or Debug excel crashes.
If I choose an existing [Company] in my form and just add new contact info then I have no issue.
Code where I get the bug
VBA Code:
'Check to see if the Company already exists, if it does NOT, add it to the company table
Set r = wsData.ListObjects("Customers").Range
Set rngCompany = r.Find(strCompany, LookIn:=xlValues)
If rngCompany Is Nothing Then
iRow2 = wsData.ListObjects("Customers").Range.Columns(1).Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
Cells(iRow2, 1).Value = cmbCompany.Value
End If
End Sub
Full code
VBA Code:
Private Sub cmdSubmit_Click()
'Add new customer to the customer table
Dim iRow, iRow2, iAnswer As Integer
Dim strContact, strCompany As String
Dim wsData As Worksheet
Dim rngCompany, r As Range
Set wsData = Worksheets("Data")
Application.ScreenUpdating = False
Sheets("data").Activate
'Find last row in the contacts table
iRow = wsData.ListObjects("Contacts").Range.Columns(1).Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
'Copy data from the form to the contacts table
Cells(iRow, 3).Value = cmbCompany.Value
Cells(iRow, 4).Value = txtContact.Value
Cells(iRow, 5).Value = txtAddress.Value
Cells(iRow, 6).Value = txtCity.Value
Cells(iRow, 7).Value = txtState.Value
Cells(iRow, 8).Value = txtZip.Value
Cells(iRow, 9).Value = txtPhone.Value
Cells(iRow, 10).Value = txtEmail1.Value
Cells(iRow, 11).Value = txtEmail2.Value
'Set some variables for later use
strContact = Cells(iRow, 4).Value
strCompany = Cells(iRow, 3).Value
'Check to see if the Company already exists, if it does NOT, add it to the company table
Set r = wsData.ListObjects("Customers").Range
Set rngCompany = r.Find(strCompany, LookIn:=xlValues)
If rngCompany Is Nothing Then
iRow2 = wsData.ListObjects("Customers").Range.Columns(1).Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
[B]Cells(iRow2, 1).Value = cmbCompany.Value[/B]
End If
'Check with user to see if we want to add another contact
iAnswer = MsgBox(strContact & " has been successfully added to " & strCompany & ". Would you like to add another?", _
vbQuestion + vbYesNo + vbDefaultButton2, "Success")
If iAnswer = vbYes Then
Call UserForm_Initialize
GoTo ImDone
Else
Unload Me
End If
'Sort the contacts table
wsData.ListObjects("Contacts").Sort.SortFields.Clear
wsData.ListObjects("Contacts").Sort.SortFields.Add2 _
Key:=Range("Contacts[[#All],[Company]]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Data").ListObjects("Contacts").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("Quotation").Activate
Application.ScreenUpdating = True
ImDone:
End Sub
Last edited: