根据Excel中非空白的主列值合并单列中的值

Sample

我需要检查excel文件中的第一列,如果它不是空白,那么将另一列中的所有非空行组合成一个单元格,并在它们之间留有空格。 例如,在给定的示例中,对于序列号2,我必须在列“log”中合并行3,4,5,以便第二个条目(序列号2)的Log值在第一个实验失败中成为第三次实验第五次实验成功

用您的示例数据testing了这个,这将是我对这个问题的最后一篇文章

Public Sub Answer() Dim ws As Worksheet Dim Data As Variant Dim Row As Integer Dim dRow As Integer Dim DelRange As Range ' change sheet4 to the name of your worksheet Set ws = ThisWorkbook.Worksheets("Sheet4") ' change A2:C8 to the range of your data (yes there are simpler ways to do this) Data = Range("A1:C8") Row = 2 Do dRow = Row Row = Row + 1 Do While Data(Row, 1) = "" If Data(Row, 3) <> "" Then Data(dRow, 3) = Data(dRow, 3) & " " & Data(Row, 3) If DelRange Is Nothing Then Set DelRange = ws.Rows(Row) Else Set DelRange = Union(DelRange, ws.Rows(Row)) End If End If Row = Row + 1 If Row > UBound(Data, 1) Then Exit Do Loop Loop Until Row >= (UBound(Data, 1) - 1) Range("A1:C8") = Data DelRange.Delete End Sub