請測試看看,謝謝
Sub test()
Dim Arr, Brr, T$, i&, i2&
Arr = Range([c1], [b65536].End(3))
Brr = Range([g1], [g65536].End(3))
For i = 2 To UBound(Brr)
T = UCase(Brr(i, 1)): If T = "" Then GoTo 99
For i2 = 2 To UBound(Arr)
If InStr(UCase(Arr(i2, 1)), T) Then
Arr(i2, 2) = Brr(i, 1)
End If
Next
99: Next
[b1].Resize(UBound(Arr), 2) = Arr
End Sub作者: lumark1976 時間: 2022-11-29 09:25
Sub test()
Dim Arr, Brr, T$, i&, i2&
Arr = Range([c1], [b65536].End(3)) 'B、C欄資料裝入Arr
Brr = Range([g1], [g65536].End(3)) 'G欄資料裝入Brr
For i = 2 To UBound(Brr)
T = UCase(Brr(i, 1)): If T = "" Then GoTo 99 '無資料換下一個
For i2 = 2 To UBound(Arr)
If InStr(UCase(Arr(i2, 1)), T) Then '比對G欄資料有無在B欄
Arr(i2, 2) = Brr(i, 1) '有時,G欄資料寫入到Arr
End If
Next
99: Next
[b1].Resize(UBound(Arr), 2) = Arr 'Arr貼回excel
End Sub作者: Andy2483 時間: 2022-11-29 16:44
Option Explicit
Sub TEST_2()
Dim Brr, Grr, Crr, C&, i&, x$, xR, R&, T, V, Y, Z
Dim Sh, Q$(5), A$(5), j&
Set Y = CreateObject("Scripting.Dictionary")
Set Sh = Sheets("PN標準工時")
Brr = Range(Sh.[B2], Sh.Cells(Rows.Count, "B").End(3))
Grr = Range(Sh.[G2], Sh.Cells(Rows.Count, "G").End(3))
ReDim Crr(1 To UBound(Brr), 1 To 1)
For i = 1 To UBound(Grr)
xR = Replace(Replace(Replace(UCase(Grr(i, 1)), "(", "*"), ")", "*"), " ", "*")
Y(xR) = Grr(i, 1)
Next
Q(1) = UCase("CONN POWER JACK")
A(1) = UCase("POWER JACK")
Q(2) = UCase("CONN RJ45")
A(2) = UCase("RJ45")
Q(3) = UCase("Shield")
A(3) = UCase("Shielding")
For Each xR In Y.Keys
For i = 1 To UBound(Brr)
If Crr(i, 1) <> "" Then
GoTo 111
End If
x = UCase(Brr(i, 1))
For j = 1 To 3
x = Replace(x, Q(j), A(j))
Next
If x Like "*" & xR & "*" Then
Crr(i, 1) = Y(xR)
End If
111
Next
Next
Sh.[C2].Resize(UBound(Crr), 1) = Crr
Set Brr = Nothing
Set Grr = Nothing
End Sub作者: Andy2483 時間: 2022-11-30 09:24
Option Explicit
Sub TEST_2()
Dim Brr, Grr, Crr, C&, i&, x$, xR, R&, T, V, Y, Z
Dim Sh, Q$(5), A$(5), j&
'↑宣告變數,Q$(5)是批次宣告從Q(0)~Q(5), A$(5)類推
Set Y = CreateObject("Scripting.Dictionary")
'↑令Y是字典
Set Sh = Sheets("PN標準工時")
'↑令Sh 是工作表
Brr = Range(Sh.[B2], Sh.Cells(Rows.Count, "B").End(3))
'↑令Brr 是陣列!倒入工作表[B2]到B欄最後有內容的儲存格的值
Grr = Range(Sh.[G2], Sh.Cells(Rows.Count, "G").End(3))
'↑令Grr 是陣列!倒入工作表[G2]到G欄最後有內容的儲存格的值
ReDim Crr(1 To UBound(Brr), 1 To 1)
'↑宣告Crr陣列的範圍!縱向從1到Brr陣列縱向最大列號,橫向從1到1欄
For i = 1 To UBound(Grr)
'↑設順迴圈!i從1到 Grr陣列縱向最大列號
xR = Replace(Replace(Replace(UCase(Grr(i, 1)), "(", "*"), ")", "*"), " ", "*")
'↑令xR 是迴圈Grr陣列值經 英文字轉大寫再將 "(" ,")" ," ",這三個字元置換為"*"字元的字串新值
Y(xR) = Grr(i, 1)
'↑令xR 當key,迴圈Grr陣列值當item
Next
Q(1) = UCase("CONN POWER JACK")
'↑令第二個Q字串變數是 將雙引號內文字的英文字置換為大寫後的新字串
A(1) = UCase("POWER JACK")
'↑令第二個A字串變數是 將雙引號內文字的英文字置換為大寫後的新字串
Q(2) = UCase("CONN RJ45")
'↑類推
A(2) = UCase("RJ45")
'↑類推
Q(3) = UCase("Shield")
'↑類推
A(3) = UCase("Shielding")
'↑類推
For Each xR In Y.Keys
'↑設外順迴圈!令xR是 Y字典裡key的一員
For i = 1 To UBound(Brr)
'↑設中順迴圈!i從1 到Brr陣列縱向最大列號
If Crr(i, 1) <> "" Then
'↑如果迴圈Crr陣列值不是空字元
GoTo 111
'↑跳到 111位置繼續執行,代表已經處理過!有值了
End If
x = UCase(Brr(i, 1))
'↑令x是 迴圈Brr陣列值經 英文字置換為大寫後的新字串
For j = 1 To 3
'↑設內順迴圈!j從1 到3
x = Replace(x, Q(j), A(j))
'↑令x是 x經過Q字串變數置換為 A字串變數的新字串
Next
If x Like "*" & xR & "*" Then
'↑如果經過處理過的 x新字串 吻合 xR這key結合前後包夾一個*萬字元的邏輯判斷??
Crr(i, 1) = Y(xR)
'↑如果if條件成立!就讓迴圈Crr陣列值以 xR字串變數當key查察Y字典的item帶入
End If
111
Next
Next
Sh.[C2].Resize(UBound(Crr), 1) = Crr
'↑工作表的[C2]儲存格擴展向下 Crr陣列縱向最大列號數,左右沒再擴展的儲存格範圍,
'倒入Crr陣列的值
Set Brr = Nothing
Set Grr = Nothing
Set Crr = Nothing
Set Y = Nothing
'↑把這些容器從記憶體中釋放掉
End Sub作者: lumark1976 時間: 2022-11-30 13:12