返回列表 上一主題 發帖

[發問] 對應欄位問題

回復 2# starbox520
妳的功力有增強了,加油!
以下兩個模組在使用陣列時,應用上有些許變化,
提供妳參考:
  1. Sub Ex()
  2.     Dim ln As Variant, ar As Variant
  3.     Dim cts As Integer, ct2 As Integer
  4.    
  5.     With 工作表1
  6.         ln = .[A1].CurrentRegion.Value
  7.         ReDim ar(1 To UBound(ln, 2) - 1, 1 To 2)

  8.         For cts = 1 To UBound(ln, 2) - 1
  9.             ar(cts, 1) = ln(1, cts + 1)
  10.             ar(cts, 2) = ""
  11.             For ct2 = 3 To UBound(ln, 1)
  12.                 If ln(ct2, cts + 1) <> 0 Then
  13.                     ar(cts, 2) = IIf(ar(cts, 2) = "", ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1), _
  14.                              ar(cts, 2) & "," & ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1))
  15.                 End If
  16.             Next ct2
  17.         Next cts

  18.         With 工作表2
  19.             .UsedRange.ClearContents
  20.             .[A1].Resize(UBound(ar, 1), UBound(ar, 2)) = ar
  21.         End With
  22.     End With
  23. End Sub
複製代碼
  1. Sub Ex1()      '  ReDim Preserve 的應用;變更最後維度的大小時,用來保留現有陣列資料。
  2.     Dim ln As Variant, ar As Variant
  3.     Dim cts As Integer, ct2 As Integer
  4.    
  5.     With 工作表1
  6.         ln = .[A1].CurrentRegion.Value
  7.         '  UBound(Ln, 1) = 25 : Long   /   UBound(Ln, 2) : 8 : Long

  8.         For cts = 1 To UBound(ln, 2) - 1
  9.             If IsEmpty(ar) Then ReDim ar(1 To 2, 1 To 1) Else ReDim Preserve ar(1 To 2, 1 To UBound(ar, 2) + 1)
  10.             ar(1, cts) = ln(1, cts + 1)
  11.             ar(2, cts) = ""
  12.             For ct2 = 3 To UBound(ln, 1)
  13.                 If ln(ct2, cts + 1) <> 0 Then
  14.                     ar(2, cts) = IIf(ar(2, cts) = "", ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1), _
  15.                               ar(2, cts) & "," & ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1))
  16.                 End If
  17.             Next ct2
  18.         Next cts
  19.         
  20.         With 工作表2
  21.             .UsedRange.ClearContents
  22.             .[A1].Resize(UBound(ar, 2), UBound(ar, 1)) = Application.Transpose(ar)
  23.         End With
  24.     End With
  25. End Sub
複製代碼

TOP

回復 6# starbox520

TOP

本帖最後由 c_c_lai 於 2016-12-12 14:08 編輯

回復 6# starbox520
scanttt.rar (27.91 KB)
這是我把 POA、以及 POB 的內容值修改了。

TOP

本帖最後由 c_c_lai 於 2016-12-12 17:36 編輯

回復 6# starbox520
依照妳 #6 所附 scanttt.xlsx  原本資料 (未加異動),
執行之修改版本。
因為每一陣列變數有 "最長不能超過 255" 的限制,
所以程式裡加了判斷,超出部分予以截掉不處裡。
scanttt.rar (30.89 KB)

TOP

本帖最後由 c_c_lai 於 2016-12-12 19:17 編輯

回復 10# starbox520
沒錯!
使用第二種方法 (ReDim Preserve),雖受限於 Application.Transpose() 255 的限制,
但它能得以動態的增加陣列,是它的優點。但是由於妳現有的案例卻不太適合,是故改採
一次直接宣告陣列大小的第一種方法 (ReDim ar()),而將陣列直接移轉 (Assign) 到工作表單內。
不致受限於 Transpose() 長度的限制。
  1. Sub Ex()
  2.     Dim ln As Variant, ar As Variant
  3.     Dim cts As Integer, ct2 As Integer
  4.    
  5.     With Sheets("Data")
  6.         ln = .[A1].CurrentRegion.Value      '  Ln :  : Variant/Variant(1 to 177, 1 to 35)
  7.         '  UBound(Ln, 1) = 177 : Long   /   UBound(Ln, 2) : 35 : Long
  8.         ReDim ar(1 To UBound(ln, 2) + 1, 1 To 2)

  9.         For cts = 1 To UBound(ln, 2) - 5
  10.             ar(cts, 1) = ln(1, cts + 1)  
  11.             ar(cts, 2) = ""
  12.             For ct2 = 3 To UBound(ln, 1)
  13.                  If ln(ct2, cts + 1) <> 0 Then
  14.                     ar(cts, 2) = IIf(ar(cts, 2) = "", ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1), _
  15.                                         ar(cts, 2) & "," & ln(ct2, 1) & IIf(ln(ct2, cts + 1) > 0, "+", "") & ln(ct2, cts + 1))
  16.                 End If
  17.             Next ct2
  18.         Next cts
  19.     End With
  20.         
  21.     With Sheets("TEST")
  22.         .[H:I] = ""
  23.         .[H2].Resize(UBound(ar, 1), UBound(ar, 2)) = ar
  24.     End With
  25. End Sub
複製代碼
scanttt2.rar (31.82 KB)

TOP

P0A 的組合:

TOP

本帖最後由 c_c_lai 於 2016-12-13 09:15 編輯

回復 10# starbox520
這是之前執行第二種方式 (Ex1()) 所產生錯誤之原因:

TOP

回復 15# starbox520
第174.175.254.255 不要讀到這4列的資訊(反黃部分) ?
不太明瞭,請白話一點,
是忽略不去計列處理,還是?

TOP

回復 17# starbox520
但是妳的 SQ0001 反黃的部分是 127、255、127、116,
那 又是怎麼回事?
116、127 也算數嗎?

TOP

回復 15# starbox520
不等妳的確認回復了,
我準備要出門去林口長庚回診了。
SQ0001_xlsm.rar (35.78 KB)

TOP

        靜思自在 : 一個人的快樂.不是因為他擁有得多,而是因為他計較得少。
返回列表 上一主題