0
我想創建一個循環變量,通過字符串查找字符串數組,並將它們分配給組,如果找到匹配,但是,我不'如果源字符串與搜索字符串相同,就需要它是完全匹配的。示例代碼貼在下面:Excel VBA搜索字符串中的數組字符串
Sub add_Categories()
Dim rRange As Range, rCell As Range
Dim wSheet As Worksheet
Dim wSheetStart As Worksheet
Dim strText As String
Set wSheetStart = ActiveSheet
wSheetStart.AutoFilterMode = False
Set rRange = Range("B1", Range("B65536").End(xlUp))
Application.DisplayAlerts = False
With wSheetStart
For Each rCell In rRange
If rCell Like "*Apple*" Then rCell.Offset(0, 2) = "Grocery"
If rCell Like "*Orange*" Then rCell.Offset(0, 2) = "Grocery
If rCell Like "*Mop*" Then rCell.Offset(0, 2) = "Kitchen"
If rCell Like "*Broom*" Then rCell.Offset(0, 2) = "Kitchen"
'If rCell Like "*Shirt*" Then rCell.Offset(0, 2) = "Clothing"
'If rCell Like "*Pants*" Then rCell.Offset(0, 2) = "Clothing"
Next rCell
End With
With wSheetStart
'.AutoFilterMode = False
.Activate
End With
On Error GoTo 0
Application.DisplayAlerts = True
End Sub
上面的例子只有每個類別的兩個字符串,但在現實中我有上百個,這將是更容易進入他們作爲數組,而不是爲每個語句行。任何幫助深表感謝。
不確定您是否知道,但您的代碼在第二個雜貨實例的末尾缺少雙引號。 –
如果您擁有如此多的產品和類別對:如果將它們放在隱藏表格中,而不是將它們硬編碼到數組中,會更容易嗎? – Jochen