Excel has started crashing without any explanation and the code has worked previously so I am not exactly sure why it is constantly crashing now. It isn't even that much code or very complex. I am simply adding new values to a table in two separate worksheets. Any help would be greatly appreciated!
Sheet 1:
Option Explicit
Private Sub CommandButton1_Click()
Dim nameStr As String
nameStr = TextBox1.Value
Dim costInt As Integer
costInt = TextBox2.Value
Dim newRateValues(8)
newRateValues(0) = nameStr
newRateValues(1) = costInt
newRateValues(2) = costInt
newRateValues(3) = costInt
newRateValues(4) = costInt
newRateValues(5) = costInt
newRateValues(6) = costInt
newRateValues(7) = costInt
newRateValues(8) = costInt
AddDataRow "ratesTable", newRateValues
AddDataRow2 "tableResources"
End Sub
Module:
Option Explicit
Sub AddDataRow(tableName As String, values() As Variant)
Dim sheet As Worksheet
Dim table As ListObject
Dim col As Integer
Dim lastRow As Range
Set sheet = ActiveWorkbook.Worksheets("Sheet1")
Set table = sheet.ListObjects.Item(tableName)
'First check if the last row is empty; if not, add a row
If table.ListRows.count > 0 Then
Set lastRow = table.ListRows(table.ListRows.count).Range
For col = 1 To lastRow.Columns.count
If Trim(CStr(lastRow.Cells(1, col).Value)) <> "" Then
table.ListRows.Add
Exit For
End If
Next col
Else
table.ListRows.Add
End If
'Iterate through the last row and populate it with the entries from values()
Set lastRow = table.ListRows(table.ListRows.count).Range
For col = 1 To lastRow.Columns.count
If col <= UBound(values) + 1 Then lastRow.Cells(1, col) = values(col - 1)
Next col
End Sub
Sub AddDataRow2(tableName As String)
Dim sheet As Worksheet
Dim table As ListObject
Dim col As Integer
Dim lastRow As Range
Dim newRate As Integer
Set sheet = ActiveWorkbook.Worksheets("Sheet2")
Set table = sheet.ListObjects.Item(tableName)
newRate = ActiveSheet.TextBox1.Text
'First check if the last row is empty; if not, add a row
If table.ListRows.count > 0 Then
Set lastRow = table.ListRows(table.ListRows.count).Range
For col = 1 To lastRow.Columns.count
If Trim(CStr(lastRow.Cells(1, col).Value)) <> "" Then
table.ListRows.Add
Exit For
End If
Next col
Else
table.ListRows.Add
End If
'Iterate through the last row and populate it with the entries from values()
Set lastRow = table.ListRows(table.ListRows.count).Range
lastRow.Cells(1, 2) = newRate
End Sub
table.ListRows.Add
line