Code that locates a specific character string in a range, and then copies the row that its in and pastes it beneath it...

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
this one is better explained through pictures:

I want to get from this:
Test Worksheet - before-1.jpg

and have the code make this: (where it is copying the entire row if it finds a cell in that row that contains the string ", " and then inserts 2 new copied rows beneath the target row...)
Test Worksheet - after1.jpg

the code I am playing around with looks to be on the right path, but I cant get it to find the right character string (", " comma followed by a space) in the right column (column C)...
thats my first hurdle... once I get that right, then I need to figure out how to make the correct number of copies of the target row and insert the copied row beneath it.

Is this even possible or am I asking too much of VBA to accomplish? Thanks for any help.

current code:

VBA Code:
Private Sub CommandButton1_Click()
Dim rng As Range, Lstrw As Long, RecordRow As Long, c As Range
    Dim SpltRng As Range
    Dim i As Integer
    Dim Orig As Variant
    Dim txt As String

    Lstrw = Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = Range("C3:C" & Lstrw)
'
        For Each c In rng.Cells
        Set SpltRng = c.Offset(, 3)
        txt = SpltRng.Value
        Orig = Split(txt, ",")

        For i = 0 To UBound(Orig)
            Cells(Rows.Count, "B").End(xlUp).Offset(1) = c
            Cells(Rows.Count, "B").End(xlUp).Offset(, 1) = Orig(i)
        Next i
    Next c
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Or just use Power query
Book1
ABCDE
1ColAColBColCColDColE
2Pumpkin Spi ce Linen Spray RTU Ademark Products, Inc.31573M-1347D-1347
3MagnusBA500CERATECHM-552D-552
4ConvoCareMagnus18151, CC202, CC204M-1238D-1238
5Liquid Minera s Group, Ltd.571M25M-22.10D-2210
6SPEED DRI CHENEYSeatex, LLC21945M-345D-345
7LiMiN w/o Mol AdditiveLevity CropSci ence LtdADD0005-01M-1097D-1097
8NOVEL TDA-8 EthoxylateSASOL North America Inc.M-2159D-2159
9eMi croRec1 aimCreative Products23385D-226
10WITCONATE P-1220AKZO NOBEL SURFACE CHEMISTRYM-2460D-2460
11PEED DRI NEW (ROSS)Seatex, LLC21941M-1191D-1191
12itrus CleanMagnus600004, RD102M-1278D-1278
13DREXX PROFESSIONALKoch Agronomic Servi ces, LLCM-1047D-1047
14Non-Chlor RP 04-8ADT, Inc.26066M-1460D-1460
15NP-FOT MAXNewport Fuel Solutions Inc.M-1811D-1811
16PUR PHOS 1.2Purify21033M-1178D-1178
17FOX BATHSeatex22730M —456D-456
18WF LiquidMagnus15485,WF9908M-2219D-2219
19Super XSeat ex, Ltd.22220M-329D-329
20Whi tewal 1Seat ex22981M-1313D-1313
Sheet1


Book1
ABCDE
1ColAColBValueColDColE
2Pumpkin Spi ce Linen Spray RTU Ademark Products, Inc.31573M-1347D-1347
3ConvoCareMagnus18151M-1238D-1238
4ConvoCareMagnus CC202M-1238D-1238
5ConvoCareMagnus CC204M-1238D-1238
6Liquid Minera s Group, Ltd.571M25M-22.10D-2210
7SPEED DRI CHENEYSeatex, LLC21945M-345D-345
8LiMiN w/o Mol AdditiveLevity CropSci ence LtdADD0005-01M-1097D-1097
9eMi croRec1 aimCreative Products23385D-226
10PEED DRI NEW (ROSS)Seatex, LLC21941M-1191D-1191
11itrus CleanMagnus600004M-1278D-1278
12itrus CleanMagnus RD102M-1278D-1278
13Non-Chlor RP 04-8ADT, Inc.26066M-1460D-1460
14PUR PHOS 1.2Purify21033M-1178D-1178
15FOX BATHSeatex22730M —456D-456
16WF LiquidMagnus15485M-2219D-2219
17WF LiquidMagnusWF9908M-2219D-2219
18Super XSeat ex, Ltd.22220M-329D-329
19Whi tewal 1Seat ex22981M-1313D-1313
Table1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ColA", type text}, {"ColB", type text}, {"ColC", type any}, {"ColD", type text}, {"ColE", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type", {{"ColC", type text}}, "en-IE"), "ColC", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"ColC.1", "ColC.2", "ColC.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ColC.1", type text}, {"ColC.2", type text}, {"ColC.3", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"ColA", "ColB", "ColD", "ColE"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"ColA", "ColB", "Value", "ColD", "ColE"}),
    #"Filtered Rows" = Table.SelectRows(#"Reordered Columns", each true)
in
    #"Filtered Rows"
 
Upvote 0
Solution
Or just use Power query
Book1
ABCDE
1ColAColBColCColDColE
2Pumpkin Spi ce Linen Spray RTU Ademark Products, Inc.31573M-1347D-1347
3MagnusBA500CERATECHM-552D-552
4ConvoCareMagnus18151, CC202, CC204M-1238D-1238
5Liquid Minera s Group, Ltd.571M25M-22.10D-2210
6SPEED DRI CHENEYSeatex, LLC21945M-345D-345
7LiMiN w/o Mol AdditiveLevity CropSci ence LtdADD0005-01M-1097D-1097
8NOVEL TDA-8 EthoxylateSASOL North America Inc.M-2159D-2159
9eMi croRec1 aimCreative Products23385D-226
10WITCONATE P-1220AKZO NOBEL SURFACE CHEMISTRYM-2460D-2460
11PEED DRI NEW (ROSS)Seatex, LLC21941M-1191D-1191
12itrus CleanMagnus600004, RD102M-1278D-1278
13DREXX PROFESSIONALKoch Agronomic Servi ces, LLCM-1047D-1047
14Non-Chlor RP 04-8ADT, Inc.26066M-1460D-1460
15NP-FOT MAXNewport Fuel Solutions Inc.M-1811D-1811
16PUR PHOS 1.2Purify21033M-1178D-1178
17FOX BATHSeatex22730M —456D-456
18WF LiquidMagnus15485,WF9908M-2219D-2219
19Super XSeat ex, Ltd.22220M-329D-329
20Whi tewal 1Seat ex22981M-1313D-1313
Sheet1


Book1
ABCDE
1ColAColBValueColDColE
2Pumpkin Spi ce Linen Spray RTU Ademark Products, Inc.31573M-1347D-1347
3ConvoCareMagnus18151M-1238D-1238
4ConvoCareMagnus CC202M-1238D-1238
5ConvoCareMagnus CC204M-1238D-1238
6Liquid Minera s Group, Ltd.571M25M-22.10D-2210
7SPEED DRI CHENEYSeatex, LLC21945M-345D-345
8LiMiN w/o Mol AdditiveLevity CropSci ence LtdADD0005-01M-1097D-1097
9eMi croRec1 aimCreative Products23385D-226
10PEED DRI NEW (ROSS)Seatex, LLC21941M-1191D-1191
11itrus CleanMagnus600004M-1278D-1278
12itrus CleanMagnus RD102M-1278D-1278
13Non-Chlor RP 04-8ADT, Inc.26066M-1460D-1460
14PUR PHOS 1.2Purify21033M-1178D-1178
15FOX BATHSeatex22730M —456D-456
16WF LiquidMagnus15485M-2219D-2219
17WF LiquidMagnusWF9908M-2219D-2219
18Super XSeat ex, Ltd.22220M-329D-329
19Whi tewal 1Seat ex22981M-1313D-1313
Table1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ColA", type text}, {"ColB", type text}, {"ColC", type any}, {"ColD", type text}, {"ColE", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type", {{"ColC", type text}}, "en-IE"), "ColC", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"ColC.1", "ColC.2", "ColC.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"ColC.1", type text}, {"ColC.2", type text}, {"ColC.3", type text}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"ColA", "ColB", "ColD", "ColE"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"ColA", "ColB", "Value", "ColD", "ColE"}),
    #"Filtered Rows" = Table.SelectRows(#"Reordered Columns", each true)
in
    #"Filtered Rows"

Wow. I am not (was not) familiar at all with Power Query, but after trying it out and inserting the M code you provided, I am blown away. Thank you, THANK YOU! This will work perfectly!
-Keith
Power_Query_ERROR.JPG
 
Upvote 0
sorry, I posted the wrong picture... I had some issues early on, but it was because I hadn't converted it to a table first.
(pic I meant to post)
Power_Query_WORKING1.JPG
 
Upvote 0
Power Query a hidden gem, can do a lot of the data sorting and transformation that people use VBA for, but Power query is very clear in the steps applied and if you make a mistake remove the step and try another approach ( the code is generated as you apply the steps). Once you get into it one of the best things in Excel for those of us not gifted in the VBA way :biggrin:
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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