返回列表 上一主題 發帖

[發問] 多條件篩選&ListBox

回復 81# starbox520
參考看看:
  1. Sub Test()
  2.     Dim v
  3.     v = GetMyData("SYNAPTICS", "BGA", "17.3X7", 36)
  4.     Stop
  5. End Sub
複製代碼
新增模組
  1. Private arMaterial, arSh2
  2. Private dResult As Object

  3. Function GetMyData(cus, pkg, size, lc)
  4.     ReadFromSheet
  5.    
  6.     Method1 cus, pkg, size, lc
  7.     Method2 cus, pkg, size, lc
  8.    
  9.     Dim ar
  10.     If dResult.Count > 0 Then
  11.         ar = Application.Transpose(Application.Transpose(dResult.items))
  12.     End If
  13.     GetMyData = ar
  14.    
  15.     Erase arMaterial
  16.     Erase arSh2
  17.     Set dResult = Nothing
  18. End Function

  19. Sub ReadFromSheet()
  20.     Set dResult = CreateObject("scripting.dictionary")
  21.     '讀到 array 中
  22.     With Sheets("工作表2")
  23.         arSh2 = .[a1].CurrentRegion.Value
  24.     End With
  25.     With Sheets("材料")
  26.         arMaterial = .[a1].CurrentRegion.Value
  27.     End With
  28.    
  29.     '建立簡碼對應全名的字典
  30.     Dim ar, dCustCode As Object
  31.     Set dCustCode = CreateObject("scripting.dictionary")
  32.     With Sheets("Cus簡碼")
  33.         ar = .[a1].CurrentRegion.Value
  34.     End With
  35.     For i = 2 To UBound(ar): dCustCode(ar(i, 1)) = ar(i, 2): Next
  36.    
  37.     ' 將 arMaterial 中取代 簡碼為全名
  38.     For i = 2 To UBound(arMaterial)
  39.         If dCustCode.exists(arMaterial(i, 13)) Then
  40.             arMaterial(i, 13) = dCustCode(arMaterial(i, 13))
  41.         End If
  42.     Next
  43. End Sub

  44. Function Method1(cus, pkg, size, lc)
  45.     '找出 match 的CARRIER1 P/N
  46.     Dim dPN As Object: Set dPN = CreateObject("scripting.dictionary")
  47.     For i = 2 To UBound(arMaterial)
  48.         'M、P、Q、R , find BA
  49.         If StrComp(cus, arMaterial(i, 13), vbTextCompare) = 0 And _
  50.             StrComp(pkg, arMaterial(i, 16), vbTextCompare) = 0 And _
  51.             StrComp(size, arMaterial(i, 17), vbTextCompare) = 0 And _
  52.             StrComp(lc, arMaterial(i, 18), vbTextCompare) = 0 Then
  53.             dPN(arMaterial(i, 53)) = 0
  54.         End If
  55.     Next
  56.    
  57.     Dim ar, key
  58.     For i = 2 To UBound(arMaterial)
  59.         If dPN.exists(arMaterial(i, 53)) Then
  60.             For j = 2 To UBound(arSh2)
  61.                 'M、P、Q、R <-> A、B、C、D
  62.                 If StrComp(arMaterial(i, 13), arSh2(j, 1), vbTextCompare) = 0 And _
  63.                     StrComp(arMaterial(i, 16), arSh2(j, 2), vbTextCompare) = 0 And _
  64.                     StrComp(arMaterial(i, 17), arSh2(j, 3), vbTextCompare) = 0 And _
  65.                     StrComp(arMaterial(i, 18), arSh2(j, 4), vbTextCompare) = 0 Then
  66.                     If Not dResult.exists(j) Then dResult.Add j, Array(arSh2(j, 1), arSh2(j, 2), arSh2(j, 3), arSh2(j, 4), arSh2(j, 5), arSh2(j, 6), arSh2(j, 7), arSh2(j, 8), "1")
  67.                 End If
  68.             Next
  69.         End If
  70.     Next
  71. End Function
  72. Function Method2(cus, pkg, size, lc)
  73.     '找出 match 的 Width
  74.     Dim dPN As Object: Set dPN = CreateObject("scripting.dictionary")
  75.     For i = 2 To UBound(arMaterial)
  76.         'M、P、Q、R , find AZ
  77.         If StrComp(cus, arMaterial(i, 13), vbTextCompare) = 0 And _
  78.             StrComp(pkg, arMaterial(i, 16), vbTextCompare) = 0 And _
  79.             StrComp(size, arMaterial(i, 17), vbTextCompare) = 0 And _
  80.             StrComp(lc, arMaterial(i, 18), vbTextCompare) = 0 Then
  81.             dPN(arMaterial(i, 52)) = 0
  82.         End If
  83.     Next
  84.    
  85.     Dim ar, key
  86.     For i = 2 To UBound(arMaterial)
  87.         If dPN.exists(arMaterial(i, 52)) Then
  88.             For j = 2 To UBound(arSh2)
  89.                 'M、P、Q、R <-> A、B、C、D
  90.                 If StrComp(arMaterial(i, 13), arSh2(j, 1), vbTextCompare) = 0 And _
  91.                     StrComp(arMaterial(i, 16), arSh2(j, 2), vbTextCompare) = 0 And _
  92.                     StrComp(arMaterial(i, 17), arSh2(j, 3), vbTextCompare) = 0 And _
  93.                     StrComp(arMaterial(i, 18), arSh2(j, 4), vbTextCompare) = 0 Then
  94.                     If Not dResult.exists(j) Then dResult.Add j, Array(arSh2(j, 1), arSh2(j, 2), arSh2(j, 3), arSh2(j, 4), arSh2(j, 5), arSh2(j, 6), arSh2(j, 7), arSh2(j, 8), "2")
  95.                 End If
  96.             Next
  97.         End If
  98.     Next
  99. End Function
複製代碼
表達不清、題意不明確、沒附檔案格式、沒有討論問題的態度~~~~~~以上愛莫能助。

TOP

        靜思自在 : 站在半路,比走到目標更辛苦。
返回列表 上一主題