连接时间的Excel公式

给您带来的不便,敬请见谅。谢谢!

连接时间的Excel公式

dongyafei123 回答:连接时间的Excel公式

这可以帮助您:

VBA方法:

Option Explicit

Sub TEST()

    Dim LastRowA As Long,LastRowD As Long,i As Long,j As Long
    Dim Name As String,StartingTime As String,EndingTime As String

    j = 0

    With ThisWorkbook.Worksheets("Sheet1")

        LastRowA = .Cells(.Rows.Count,"A").End(xlUp).Row

        For i = 1 To LastRowA

            If i >= j Then

                StartingTime = .Range("A" & i).Value
                Name = .Range("B" & i).Value

                For j = i + 1 To LastRowA + 1

                    If .Range("B" & j).Value <> Name Then

                        EndingTime = .Range("A" & j - 1).Value
                        LastRowD = .Cells(.Rows.Count,"D").End(xlUp).Row

                        .Range("D" & LastRowD + 1).Value = Name

                        If StartingTime = EndingTime Then
                            .Range("E" & LastRowD + 1).Value = StartingTime
                        Else
                            .Range("E" & LastRowD + 1).Value = StartingTime & "-" & EndingTime
                        End If

                        Exit For

                    End If

                Next j

            End If

        Next i

    End With

End Sub

结果:

enter image description here

Excel方法:

导入并向下拖动以下公式

单元格D2中的公式: =LOOKUP(2,1/(COUNTIF($D$1:D1,$B$1:$B$8)=0),$B$1:$B$8)

单元格E2中的公式: =IF(INDEX($A$1:$A$8,MATCH(D2,$B$1:$B$8,0))= LOOKUP(2,1/($B$1:$B$8=D2),$A$1:$A$8),LOOKUP(2,INDEX($A$1:$A$8,0)) & "-" & LOOKUP(2,$A$1:$A$8))

结果:

enter image description here

本文链接:https://www.f2er.com/3169062.html

大家都在问