简体   繁体   中英

Divide cell in excel by 3 different string patterns using vba

I have a list with a row in excel which has three different types (pattern) of string:

  1. ABCD, CBFG, EXAL

For the first type it's for example: ABCD

So it will always be a combination of letters and no numbers or anything else.

  1. ABCD (HOLA), CBFG (HOLA), EXAN (HOLA)

For the second type it's for example: ABCD (HOLA)

So it will be always a combination of letters and a combination of letters in brackets.

  1. ABCD (HOLA), SOLE, CBFG (HOLA), SUPRA, EXAN (HOLA), ITAN

For the third type it's for example: ABCD (HOLA), SOLE

So it will be always a combination of letters and a combination of letters in brackets and a comma followed by another combination of letters

All these three types are listed with the following pattern.

Let's say we have: ABCD and ABCD (SOLE) and ABCD (HOLA), SOLE

Then my cell will look as follows:

ABCD, ABCD (HOLA), ABCD (HOLA), SOLE

Now I have the type 3 which also includes a comma. You can say there are two different commas here. One is used to divide each entry and the other one is actually a part of one entry. What I am trying to do now is to get these three types and paste them into another cell.

I don't know how to go about this. If someone can give me an advice on how to start that would be great.

Here is an example how I want my cell to divide the information: In B2 is the information I have. The range H1 to J4 is what I want to have

Soshiribo, I think that will would work for you. You would obviously want to rename and define all of the variables and change the limits on the loop but this should work.

Dim string2() As String
For I = 1 To 3
    Erase string2()
    string1 = ActiveSheet.Cells(I + 1, 2)
    string2() = Split(string1, ",")
    x = UBound(string2) - LBound(string2) + 1

    Select Case x:
        Case Is = 5
            String3 = string2(0) + "," + string2(1)
            String4 = string2(2)
            String5 = string2(3) + "," + string2(4)
            ActiveSheet.Cells(I + 1, 4) = String3
            ActiveSheet.Cells(I + 1, 5) = String4
            ActiveSheet.Cells(I + 1, 6) = String5
    Case Is = 6
            String3 = string2(0) + "," + string2(1)
            String4 = string2(2) + "," + string2(3)
            String5 = string2(4) + "," + string2(5)
            ActiveSheet.Cells(I + 1, 4) = String3
            ActiveSheet.Cells(I + 1, 5) = String4
            ActiveSheet.Cells(I + 1, 6) = String5
    End Select

Next I

If you're looking for a regex, here's an idea:

(\w+)( \(HOLA\))?,? ?(SOLE|SUPRA|ITAN)?

I don't fully understand all the requirements, so this is really just a starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM