I've been trying my hand at some code that is supposed to convert a range in a worksheet to an html table. It seems to work fine mostly. But sometimes populates the rows multiple times, meaning there would be only 2 rows to copy in the active sheet, but the html table output contains repetitions of the header and data rows in the table rows of the html code. The funny thing is if I set a break point just after counting rows and columns the bug seems to occur less often. I'm really lost here could anybody shed some light on this?
I am using the following code:
' establish number of columns and rows to send
Report.Activate 'this is a worksheet object
NumbofRows = Report.Range("A1", Range("A1").End(xlDown)).Rows.Count
NumbofCols = Report.Range("A1", Range("A1").End(xlToRight)).Columns.Count
' Populate headers
TableHeaders = "<table> <tr>"
For i = 1 To NumbofCols
TableHeaders = TableHeaders & "<th>" & Report.Cells(1, i) & "</th>"
Next i
TableHeaders = TableHeaders & "</tr>"
' populate response rows
For y = 2 To NumbofRows
If WorksheetFunction.IsEven(y) Then
Style = "style= " & Chr(39) & "background:#CCEBFF" & Chr(39)
Else
Style = "style= " & Chr(39) & "background:#E6F5FF" & Chr(39)
End If
' loop through cells on the current row and add them to the table
TableRows = TableRows & "<tr " & Style & ">"
For x = 1 To NumbofCols
TableRows = TableRows & "<td>" & Report.Cells(y, x) & "</td>"
Next x
TableRows = TableRows & "</tr>"
Next y
' close table tag
TableRows = TableRows & "</table> <br> <br>"
'stick them together
ResponseTable = TableHeaders & TableRows