Late to the game (babysitting grandkids) but since arrays can start and end with any value, e.g. Array1(3 to 12), Array2(-2 to 0), etc., I don't like to make assumptions regardiong bounds
I often do this if I have to map to (say) columns N:Z. Too me it just makes it more readableSub test2() Dim a(-2 To 0) a(-2) = 1 a(-1) = 2 a(0) = 100 End Sub
Sub test3() Dim A(14 To 26) As Variant Dim i As Long For i = LBound(A) To UBound(A) Debug.Print Columns(i).Address Next i End Sub
So my shot was to try and make the Merge wotk without and bounds assumptions. Sometimes I have way TOO much time on my hands
Option Explicit Sub test() Dim ary1 As Variant, ary2 As Variant, aryOut As Variant Dim i As Long ary1 = Array(100, 300, 500, 700, 900, 1100) ary2 = Array(1, 2, 3, 4) aryOut = Merge(ary1, ary2) For i = LBound(aryOut) To UBound(aryOut) Debug.Print i, aryOut(i) Next i End Sub Function Merge(A1 As Variant, A2 As Variant) As Variant Dim n1 As Long, n2 As Long, i As Long, o As Long, m As Long Dim i1 As Long, i2 As Long Dim A() As Variant n1 = UBound(A1) - LBound(A1) + 1 n2 = UBound(A2) - LBound(A2) + 1 ReDim A(1 To n1 + n2) m = Application.Min(n1, n2) o = 1 i1 = LBound(A1) i2 = LBound(A2) For i = 1 To m A(o) = A1(i1) i1 = i1 + 1 A(o + 1) = A2(i2) i2 = i2 + 1 o = o + 2 Next i If n1 > n2 Then For i = i1 + 1 To n1 A(o) = A1(i1) i1 = i1 + 1 o = o + 1 Next i Else For i = i2 + 1 To n2 A(o) = A2(i2) i2 = i2 + 1 o = o + 1 Next i End If Merge = A End Function




Reply With Quote