0

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
5
  • 3
    Step through the code line by line and see if it freezes or errors
    – mojo3340
    Commented Jan 9, 2017 at 11:32
  • Yeah it seems to dislike the table.ListRows.Add line
    – eugene
    Commented Jan 9, 2017 at 11:54
  • And what does it not like about it? hover above the line whilst stepping through. Is it throwing an error? If so what is the error
    – mojo3340
    Commented Jan 9, 2017 at 12:00
  • Doesn't specify, Excel crashes entirely right after that line, no information and no option to hover over any tooltip
    – eugene
    Commented Jan 9, 2017 at 12:20
  • Perhaps there is an infinite interation occuring somehow. Before that, first try setting screenupdating, enableevents to false and calculation to Manual.
    – mojo3340
    Commented Jan 9, 2017 at 14:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.