[VBA] Create categories in Excel including or excluding certain keywords?

ottame

New Member
Joined
Nov 29, 2018
Messages
2
Hi, I've just signed up because I'm struggling with a categorization problem and hope that I can find some help here.

I need to categorize a list of items, depending on whether they contain some keywords and at the same time do not contain others. These are the categories that I need to create, together with the rules ("Contiene" means contains and "No contiene" means does not contain):

fmM4o.png


The category "ENCIMERAS", for example, should group all items that contain "encimera" and that at the same time do not contain "cocina", "cuarzo", "granito" or "gris".

On the other hand, the category "ENCIMERAS DE COCINA" should include all items that contain "encimera" and also "cocina" (not necessarily in this order) but that do not contain "cuarzo", "granito" or "gris".
This is a sample of the elements I'm working with and the expected outcome:

FWmyy.png


Due to the filters I'm using, not every element will fall in a category, some of them will remain uncategorized, I am aware, it's not an error.

I've tried to achieve the categorization with a regular expression, but couldn't find an expression that can include some keywords and exclude some others at the same time.

I thought that maybe it could be achieved with VBA, but I don't know where to start. My guess is that i should create an expression like this:

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">IF [Consulta] CONTAINS "encimera" AND "cocina"
AND NOT CONTAINS "cuarzo" OR "granito" OR "gris"
THEN "ENCIMERAS DE COCINA"
ELSEIF....
ELSE "OTROS"

</code>How do I translate contains and does not contain into VBA language? And, more important, is it even possible to do such thing?

If you want to see and manipulate the sample data, I've put them in a spreadsheet: google-speadsheet-sample-data

Thanks in advance,
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi, I've just signed up because I'm struggling with a categorization problem and hope that I can find some help here.

I need to categorize a list of items, depending on whether they contain some keywords and at the same time do not contain others. These are the categories that I need to create, together with the rules ("Contiene" means contains and "No contiene" means does not contain):

I thought that maybe it could be achieved with VBA, but I don't know where to start. My guess is that i should create an expression like this:

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">IF [Consulta] CONTAINS "encimera" AND "cocina"
AND NOT CONTAINS "cuarzo" OR "granito" OR "gris"
THEN "ENCIMERAS DE COCINA"
ELSEIF....
ELSE "OTROS"

</code>

You can use InStr function, like this:

Code:
For Each r In somerange
    If InStr(1, r, "encimera", 1) > 0 And InStr(1, r, "cocina", 1) > 0 And InStr(1, r, "cuarzo", 1) = 0 _
        And InStr(1, r, "granito", 1) = 0 And InStr(1, r, "gris", 1) = 0 Then
        r.Offset(, 1) = "ENCIMERAS DE COCINA"
    ELSEIF....
    
    End If
Next

To learn about InStr function:
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/instr-function
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top