SOLVED

Split data with the help of VBA

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3167074%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3ESplit%20data%20with%20the%20help%20of%20VBA%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3167074%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EHello%20Everyone%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%26nbsp%3Bam%20trying%20to%20create%20a%20unique%20list%20from%20the%20given%20input.%20A%20sample%20data%20as%20input%20and%20corresponding%20output%20which%20is%20expected%2C%20I%20have%20attached%20here.%20any%20help%20on%20writing%20a%20vba%20would%20be%20appreciated.%26lt%3B%5C%2FSPAN%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348317i1139027E367DC9D8%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22Screenshot%202022-02-15%20165251.png%22%202022-02-15%3D%22%22%20165251.png%3D%22%22%20alt%3D%22Screenshot%202022-02-15%20165251.png%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FSPAN%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%26lt%3B%5C%2FSPAN%26gt%3BI%26nbsp%3Bwant%20to%20split%20data.%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EDescription%20of%20data%20set%20%3A%26lt%3B%5C%2FSTRONG%26gt%3B%3CSPAN%3E%26nbsp%3B%26lt%3B%5C%2FSPAN%26gt%3BIn%20input%20table%2C%20you%20can%20see%206%20rows%20excluding%20header%20row.%26nbsp%3B%20In%20cat1%20column%20for%20'a'%20value%20there%20is%20corresponding%20two%20different%20values%20in%20cat2%20column%20and%20blank%20cell%20in%20column2.%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3EI%20am%20trying%20to%20do%20below%20two%20things%20%3A%26lt%3B%5C%2FSTRONG%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E1.%26nbsp%3B%20Eliminate%20rows%20which%20has%20blank%20cell%20in%20either%20cat1%20or%20cat2.%20In%20the%20shared%20sample%20data%20set%2C%20it%20is%20available%20in%20cat2%20only.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E2.%20Create%20two%20tables%20from%20the%20input%20table%20with%20below%20features%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Ba.%20Table%201%20%3A%20only%20contain%20values%20from%20cat1%20column%20which%20has%20one%20to%20one%20mapping%20with%20corresponding%20values%20in%20cat2%20column%20e.g.%20%22c%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bb.%20Table%202%20%3A%26nbsp%3Bonly%20contain%20values%20from%20cat1%20column%20which%20has%20multiple%20mapping%20with%20corresponding%20values%20in%20cat2%20column%20e.g.%20%22a%22%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ESo%2C%20what%20should%20i%20write%20in%20VBA%20%3F%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EPlease%20help..%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EHere%20is%20a%20attached%20file..%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3167074%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3167074%22%20slang%3D%22en-US%22%3ESplit%20data%20with%20the%20help%20of%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3167074%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Everyone%2C%3C%2FP%3E%3CP%3E%3CSPAN%3EI%26nbsp%3Bam%20trying%20to%20create%20a%20unique%20list%20from%20the%20given%20input.%20A%20sample%20data%20as%20input%20and%20corresponding%20output%20which%20is%20expected%2C%20I%20have%20attached%20here.%20any%20help%20on%20writing%20a%20vba%20would%20be%20appreciated.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202022-02-15%20165251.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348317i1139027E367DC9D8%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202022-02-15%20165251.png%22%20alt%3D%22Screenshot%202022-02-15%20165251.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EI%26nbsp%3Bwant%20to%20split%20data.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EDescription%20of%20data%20set%20%3A%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EIn%20input%20table%2C%20you%20can%20see%206%20rows%20excluding%20header%20row.%26nbsp%3B%20In%20cat1%20column%20for%20'a'%20value%20there%20is%20corresponding%20two%20different%20values%20in%20cat2%20column%20and%20blank%20cell%20in%20column2.%3C%2FP%3E%3CP%3E%3CSTRONG%3EI%20am%20trying%20to%20do%20below%20two%20things%20%3A%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E1.%26nbsp%3B%20Eliminate%20rows%20which%20has%20blank%20cell%20in%20either%20cat1%20or%20cat2.%20In%20the%20shared%20sample%20data%20set%2C%20it%20is%20available%20in%20cat2%20only.%3C%2FP%3E%3CP%3E2.%20Create%20two%20tables%20from%20the%20input%20table%20with%20below%20features%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Ba.%20Table%201%20%3A%20only%20contain%20values%20from%20cat1%20column%20which%20has%20one%20to%20one%20mapping%20with%20corresponding%20values%20in%20cat2%20column%20e.g.%20%22c%22%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3Bb.%20Table%202%20%3A%26nbsp%3Bonly%20contain%20values%20from%20cat1%20column%20which%20has%20multiple%20mapping%20with%20corresponding%20values%20in%20cat2%20column%20e.g.%20%22a%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%2C%20what%20should%20i%20write%20in%20VBA%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20help..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20a%20attached%20file..%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3167074%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3167450%22%20slang%3D%22en-US%22%3ERe%3A%20Split%20data%20with%20the%20help%20of%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3167450%22%20slang%3D%22en-US%22%3EThank%20you%20so%20much%20sir.%3C%2FLINGO-BODY%3E
Super Contributor

