DavidSCowan
Board Regular
- Joined
- Jun 7, 2009
- Messages
- 78
I want to extract the brand names from a column of SKUs (stock keeping units). I know how to do it using standard worksheet functions (i.e. without using vba) and I know how to do it by simple looping. However I want to use arrays because there are several hundred thousand rows of SKUs and looping will take too long (I also want to improve my vba skills – the board has already helped me a lot with this) But I have got stuck in two places and it is driving me mad
Below are example SKUs.
Sure 24hr Intensive Unisex Aerosol Active 150ml Antipers
Addiction Female Aerosol Citrmusk 75ml Nonantpe
Sure For Men 24hr Intensive Male Aerosol Sensitiv 250ml Antipers
Arrid Extra Dry Unisex Aerosol Showfres 200ml Antipers
Soft & Gentle Unisex Aerosol Pureline 250ml Antipers
Adidas Female Aerosol Intensiv 250ml Antipers
The brand names are always followed by one of three gender tags – Unisex, Female or Male. So using this enables the brand names to be extracted. The brand names in the SKUs above are as follows:
Sure 24hr Intensive
Addiction
Sure For Men 24hr Intensive
Arrid Extra Dry
Soft & Gentle
Adidas
Here is the code with some annotations
Can someone help please.
With kind regards
David
Below are example SKUs.
Sure 24hr Intensive Unisex Aerosol Active 150ml Antipers
Addiction Female Aerosol Citrmusk 75ml Nonantpe
Sure For Men 24hr Intensive Male Aerosol Sensitiv 250ml Antipers
Arrid Extra Dry Unisex Aerosol Showfres 200ml Antipers
Soft & Gentle Unisex Aerosol Pureline 250ml Antipers
Adidas Female Aerosol Intensiv 250ml Antipers
The brand names are always followed by one of three gender tags – Unisex, Female or Male. So using this enables the brand names to be extracted. The brand names in the SKUs above are as follows:
Sure 24hr Intensive
Addiction
Sure For Men 24hr Intensive
Arrid Extra Dry
Soft & Gentle
Adidas
Here is the code with some annotations
Code:
Sub ExtractingBrandname()
[COLOR="SeaGreen"]'Starts By Finding The Position Of Gender Because The Brand Name Is The String
'Up Until That Point
[/COLOR]
Dim SKU As Range
Dim Arr
Dim iArr
[COLOR="SeaGreen"]' Set to take all 7 elements of the SKU i.e. not just the brand name[/COLOR]
Set SKU = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp).Offset(, 7))
[COLOR="SeaGreen"]'Set cells in SKU to an Array[/COLOR][/COLOR]
Arr = SKU
'Loop through the array
For n = 1 To UBound(Arr, 1)
[COLOR="SeaGreen"]'Create an array iArr out of the elements of Arr[/COLOR]
iArr = Split(Arr(n, 1), " ")
[COLOR="SeaGreen"]'Find the array position of the words Unisex, Female or Male as these come
'immediately after the brand name
'BUT I CAN'T GET THIS TO WORK - I GET "ELSE WITHOUT IF" ERROR WHAT AM I DOING
'WRONG?[/COLOR]
For i = 0 To UBound(iArr)
If iArr(i) = "Male" Or iArr(i) = "Female" Or iArr(i) = "Unisex" Then
pos = i - 1
End If
Exit For
Else
Next i
[COLOR="SeaGreen"]'What I want to do now is to use pos to extract the words in the cell up
'until the Unisex, Female or Male but I don't know how to do it!
Once I have got this I will write the array back onto the spreadsheet[/COLOR]
Can someone help please.
With kind regards
David