Sub 模糊查詢()
Dim Rg As Range, Addr0$, R1&
[K:N].ClearContents
With [庫存!C:C]
Set Rg = .Find([J2] & "*", , , xlWhole)
If Not Rg Is Nothing Then Addr0 = Rg.Address
Do While Not Rg Is Nothing
R1 = R1 + 1
Rg.Resize(, 4).Offset(, -2).Copy Cells(R1, "K")
Set Rg = .FindNext(Rg)
If Rg.Address = Addr0 Then Exit Do
Loop
End With
End Sub
Sub 模糊查詢()
Dim Rg As Range, Addr0$, R1&
[K:N].ClearContents
[K1:N1] = Array("品號", "品名", "規格", "數量")
R1 = 1
With [庫存!A:C]
For Each a In Sheets("目標").Range([a2], [a2].End(4))
If a.Offset(, 2) <> "" Then
Set Rg = .Find(Left(a.Offset(, 2), 8) & "*", , , xlWhole)
Else
Set Rg = .Find(a, , , xlWhole)
End If
If Not Rg Is Nothing Then Addr0 = Rg.Address
Do While Not Rg Is Nothing
R1 = R1 + 1
If Rg.Column = 3 Then
Rg.Resize(, 4).Offset(, -2).Copy Cells(R1, "K")
Else
Rg.Resize(, 4).Copy Cells(R1, "K")
End If
Set Rg = .FindNext(Rg)
If Rg.Address = Addr0 Then Exit Do
Loop
Next
End With
End Sub作者: ikboy 時間: 2020-8-20 12:19
刪去公式,使用VBA會更快:
Sub zz()
Dim a, d As Object, b(), n&
a = Sheets(1).Range("a2:d" & Sheets(1).[a1048576].End(3).Row)
Set d = CreateObject("scripting.dictionary")
With CreateObject("vbscript.regexp")
.Pattern = "-\w$"
For i = 1 To UBound(a)
If Len(a(i, 3)) = 0 Then a(i, 3) = a(i, 1)
k = a(i, 3)
a(i, 3) = .Replace(k, "")
d(a(i, 3)) = ""
Next
k = Join(d.keys, "|")
.Pattern = k
a = Sheets(2).[a1].CurrentRegion
ReDim b(1 To UBound(a), 1 To UBound(a, 2))
For i = 2 To UBound(a)
If Len(a(i, 3)) > 0 Then k = a(i, 3) Else k = a(i, 1)
If .test(k) Then
n = n + 1
For j = 1 To UBound(a, 2)
b(n, j) = a(i, j)
Next
End If
Next
Sheets(3).[a1].CurrentRegion.Offset(1).Clear
Sheets(3).[a2].Resize(n, 4) = b
End With
End Sub
複製代碼
作者: 准提部林 時間: 2020-8-20 21:12
簡化不了,
Sub TEST()
Dim Arr, xD, i&, j%, N&, T$, V%
Set xD = CreateObject("scripting.dictionary")
Arr = Range([目標!C1], [目標!A65536].End(xlUp))
For i = 2 To UBound(Arr)
For j = 1 To 3 Step 2
T = Arr(i, j): If T <> "" Then xD(T) = 1
If T Like "*-*-*" Then xD(Left(T, InStrRev(T, "-") - 1)) = 1
Next j
Next i
Arr = Range([庫存!D1], [庫存!A65536].End(xlUp))
For i = 2 To UBound(Arr)
For j = 1 To 3 Step 2
T = Arr(i, j): V = V + xD(T)
If T Like "*-*-*" Then V = V + xD(Left(T, InStrRev(T, "-") - 1))
Next j
If V = 0 Then GoTo 101
N = N + 1: V = 0
For j = 1 To 4: Arr(N, j) = Arr(i, j): Next
101: Next i
[成果!A2:A6000].ClearContents
If N > 0 Then [成果!A2:D2].Resize(N) = Arr
End Sub
回復 18#qaqa3296
紅色部分是你的搜尋目標嗎? 如果是 你可以用關鍵字篩選 比較簡單 如果結果不是你要的 你可以把 "" & x & "*" 改成你要的方式
Public Sub 模糊篩選()
Range(Cells(2, 1), Cells(2, 4).End(xlDown)).Font.Color = RGB(255, 0, 0)
Application.ScreenUpdating = False
G = True
Sheets(3).Select
Sheets(3).Range(Cells(1, 6), Cells(1, 9).End(xlDown)).Clear
Sheets(2).Select
For K = 2 To Cells(2, 5).End(xlDown).Row
x = Cells(K, 5)
For i = 2 To Cells(2, 3).End(xlDown).Row '依條件篩選
If Cells(K, 5) = "" Then
Cells(i, 1).AutoFilter Field:=3, Criteria1:="="
Range(Cells(2, 1), Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Else
Cells(i, 1).AutoFilter Field:=3, Criteria1:="" & x & "*"
Range(Cells(2, 1), Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If G = True Then
Range(Cells(1, 1), Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 6)
G = False
Else
Range(Cells(2, 1), Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 6).End(xlDown).Offset(1, 0)
End If
Cells(2, 3).AutoFilter
Exit For
Next i
Next K
Sheets(3).Select
Range(Cells(2, 6), Cells(2, 9).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: n7822123 時間: 2020-8-22 03:12
Sub 模糊查詢()
Dim Rg As Range, 查找範圍 As Range, 此表 As Object
Dim Arr, R&, Key$, MD$, Csft&, K2$, Addr0$, R1&
[成果!A1].CurrentRegion.Offset(1).ClearContents
Arr = Range([D1], [A1].End(4))
Set 此表 = ActiveSheet: Sheets("成果").Activate
R1 = 1: [A1:D1] = Array("品號", "品名", "規格", "數量")
For R = 2 To UBound(Arr)
MD = Replace(Arr(R, 3), " ", "") '移除空白(不管在哪個位置)
Key = ""
If MD Like "####*" Then Key = Left(MD, 4)
If MD Like "[A-Z]####*" Then Key = Left(MD, 5)
If MD Like "###-####*" Then Key = Left(MD, 8)
If MD Like "[A-Z]##-[A-Z]###*" Then Key = Left(MD, 8)
If Key <> "" Then '若規格符合上述4種格式,則模糊查詢
Set 查找範圍 = [庫存!C:C]: Csft = -2: K2 = "*"
Else '若規格不符合上述4種格式,改查品號(僅單筆)
Set 查找範圍 = [庫存!A:A]: Csft = 0: K2 = "": Key = Arr(R, 1)
End If
With 查找範圍
Set Rg = .Find(Key & K2, , , xlWhole)
If Not Rg Is Nothing Then Addr0 = Rg.Address
Do While Not Rg Is Nothing
R1 = R1 + 1
Rg.Resize(, 4).Offset(, Csft).Copy Cells(R1, "A")
Set Rg = .FindNext(Rg)
If Rg.Address = Addr0 Then Exit Do
Loop
End With
Next R
End Sub
測試資料太少, 無法多做驗證:
Sub TEST_V1()
Dim Arr, A, xD, i&, j%, N&, T$, V%
[成果!A2:D6000].ClearContents
Set xD = CreateObject("scripting.dictionary")
Arr = Range([目標!C1], [目標!A65536].End(xlUp))
For i = 2 To UBound(Arr)
T = Trim(Arr(i, 1)): If T <> "" Then xD(T) = 1
T = 拆解編號(Trim(Arr(i, 3))): If T = "" Then GoTo 101
For Each A In Split(T, "/"): xD(A & "") = 1: Next
101: Next i
Arr = Range([庫存!D1], [庫存!A65536].End(xlUp))
For i = 2 To UBound(Arr)
If xD("|" & i) > 0 Then GoTo 102 '如果該行已被提取過, 略過, 避免重覆提取
T = Trim(Arr(i, 1)): If xD(T) > 0 Then V = 1: GoTo 999 '[品號]相符即直接提取
T = Trim(Arr(i, 3)): If T = "" Then GoTo 102
T = 拆解編號(T) '拆解[規格]
For Each A In Split(T, "/")
If xD(A & "") > 0 Then V = 1: Exit For
Next
999:
If V = 0 Then GoTo 102
N = N + 1: V = 0
For j = 1 To 4: Arr(N, j) = Trim(Arr(i, j)): Next
xD("|" & i) = 1 '已提取行號位置,記錄入字典
102: Next i
If N > 0 Then [成果!A2:D2].Resize(N) = Arr
End Sub
'==========================================
Function 拆解編號(xS$) As String
Dim TT$, j%, ST$
If xS = "" Then Exit Function
If xS & "-" Like "####[-.]*" Then TT = Left(xS, 4)
If xS & "A" Like "####[A-Z]*" Then TT = Left(xS, 4)
If xS & "-" Like "?????[-.]*" Then TT = Left(xS, 5)
If xS & "-" Like "???-????[-.]*" Then TT = Left(xS, 8)
xS = xS & "-"
For j = Len(TT) + 2 To Len(xS)
If Mid(xS, j, 1) Like "[-.]" Then TT = Left(xS, j - 1) & "/" & TT
Next j
拆解編號 = TT
End Function
Sub TEST_V1()
Dim Arr, A, xD, i&, j%, N&, T$, V%
[成果!A2:D6000].ClearContents
Set xD = CreateObject("scripting.dictionary")
Arr = Range([目標!C1], [目標!A65536].End(xlUp))
For i = 2 To UBound(Arr)
T = Trim(Arr(i, 1)): If T <> "" Then xD(T) = 1
T = 拆解編號(Trim(Arr(i, 3))): If T = "" Then GoTo 101
For Each A In Split(T, "/"): xD(A & "") = 1: Next
101: Next i
Arr = Range([庫存!D1], [庫存!A65536].End(xlUp))
For i = 2 To UBound(Arr)
If xD("|" & i) > 0 Then GoTo 102 '如果該行已被提取過, 略過, 避免重覆提取
T = Trim(Arr(i, 1)): If Val(xD(T)) > 0 Then V = 1: GoTo 999 '[品號]相符即直接提取
T = Trim(Arr(i, 3)): If T = "" Then GoTo 102
T = 拆解編號(T) '拆解[規格]
For Each A In Split(T, "/")
If A <> "" And Val(xD(A & "")) > 0 Then V = 1: Exit For
Next
999:
If V = 0 Then GoTo 102
N = N + 1: V = 0
For j = 1 To 4: Arr(N, j) = Trim(Arr(i, j)): Next
xD("|" & i) = 1 '已提取行號位置,記錄入字典
102: Next i
If N > 0 Then [成果!A2:D2].Resize(N) = Arr
End Sub
'==========================================
Function 拆解編號(xS$) As String
Dim TT$, j%, ST$
If xS = "" Then Exit Function
If Left(xS, 4) Like "####" Then TT = Left(xS, 4)
If Left(xS, 5) Like "####[A-Z]" Then TT = Left(xS, 5) & "/" & TT
If Left(xS, 5) Like "[A-Z]####" Then TT = Left(xS, 5) & "/" & TT
If Left(xS, 8) Like "???-????" Then TT = Left(xS, 8) & "/" & TT
xS = xS & "-"
For j = Len(TT) + 2 To Len(xS)
If Mid(xS, j, 1) Like "[-.(]" Then TT = Left(xS, j - 1) & "/" & TT
Next j
拆解編號 = TT
End Function
Public Sub 模糊篩選()
Application.ScreenUpdating = False
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(255, 0, 0)
G = True
Sheets(3).Select
Sheets(3).Range(Cells(1, 1), Cells(1, 4).End(xlDown)).Clear
Sheets(2).Select
For K = 2 To Cells(2, 5).End(xlDown).Row
x = Sheets(1).Cells(K, 3)
If Sheets(1).Cells(K, 1) = "" Then Exit For
For i = 2 To Cells(2, 3).End(xlDown).Row '依條件篩選
If Sheets(1).Cells(K, 3) = "" And Asc(Sheets(1).Cells(K, 2)) > 127 Or Asc(Sheets(1).Cells(K, 2)) < 0 Then
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Count - 1 And Sheets(1).Cells(K, 3) = "" Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
Else
Cells(i, 1).AutoFilter Field:=3, Criteria1:="*" & x & "*"
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Exit For
Next i
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub
javascript:;作者: 軒云熊 時間: 2020-8-23 10:39
如果是 品名還有規格 打錯字 我是改這樣 但是 規格的結果跟 準大的不同
Public Sub 模糊篩選()
Application.ScreenUpdating = False
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(255, 0, 0)
G = True
Sheets(3).Select
Sheets(3).Range(Cells(1, 1), Cells(1, 4).End(xlDown)).Clear
Sheets(2).Select
For K = 2 To Cells(2, 5).End(xlDown).Row
x = Sheets(1).Cells(K, 3)
If Sheets(1).Cells(K, 1) = "" Then Exit For
For i = 2 To Cells(2, 3).End(xlDown).Row '依條件篩選
If Sheets(1).Cells(K, 3) = "" And Asc(Sheets(1).Cells(K, 2)) > 127 Or Asc(Sheets(1).Cells(K, 2)) < 0 Then
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Count - 1 And Sheets(1).Cells(K, 3) = "" Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Count - 1 And Sheets(1).Cells(K, 3) <> "" Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
Else
Cells(i, 1).AutoFilter Field:=3, Criteria1:="*" & x & "*"
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Exit For
Next i
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: qaqa3296 時間: 2020-8-23 12:21
If X Like "####[-.]*" Or X Like "####[A-Z]*" Then
Cells(i, 1).AutoFilter Field:=3, Criteria1:="=*" & Mid(X, 1, 4) & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
If X = "" Then
X = Trim(Sheets(1).Cells(K, 2))
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Exit For
End If
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
Exit For
Next i
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: qaqa3296 時間: 2020-8-25 20:44
If X Like "####[-.]*" Or X Like "####[A-Z]*" Then
Cells(i, 1).AutoFilter Field:=3, Criteria1:="=*" & Mid(X, 1, 4) & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
If X = "" Then
X = Trim(Sheets(1).Cells(K, 2))
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Exit For
End If
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
Exit For
Next i
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: 軒云熊 時間: 2020-8-25 21:30
If X Like "####[-.]*" Or X Like "####[A-Z]*" Then
Cells(i, 1).AutoFilter Field:=3, Criteria1:="=*" & Mid(X, 1, 4) & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If Sheets(1).Cells(K, 2) <> "" Then
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
End If
If X = "" Then
X = Trim(Sheets(1).Cells(K, 2))
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Exit For
End If
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
Exit For
Next i
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: 軒云熊 時間: 2020-8-25 22:32
回復 38#軒云熊
如果是 品號 品名 規格 數量 其中一個忘記打 可以試試這個:)
Public Sub 模糊篩選()
If X Like "####[-.]*" Or X Like "####[A-Z]*" Then
Cells(i, 1).AutoFilter Field:=3, Criteria1:="=*" & Mid(X, 1, 4) & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
If Sheets(1).Cells(K, 2) <> "" Then
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:="=" & X & ""
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Cells(i, 1).AutoFilter Field:=2, Criteria1:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
End If
If X = "" Then
X = Trim(Sheets(1).Cells(K, 2))
Cells(i, 1).AutoFilter Field:=2, Criteria1:="=*" & X & "*", Operator:=xlOr, Criteria2:=Sheets(1).Cells(K, 2)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Sheets(2).Cells(2, 3).AutoFilter
Cells(i, 1).AutoFilter Field:=1, Criteria1:=Sheets(1).Cells(K, 1)
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count = 1 Then
Exit For
End If
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
End If
End If
Exit For
Next i
If G = True Then
Range(Sheets(2).Cells(1, 1), Sheets(2).Cells(1, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1)
G = False
Else
Range(Sheets(2).Cells(2, 1), Sheets(2).Cells(2, 4).End(xlDown)).Copy Sheets(3).Cells(1, 1).End(xlDown).Offset(1, 0)
End If
Sheets(2).Cells(2, 3).AutoFilter
Next K
Sheets(3).Select
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).RemoveDuplicates Columns:=1
Range(Sheets(3).Cells(2, 1), Sheets(3).Cells(2, 4).End(xlDown)).Font.Color = RGB(0, 0, 0)
Application.ScreenUpdating = True
End Sub作者: qaqa3296 時間: 2020-8-25 23:55
測試過程有時會出現
執行階段錯誤91:
沒有設定物件變數或With區塊變數
錯誤範圍
If ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Rows.Count <> 1 _
Or ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas.Count <> 1 Then
但觸發條件不明
謝謝前輩發表此主題與範例
不論是否符合需求! 後學在此帖學到很多知識!
後學的陣列與字典練習心得註解如下:
Option Explicit
Sub TEST_1()
Dim Brr, Arr, c&, R&, V, Y, Z
Dim K$, P$, Q, S
'↑宣告變數
S = Timer
Sheets(3).[M2:P60000].ClearContents
Set Y = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
Set V = CreateObject("Scripting.Dictionary")
'↑令Y,Z,V各是字典
Arr = Sheets(1).Range("A1:C" & Sheets(1).[A65536].End(3).Row)
'↑目標表 陣列範圍
For R = 1 To UBound(Arr)
'↑外順迴圈把 目標表 規格拆解,重組為模糊比對關鍵字並倒入V字典
For c = 1 To UBound(Arr, 2)
'↑內順迴圈去除空白字元
Arr(R, c) = Replace(Arr(R, c), " ", "")
Next
P = Arr(R, 3)
If P Like "*-*-*" Then
P = Split(P, "-")(0) & "-" & Split(P, "-")(1)
ElseIf P = "" Then
'↑如果規格欄是空格 就以A欄格與B欄格組為模糊比對關鍵字
P = Arr(R, 1) & Arr(R, 2)
End If
V(P) = 1
'↑倒入V字典
P = ""
Next
Brr = Sheets(2).Range("D1:A" & Sheets(2).[A65536].End(3).Row)
'↑庫存表 陣列範圍
For R = 1 To UBound(Brr)
'↑外順迴圈把 庫存表 規格拆解,重組再加入符號 "|" 與列數
',為模糊比對關鍵字並倒入Z字典
For c = 1 To UBound(Brr, 2)
'↑內順迴圈去除空白字元
Brr(R, c) = Replace(Brr(R, c), " ", "")
P = P & Brr(R, c) & "|"
'↑把每列4欄的資料用 "|" 串起來
Next
K = Brr(R, 3)
If K Like "*-*-*" Then
K = Split(K, "-")(0) & "-" & Split(K, "-")(1)
ElseIf K = "" Then
K = Brr(R, 1) & Brr(R, 2)
End If
Z(K & "|" & R) = P '@@
'↑重組再加入符號 "|" 與列數
P = ""
Next
For Each Q In Z.KEYS
If V(Split(Q, "|")(0)) = 1 Then
'↑用 "|" 拆解Z字典裡的key,字串在V字典找到,代表符合提取條件
Y(Q) = Split(Z(Q), "|")
'↑用Y字典裝 符合條件 的Z字典item資料 @@處
End If
Next
Arr = Application.Transpose(Application.Transpose(Y.items))
'↑將Y字典的 items 轉置兩次 就是結果資料
Sheets(3).[M1].Resize(Y.Count, 4) = Arr
MsgBox Timer - S & "秒"
End Sub