做了个数组循环计算时间段组叠加的代码。
稍稍复杂一点,不知道有没有bug。- Sub kagawa_ArrTest()
- arr1 = Application.InputBox("选取时间段组1所在区域", "时间段组叠加计算", Type:=8): n1 = UBound(arr1)
- arr2 = Application.InputBox("选取时间段组1所在区域", "时间段组叠加计算", Type:=8): n2 = UBound(arr2)
- ReDim arr3(1 To n1 + n2 + 1, 1 To 2)
-
- i2 = 1: i3 = 1
- For i1 = 1 To n1
- If i3 > 1 Then
- If arr1(i1, 1) > arr3(i3 - 1, 2) Then
- GoTo NxtI2
- Else
- arr3(i3 - 1, 2) = arr1(i1, 2)
- GoTo NxtI1
- End If
- End If
- NxtI2:
- If arr1(i1, 1) < arr2(i2, 1) Then
- arr3(i3, 1) = arr1(i1, 1)
- If arr1(i1, 2) < arr2(i2, 1) Then
- arr3(i3, 2) = arr1(i1, 2)
- Else
- If arr1(i1, 2) < arr2(i2, 2) Then
- arr3(i3, 2) = arr2(i2, 2)
- Else
- arr3(i3, 2) = arr1(i1, 2)
- End If
- i2 = i2 + 1
- End If
- Else
- arr3(i3, 1) = arr2(i2, 1)
- If arr2(i2, 2) < arr1(i1, 1) Then
- arr3(i3, 2) = arr2(i2, 2)
- i1 = i1 - 1
- Else
- If arr2(i2, 2) < arr1(i1, 2) Then
- arr3(i3, 2) = arr1(i1, 2)
- Else
- arr3(i3, 2) = arr2(i2, 2)
- End If
- End If
- i2 = i2 + 1
- End If
- i3 = i3 + 1
- If i2 > n2 Then Exit For
- NxtI1:
- Next
- For i1 = i1 + 1 To n1
- arr3(i3, 1) = arr1(i1, 1)
- arr3(i3, 2) = arr1(i1, 2)
- i3 = i3 + 1
- Next
- For i2 = i2 To n2
- arr3(i3, 1) = arr2(i2, 1)
- arr3(i3, 2) = arr2(i2, 2)
- i3 = i3 + 1
- Next
- ActiveCell.Resize(i3, 2) = arr3
- End Sub
复制代码 |