识别一行中最长的非空白连续单元格

问题描述

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

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

About the image: The highlighted columns represent depth & ensemble numbers,and the non-highlighted values represent velocity values that I would like to process. This spreadsheet continues for another 2,0000 columns. There's a lot of data!

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

解决方法

您可以使用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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...