0

我有一个速度值的 Excel 电子表格(见下面的链接),其中穿插了一些空白。我试图遍历每一行并返回每行中最长的一组连续非空白单元格的范围。我最终将使用范围地址来执行其他功能(即,平均该范围内的值)。我之前使用下面的代码来计算一个范围内的列数,但还没有弄清楚如何只计算非空白单元格并继续在同一行中计数。

ColumnCount = Cells(1, Columns.Count).End(xlToLeft).Column

关于图像:突出显示的列代表深度和整体数字,未突出显示的值代表我想要处理的速度值。此电子表格继续显示另外 2,0000 列。有很多数据!

谢谢!任何帮助将非常感激!玛丽

4

1 回答 1

0

您可以使用Do Until循环和If语句从头到尾遍历整行。下面是一行的示例(目前没有 Excel,因此无法检查)。maxLength变量存储在每次迭代中找到的最大值。Cells(1, currCol).Value = ""用于检查连续范围是否仅包含 1 个单元格(否则,它将计算空范围加上 2 个非空单元格再加上 1 个单元格)。

Dim maxLength as Integer
maxLength = 0
currCol = 1
totalCol = Columns.Count
If Cells(1, currCol).Value = "" Then
    currCol = Cells(1, currCol).End(xlToRight).Column
End If
Do Until currCol = totalCol
    prevCol = currCol
    If Cells(1, prevCol + 1).Value = "" Then
        maxLength = WorkSheetFunction.Max(maxLength, 1)
    Else
        currCol = Cells(1, currCol).End(xlToRight).Column
        maxLength = WorkSheetFunction.Max(maxLength, currCol - prevCol + 1)
    End if
    currCol = Cells(1, currCol).End(xlToRight).Column
Loop
于 2020-09-08T15:32:25.910 回答