首页 > 解决方案 > Excel:表格单元格在多行数据后似乎有空格

问题描述

这是一个奇怪的问题,我不知道如何正确地用词来找到搜索结果,所以即使我不想在这里问我也会去问,因为我只是感到困惑。

我在一些代码中填充的 Excel 工作表上有一个表格。在一个特定的列中,我插入了一些多行字符串。

当数据被插入时,它会在数据之后显示一堆空行,即;行高大于应有的高度,即使它应该是自动拟合的。

如果我尝试双击左侧的行号以自动适应它不起作用的行,就好像它认为单元格中的数据后有换行符,但是,双击单元格进行编辑或选择它并单击公式栏显示没有空行,退出编辑后,该行将自动适应,一切都很好。

好的,即使我在这少数函数中删除了数百个 loc ,但代码仍然比我想发布的要多,抱歉,我只是想表明代码中的任何地方都没有任何可疑之处(我可以看到)。

因此,与此代码相关的是,有两个工作表,每个工作表都有一个表。在代码中,它们被称为globalListObject和。还有两个's ,这只是为了在更改列时使维护更容易。gloMainTablegloSummaryTableEnumMainTableColumnsSummaryTableColumns

gloSummaryTable在我们到达此代码之前被清除并且我们正在填充它。我们通过调用这个来开始:

Private Sub CreateSummaryTable()
    Dim i As Long
    For i = 1 To gloMainTable.ListRows.Count
        ' All sorts of checks and other stuff
        Call CreateTableEntry(i)
    Next i
End Sub

我们gloMainTable进行检查,当我们找到我们想要的行时,我们将该行号传递到CreateTableEntry()我们获取新行数据的地方gloSummaryTable

Private Sub CreateTableEntry(parRow As Long)
    Dim descriptions As String
    Dim projectRows As Long
    Dim endRow As Long
    'All manner of irrelevant stuff
    projectRows = GetProjectRowCount(parRow)
    endRow = parRow + projectRows - 1
    descriptions = GetDescriptions(parRow, endRow)
    Call FillTableRow(parRow, descriptions) ' Irrelevant parameters removed.
End Sub

除其他事项外,CreateTableEntry()我们在内部找出需要迭代多少行gloMainTable来为新行构建数据,方法gloSummaryTable是调用GetProjectRowCount(). 我们将开始行和结束行传入GetDescriptions(),以便我们可以构建导致我试图解决的问题的字符串。

Private Function GetProjectRowCount(parRow As Long) As Long
    Dim salesforceRef As String
    Dim res As Long
    Dim i As Long

    salesforceRef = gloMainTable.DataBodyRange(parRow, MainTableColumns.Salesforce)
    res = 1
    For i = parRow To gloMainTable.ListRows.Count
        If gloMainTable.DataBodyRange(i + 1, MainTableColumns.Salesforce) = salesforceRef Then
            res = res + 1
        Else
            Exit For
        End If
    Next i

    GetProjectRowCount = res
End Function

在这里,我们检查起始行之后有多少行属于同一数据集(检查 salesforce 参考号),以便我们可以对所述行范围执行所需的任何计算,并将描述合并到GetDescriptions()函数中的单个字符串中。

Private Function GetDescriptions(parStart As Long, parEnd As Long) As String
    Dim res As String
    Dim opt As Long
    Dim i As Long
    
    If NoMoreOptions(parStart, parEnd) Then
        res = res & gloMainTable.DataBodyRange(parStart, MainTableColumns.Description)

        For i = parStart To parEnd - 1
            res = res & vbNewLine & gloMainTable.DataBodyRange(i + 1, MainTableColumns.Description)
        Next i
    Else
        opt = GetCheapestOption(parStart, parEnd)
        res = GetDescriptionsOfOption(parStart, parEnd, opt)
    End If
    
    GetDescriptions = res
End Function

在这里,我们创建了我们的综合描述字符串,我们将它们交还给调用代码,CreateTableEntry()以便它们可以与这里未显示的许多其他数据一起传递到FillTableRow().

Private Sub FillTableRow(_
    parRow As Long, _
    Optional parDescriptions As String = "") ' All irrelevant params removed

    Dim newRow As listRow
    Set newRow = gloSummaryTable.ListRows.Add
    'All manner of irrelevant stuff
    If parDescriptions = "" Then
        newRow.Range(, SummaryTableColumns.Description) = _
        gloMainTable.DataBodyRange(parRow, MainTableColumns.Description)
    Else
        newRow.Range(, SummaryTableColumns.Description) = Trim(parDescriptions)
    End If 
End Sub

然后我们在其中创建新行gloSummaryTable并填充它(不仅仅是描述)

我只是不知道发生了什么...

标签: excelvba

解决方案


推荐阅读