標題:
[發問]
如何合併陣列
[打印本頁]
作者:
sunnyso
時間:
2013-5-12 03:07
標題:
如何合併陣列
本帖最後由 sunnyso 於 2013-5-12 03:09 編輯
請教各位大大,除了用For以外,VBA有沒有更簡潔有效的方法合併陣列呢?
例如
|A1 B1 C1|
A=|A2 B2 C2|
|A3 B3 C3|
B=|A4 B4 C4|
|A5 B5 C5|
C= A ++ B
|A1 B1 C1|
|A2 B2 C2|
C=|A3 B3 C3|
|A4 B4 C4|
|A5 B5 C5|
複製代碼
作者:
dechiuan999
時間:
2013-5-12 05:52
回復
1#
sunnyso
大大您好:
不知引用array是否
符合大大的須求
c=array(a,b)
作者:
GBKEE
時間:
2013-5-12 17:42
回復
1#
sunnyso
Option Explicit
Sub EX()
Dim A As Variant, B As Variant, C As Variant, I As Integer
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2"), Array("A3", "B3", "C3"))
B = Array(Array("A4", "B4", "C4"), Array("A5", "B5", "C5"))
C = A
For I = 0 To UBound(B)
ReDim Preserve C(0 To UBound(C) + 1)
'ReDim 陳述式 在程序層次中用來重新配置動態陣列變數的儲存空間。
'Preserve 當改變原有陣列最後一維的大小時,仍然保有原來的資料的關鍵字
C(UBound(C)) = B(I)
Next
End Sub
複製代碼
作者:
sunnyso
時間:
2013-5-12 19:46
回復
3#
GBKEE
感謝大大, 請問有沒有不用迴圈的方法.
作者:
GBKEE
時間:
2013-5-12 20:47
回復
4#
sunnyso
待我再想一想有無!!!
作者:
stillfish00
時間:
2013-5-16 20:24
回復
4#
sunnyso
兩個二維Array,不使用For,合併為一個
我只想的到借用工作表...
把兩個 Array 丟到儲存格再拿出來...
但是我感覺這樣比For沒有效率
作者:
sunnyso
時間:
2013-5-17 22:30
回復
6#
stillfish00
感謝, 可否提供code參考
thanks
作者:
Hsieh
時間:
2013-5-17 23:40
回復
7#
sunnyso
Sub EXE()
Dim A As Variant, B As Variant, C As Variant, D As Variant, I As Integer
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2"), Array("A3", "B3", "C3"))
B = Array(Array("A4", "B4", "C4"), Array("A5", "B5", "C5"))
[A1].Resize(UBound(A) + 1, 3) = Application.Transpose(Application.Transpose(A)) '陣列A寫入工作表
[A1].Offset(UBound(A) + 1).Resize(UBound(B) + 1, 3) = Application.Transpose(Application.Transpose(B)) '陣列B寫入工作表
D = Range("A1").CurrentRegion.Value '將儲存格資料存成新陣列
Range("A1").CurrentRegion = ""
[A1].Resize(UBound(D, 1), UBound(D, 2)) = D '寫回工作表
End Sub
複製代碼
作者:
c_c_lai
時間:
2013-5-18 09:15
本帖最後由 c_c_lai 於 2013-5-18 09:18 編輯
回復
7#
sunnyso
回復
5#
GBKEE
回復
8#
Hsieh
謝謝 GBKEE 以及 Hsieh 兩位大大的範例。
茲將它們稍稍引用:
Option Explicit
Sub EX() ' GBKEE
Dim A As Variant, B As Variant, C As Variant, I As Integer
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2") ,Array("A3", "B3", "C3"), Array("A4", "B4", "C4"))
B = Array(Array("A5", "B5", "C5"), Array("A6", "B6", "C6"))
C = A
For I = 0 To UBound(B)
ReDim Preserve C(0 To UBound(C) + 1)
'ReDim 陳述式 在程序層次中用來重新配置動態陣列變數的儲存空間。
'Preserve 當改變原有陣列最後一維的大小時,仍然保有原來的資料的關鍵字
C(UBound(C)) = B(I)
Next
Range("A1").CurrentRegion = ""
' UBound(C) = 5, UBound(C(0)) = 2
[A1].Resize(UBound(C) + 1, UBound(C(0)) + 1) = Application.Transpose(Application.Transpose(C)) ' 陣列A寫入工作表
End Sub
Sub EX2() ' Hsieh
Dim A As Variant, B As Variant, C As Variant, D As Variant, I As Integer
Range("A1").CurrentRegion = ""
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2"), Array("A3", "B3", "C3"), Array("A4", "B4", "C4"))
B = Array(Array("A5", "B5", "C5"), Array("A6", "B6", "C6"))
' UBound(A) = 3, UBound(A(0)) = 2
[A1].Resize(UBound(A) + 1, UBound(A(0)) + 1) = Application.Transpose(Application.Transpose(A)) ' 陣列A寫入工作表
' UBound(A) = 3, UBound(B) = 1, UBound(B(0)) = 2
[A1].Offset(UBound(A) + 1).Resize(UBound(B) + 1, UBound(B(0)) + 1) = Application.Transpose(Application.Transpose(B)) ' 陣列B寫入工作表
D = Range("A1").CurrentRegion.Value ' 將儲存格資料存成新陣列
Range("A1").CurrentRegion = ""
' UBound(D, 1) = 6, UBound(D, 2) = 3
[A1].Resize(UBound(D, 1), UBound(D, 2)) = D ' 寫回工作表
End Sub
複製代碼
作者:
sunnyso
時間:
2013-5-18 09:19
回復
8#
Hsieh
感謝超版
作者:
ML089
時間:
2013-5-22 15:55
回復
8#
Hsieh
請問為何要用 2個 Application.Transpose(Application.Transpose(A)) 來轉?
A1].Resize(UBound(A) + 1, 3) = Application.Transpose(Application.Transpose(A)) '陣列A寫入工作表
複製代碼
當然我測試了一下 A1].Resize(UBound(A) + 1, 3) = A,這代碼沒有寫回工作表
我寫一個小例子如下,不用Application.Transpose(Application.Transpose(A))卻可以寫回工作表,
請問之間的差異是什麼?
Sub test2()
A = Evaluate("{""A1"",""B1"",""C1"";""A2"",""B2"",""C2"";""A3"",""B3"",""C3""}")
[A1:F6] = ""
[A1].Resize(3, 3) = A
End Sub
複製代碼
作者:
GBKEE
時間:
2013-5-22 17:35
回復
11#
ML089
Option Base 0 'Option Base 陳述式 在模組層次中用來宣告陣列索引的預設下限
Sub Ex()
Dim A As Variant, B As Variant, C As Variant, i As Integer
'儲存格的範圍 = 是一個二維陣列 ,它的索引的預設下限是 1.
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2"), Array("A3", "B3", "C3"))
'A為一維陣列: 元素也是一維陣列
[I1] = "A無法置入"
[I2].Resize(3, 3) = A
'*** 不轉置 的寫法 **************
[E1] = "不轉置 的寫法"
For i = 0 To UBound(A)
[E1].Offset(i + 1).Resize(1, 3) = A(i)
Next
'********************************
B = Application.Transpose(A) '轉置一次可以 一次寫入 A 陣列於儲存格
'****轉置後的陣列 下限是 1 開始不用 +1
[A1].Resize(UBound(B), UBound(B, 2)) = B
[A1].End(xlDown).Offset(1) = "第二次轉置*"
C = Application.Transpose(Application.Transpose(A))
[A1].End(xlDown).Offset(1).Resize(UBound(A) + 1, 3) = C
Stop '看看變數
End Sub
Sub Ex__A()
'列 = 一維陣列
Cells.Clear
A = Array("A1", "B1", "C1")
[A1:C1] = A
B = Application.Transpose(A)
[a5:A7] = B
End Sub
複製代碼
[attach]15052[/attach]
作者:
ML089
時間:
2013-5-22 21:47
回復
12#
GBKEE
有點不太懂,請看看我的理解是否正確
A = Array(Array("A1", "B1", "C1"), Array("A2", "B2", "C2"), Array("A3", "B3", "C3"))
當 [A1:C3]=A 時,範圍儲存格視A為一維陣列,可是每個陣列其實又是為一個一維陣列,所以一個儲存格只能放入一個值沒有辦法儲存一組陣列,所以無法顯示
B = Application.Transpose(A) 或 B = Application.Transpose(Application.Transpose(A)) 時,會變成二維陣列,所以範圍儲存格就可以接受寫入陣列值。
A = Array("A1", "B1", "C1")
當 [A1:C3]=A 時,範圍儲存格視A為一維陣列,每陣列只有一值所有可以寫入
作者:
GBKEE
時間:
2013-5-23 10:28
回復
13#
ML089
12#程式碼沒說清楚
24. '列 = 一維陣列
複製代碼
清楚一點應該是: 單列(A1:F1)的儲存格範圍 =
一維陣列
, 單列以上(A1:F5)的儲存格範圍 =
二維陣列
多練習熟悉就會了解
作者:
linyancheng
時間:
2014-2-10 17:22
ReDim Preserve 效率不好,建議只擴大一次完成,不要放入迴圈。
假設陣列從1開始
ReDim Preserve C(UBound(C)+UBound(B))
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)