Join cells based on value of a cell vba

后端 未结 2 625
余生分开走
余生分开走 2021-01-27 16:21

I am trying to join cells in a row if a value exists in a cell in that row.

The data has been imported from a .txt file and various sub headers are split along 2, 3 or 4

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-27 17:04

    Not sure if this is exactly what you want but it will get you close:

    Sub summary()
        Dim sh1 As Worksheet, sh2 As Worksheet
        Dim N As Long, i As Long, r1 As Range, r2 As Range
        Dim z As Long
        Dim arr() As Variant
        z = 1
        Set sh1 = ActiveSheet
        With ActiveWorkbook
            Set sh2 = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        End With
    
        With sh1
            N = .Cells(Rows.Count, "A").End(xlUp).Row
            For i = 1 To N
                If .Cells(i, "A").Value Like "Summary*" Then
                    arr = .Range(.Cells(i, "A"), .Cells(i, "H")).Value
                    sh2.Cells(z, "A").Value = Join(arr, " ")
                    z = z + 1
                End If
            Next i
        End With
    End Sub
    

提交回复
热议问题