Board logo

標題: 如何把兩個Array合併? [打印本頁]

作者: FAlonso    時間: 2011-3-29 21:34     標題: 如何把兩個Array合併?

除了這個麻煩辦法外
  1. Sub abc()
  2. Dim a(), b(), c(), i
  3. a = Array("Tony", "Garnett")
  4. b = Array("Allen", "Pierce", "Bryant")
  5. ReDim Preserve c(UBound(a) + UBound(b) + 1)
  6. For i = 0 To UBound(a)
  7. c(i) = a(i)
  8. Next
  9. For i = 0 To UBound(b)
  10. c(i + UBound(a) + 1) = b(i)
  11. Next
  12. End Sub
複製代碼
有沒有快捷方法處理?
作者: GBKEE    時間: 2011-3-29 21:45

回復 1# FAlonso
適用一維陣列
  1. Sub ex()
  2. Dim a(), b(), C As Variant
  3. a = Array("Tony", "Garnett")
  4. b = Array("Allen", "Pierce", "Bryant")
  5. C = Join(a, ",") & "," & Join(b, ",")
  6. C = Split(C, ",")
  7. MsgBox Join(C, ",")
  8. End Sub
複製代碼

作者: FAlonso    時間: 2011-3-29 22:21

回復 2# GBKEE
  1. Sub combine_array_GBKEE()
  2. Dim a(), b(), c As Variant, d(), i As Integer, j As Integer
  3. a = Array("Tony", "Garnett")
  4. b = Array("Allen", "Pierce", "Bryant")
  5. c = Join(a, ",") & "," & Join(b, ",")

  6. i = UBound(Split(c, ","))
  7. ReDim Preserve d(i)
  8. For j = 0 To i
  9. d(j) = Split(c, ",")(j)
  10. Next

  11. End Sub
複製代碼
已經略作修改,完成了
多謝各位指導
作者: GBKEE    時間: 2011-3-30 06:35

回復 4# chin15
你的發文下方    [編輯],
勾選右上 [刪] ,[編輯帖子]  
刪除完畢
作者: chin15    時間: 2011-3-30 08:30

謝謝指教,我會了
已成功刪除自己造成的垃圾
作者: GBKEE    時間: 2011-3-30 11:48

回復 3# FAlonso
  1. Sub Ex()
  2.     Dim A(0 To 3), C(), E, R
  3.     A(0) = Array("Tony", "Garnett")
  4.     A(1) = Array("Allen", "Pierce", "Bryant")
  5.     A(2) = Array("A1", "B1", "C1")
  6.     A(3) = Array("AA1", "BB1", "CC1")
  7.     ReDim C(0)
  8.     For Each E In A
  9.         For Each R In E
  10.             C(UBound(C)) = R
  11.             ReDim Preserve C(UBound(C) + 1)
  12.         Next
  13.     Next
  14.     ReDim Preserve C(UBound(C) - 1)
  15.     MsgBox Join(C, ",")
  16. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)