Good afternoon all,
I have a macro enabled workbook running in Excel 2016. On one tab I've set up a button that runs a macro that will add a new row to a table and set some values in it.
Here's the code.
My problem is this ... if I "empty" out my table and run the macro ... it works perfectly for the first row addition ... but if I run it again then it fails at the data validation line with the annoyingly vague "Run-time error '1004': Application-defined or object-defined error.", and the same if I "end" it and try and run it again on subsequent rows.
Can anyone suggest anything that might help me sort this out? I don't get why it works fine for one row and then not for other rows.
Kind regards,
Dave.
I have a macro enabled workbook running in Excel 2016. On one tab I've set up a button that runs a macro that will add a new row to a table and set some values in it.
Here's the code.
Code:
Sub ServiceHealth_Tbl()
Dim lo As ListObject
Dim FinalRow As Long
Dim myValue As Variant
Dim calcFormulaValue As String
Dim calcRangeValue As String
FinalRow = 0
Set lo = ActiveSheet.ListObjects("ServiceHealth")
lo.ListRows.Add , 1
myInputValue = InputBox("Enter the name of the Service Health item.", "Service Health item entry.", "ITSM connector for ServiceNow")
FinalRow = Range("B" & Rows.Count).End(xlUp).Row
If FinalRow = 12 Then Cells(FinalRow, "B").Value = 1
' I know this isn't the best way to do this but it works for me jsut now ... feel free to improve it.
If FinalRow > 12 Then calcFormulaValue = "=B" & FinalRow - 1 & "+1"
If FinalRow > 12 Then Cells(FinalRow, "B").Value = calcFormulaValue
Cells(FinalRow, "C").Value = myInputValue
Cells(FinalRow, "F").Value = "='Version Control'!$G$11"
Cells(FinalRow, "G").Value = "='Version Control'!$I$11"
Cells(FinalRow, "H").Value = "='Version Control'!$G$11"
calcRangeValue = "F" & FinalRow
'Cells(FinalRow, "F").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Version Control'!$G$11:$G$21"
'Range(calcRangeValue).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertInformation, Formula1:="='Version Control'!$G$11:$G$21"
Cells(FinalRow, "G").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Version Control'!$I$11:$I$16"
'Cells(FinalRow, "H").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="='Version Control'!$G$11:$G$21"
End Sub
My problem is this ... if I "empty" out my table and run the macro ... it works perfectly for the first row addition ... but if I run it again then it fails at the data validation line with the annoyingly vague "Run-time error '1004': Application-defined or object-defined error.", and the same if I "end" it and try and run it again on subsequent rows.
Can anyone suggest anything that might help me sort this out? I don't get why it works fine for one row and then not for other rows.
Kind regards,
Dave.