Sub arr3()Dim rg1, rg2, rg3 As RangeDim arr3Set rg1 = Worksheets("sheet1").Range("a4:c4")Set rg2 = Worksheets("sheet1").Range("d5:f5")Set rg3 = Worksheets("sheet1").Range(Cells(7, 1), Cells(7, rg1.Count + rg2.Count))ReDim arr3(1 To rg1.Count + rg2.Count)i = 1 For Each cell In rg1 If i <= rg1.Count Then arr3(i) = cell.Value i = i + 1 End If Next For Each cell In rg2 If i <= rg1.Count + rg2.Count Then arr3(i) = cell.Value i = i + 1 End If Next rg3 = arr3End Sub
如果是單元格賦值的話,沒有必要經過陣列,直接將兩個range內的單元格賦值給ARR3就好了
Sub arr3()Dim rg1, rg2, rg3 As RangeDim arr3Set rg1 = Worksheets("sheet1").Range("a4:c4")Set rg2 = Worksheets("sheet1").Range("d5:f5")Set rg3 = Worksheets("sheet1").Range(Cells(7, 1), Cells(7, rg1.Count + rg2.Count))ReDim arr3(1 To rg1.Count + rg2.Count)i = 1 For Each cell In rg1 If i <= rg1.Count Then arr3(i) = cell.Value i = i + 1 End If Next For Each cell In rg2 If i <= rg1.Count + rg2.Count Then arr3(i) = cell.Value i = i + 1 End If Next rg3 = arr3End Sub執行的結果如下: