VBA excel,将范围转换为 html 时的奇怪行为

VBA excel, strange behavior when converting range to html

我一直在尝试编写一些代码,这些代码应该可以将作品sheet 中的范围转换为html table。它似乎大部分工作正常。但有时会多次填充行,这意味着在活动 sheet 中只有 2 行要复制,但 html table 输出包含 header 和数据的重复html 代码的 table 行中的行。有趣的是,如果我在计算行数和列数后立即设置一个断点,错误似乎就不会经常发生。我真的迷路了,有人能解释一下吗?

我正在使用以下代码:

 ' 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

依赖 ActiveSheet 时的一个常见错误是指定单元格范围的 parent 而未能在标记单元格上指定相同的 parent范围的开始和停止。示例:

NumbofRows = Report.Range("A1", Report.Range("A1").End(xlDown)).Rows.Count
NumbofCols = Report.Range("A1", Report.Range("A1").End(xlToRight)).Columns.Count

我会将这整段代码包装到一个 With ... End With 代码块中,并在每个 cell/range 定义前加上一个 .. 指定每个引用属于使用 With ... End With 代码定义的 parent。

With Report
     ' establish number of columns and rows to send
    '.Activate 'NOT NECESSARY
    NumbofRows = .Range("A1", .Range("A1").End(xlDown)).Rows.Count
    NumbofCols = .Range("A1", .Range("A1").End(xlToRight)).Columns.Count

    ' Populate headers
    TableHeaders = "<table> <tr>"
    For i = 1 To NumbofCols
        TableHeaders = TableHeaders & "<th>" & .Cells(1, i) & "</th>"
    Next i
    TableHeaders = TableHeaders & "</tr>"

    ' populate response rows
    For y = 2 To NumbofRows
        If CBool(y Mod 2) Then
            Style = "style= " & Chr(39) & "background:#E6F5FF" & Chr(39)
        Else
            Style = "style= " & Chr(39) & "background:#CCEBFF" & 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>" & .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
End With

这应该会处理错误的行为,让您从 ActiveSheet 的 parentage of cell/range 引用中解脱出来。

您使用Report.Range("A1").End(xlDown) 来定义范围的范围有点麻烦。 table 中没有行超过 header 行,您将所有行定义到工作表底部。