Hello Everyone,

I am trying to create a unique list from the given input. A sample data as input and corresponding output which is expected, I have attached here. any help on writing a vba would be appreciated.

Screenshot 2022-02-15 165251.png

 

 I want to split data. 

Description of data set : In input table, you can see 6 rows excluding header row.  In cat1 column for 'a' value there is corresponding two different values in cat2 column and blank cell in column2.

I am trying to do below two things :

1.  Eliminate rows which has blank cell in either cat1 or cat2. In the shared sample data set, it is available in cat2 only.

2. Create two tables from the input table with below features:

     a. Table 1 : only contain values from cat1 column which has one to one mapping with corresponding values in cat2 column e.g. "c"

     b. Table 2 : only contain values from cat1 column which has multiple mapping with corresponding values in cat2 column e.g. "a"

 

So, what should i write in VBA ?

 

Please help..

 

Here is a attached file..

7 Replies
best response confirmed by Peter Bartholomew (Trusted Contributor)
Solution

@Zan_Hanifee 

Sub SplitData()
    Dim r As Long
    Dim s As Long
    Dim t As Long
    Dim m As Long
    Application.ScreenUpdating = False
    m = Range("C:D").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("I5:M" & Rows.Count).Clear
    s = 4
    t = 4
    For r = 5 To m
        If Range("C" & r).Value <> "" And Range("D" & r).Value <> "" Then
            If Application.CountIfs(Range("C5:C" & m), Range("C" & r).Value, Range("D5:D" & m), "<>") = 1 Then
                s = s + 1
                Range("C" & r).Resize(1, 2).Copy Destination:=Range("I" & s)
            Else
                t = t + 1
                Range("C" & r).Resize(1, 2).Copy Destination:=Range("L" & t)
            End If
        End If
    Next r
    Application.ScreenUpdating = True
End Sub
Thank you so much sir.

@Hans Vogelaar 

Hello Sir,

Can you please explain above VBA code?

@Zan_Hanifee 

The code starts by finding the last used row in columns C and D. This is assigned to the variable m.

Next, it removes existing results in columns I to M below row 4.

The variables s and t are initialized to 4 (the header row for the results). s is for columns I and J, t for columns L and M.

The variable r loops through rows 5 to m. If both column C and column D in row r are not blank, we use COUNTIFs to check whether the value in column C occurs only once, with a non-blank value in D, or more than once. If once, increase the row number s and copy C / D to I /J, otherwise increase the row number t, and copy C / D to L / M.

@Zan_Hanifee 

... and without VBA

"Multiplicity=1"
= LET(
    multiplicity, COUNTIFS(Cat_1,Cat_1, Cat_2,"<>"),
    FILTER(input, (multiplicity=1)*(Cat_2<>""))
  )

"Multiplicity>1"
= LET(
    multiplicity, COUNTIFS(Cat_1,Cat_1, Cat_2,"<>"),
    FILTER(input, (multiplicity>1)*(Cat_2<>""))
  )

@Hans Vogelaar 

Thank you so much sir. 

Thank you so much sir.