0

I have a bit of code that checks a userform for data in a combobox. Then if found adds a row to the table. Unfortunately, I am finding that some times it does not add the row and just overwrites the line. Any ideas on what I have done wrong would help. The following is the code for the portion that creates the new line:

If Not vbNullString = Me.CrimeSN2.Value Then
    If Not vbNullString = Me.RowNumber.Value Then
        rownumval = Me.RowNumber.Value
        Worksheets("Offense").ListObjects("OffenseTable").ListRows.Add (rownumval)
        rownumval = rownumval + 1
    Else
        rownumval = LastRow + 1
        Worksheets("Offense").ListObjects("OffenseTable").ListRows.Add (rownumval)
        rownumval = rownumval + 1
end if
3
  • Does it just overwrite the last row of your table? Does it happen during the If or the Else part of your code, or both? Is LastRow correct when you step through your code using F8? Commented Apr 20, 2016 at 22:25
  • You check if the cell value (Me.RowNumber.Value) is not Null, however, it may not be a number. If it's not a number, the .Add will fail. Commented Apr 20, 2016 at 23:38
  • Thanks for the response Doug and OldUgly, to respond to your inquiry. It overwrites at the location of the rownumval+1. I stepped through the code and it only overwrites on the If portion. and LastRow is correct. I have stepped through it and it reads rownumval as a long and has value. it just seems to not work on the .ListRows.Add (rownumval). To Old Ugly Me.RowNumber.Value is reading as long and has value. I did try the code you provided but still continues with the error. Commented Apr 21, 2016 at 15:38

1 Answer 1

1

This will provide further protection against having a non-number in rownumval

If Not vbNullString = Me.CrimeSN2.Value Then
    If Not vbNullString = Me.RowNumber.Value And IsNumeric(Me.RowNumber.Value) Then
        rownumval = Me.RowNumber.Value
        Worksheets("Offense").ListObjects("OffenseTable").ListRows.Add (rownumval)
        rownumval = rownumval + 1
    Else
        rownumval = LastRow + 1
        Worksheets("Offense").ListObjects("OffenseTable").ListRows.Add (rownumval)
        rownumval = rownumval + 1
end if
Sign up to request clarification or add additional context in comments.

Comments

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.