Duplicating Rows based on Value from another table

%3CLINGO-SUB%20id%3D%22lingo-sub-3334974%22%20slang%3D%22en-US%22%3EDuplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3334974%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20two%20tables%20of%20data.%20Both%20will%20be%20held%20on%20two%20separate%20sheets%20in%20a%20workbook.%20Where%20there%20is%20a%20duplicate%20value%20in%20Table%202%2C%20I%20need%20to%20duplicate%20the%20row%20of%20data%20which%20contains%20this%20value%20in%20table%201.%20Both%20duplicate%20values%20need%20to%20be%20recorded%20on%20table%20one.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20this%20be%20done%20via%20Formulae%20or%20Conditional%20Formatting%3F%20Or%20would%20VBA%20be%20the%20best%20option%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBefore%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22michaeldee_0-1651862777866.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369889i73A481113A8F068F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22michaeldee_0-1651862777866.png%22%20alt%3D%22michaeldee_0-1651862777866.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22michaeldee_1-1651862798814.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F369890i6305B307FDB44363%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22michaeldee_1-1651862798814.png%22%20alt%3D%22michaeldee_1-1651862798814.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3334974%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3335140%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3335140%22%20slang%3D%22en-US%22%3ECould%20you%20upload%20a%20sample%20Workbook%20%3F%20I%20don't%20quite%20understand%20from%20the%20images....%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336137%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336137%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F421224%22%20target%3D%22_blank%22%3E%40Cambosity100%3C%2FA%3E%26nbsp%3BSure.%20See%20attached.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20the%20sheet%20called%20%22Data%20to%20Load%22%20-%20This%20is%20a%20sheet%20which%20will%20be%20used%20to%20prepare%20data%20for%20loading%20into%20SQL.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20the%20sheet%20called%20%22Reference%20Data%22%2C%20this%20is%20data%20which%20will%20be%20used%20for%20referencing%2C%20in%20otherwords%2C%20we%20will%20perform%20a%20vlookups%20to%20this%20sheet%20to%20return%20the%20level%20ID%20associated%20with%20the%20Level%20Code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20example%20on%20the%20attached%2C%20there%20are%20two%20level%20codes%20with%20the%20value%20%22SO%22%20in%20the%20column%20called%20Level%20Code%20on%20the%20sheet%20called%20reference%20data.%20Both%20have%20two%20unique%20IDs%20and%20therefore%2C%20both%20are%20valid.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOn%20the%20tab%20called%20%22Data%20to%20Load%22%2C%20there%20is%20one%20record%20which%20contains%20the%20Level%20Code%20of%20SO%20in%20the%20column%20called%20Level%20Code.%20However%2C%20as%20there%20are%20actually%20two%20distinct%20records%20for%20SO%20(as%20shown%20on%20the%20reference%20data%20sheet)%2C%20we%20would%20need%20to%20duplicate%20the%20line%20in%20yellow%2C%20one%20showing%20level%20ID%206%20and%20the%20other%20showing%20level%20id%20834.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20hope%20this%20explains%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336139%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336139%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1136972%22%20target%3D%22_blank%22%3E%40michaeldee%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336250%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336250%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1136972%22%20target%3D%22_blank%22%3E%40michaeldee%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESub%20duplicates()%0A%0ADim%20i%20As%20Integer%0ADim%20j%20As%20Integer%0ADim%20w%20As%20Integer%0ADim%20z%20As%20Integer%0A%0ARange(%22I%3AJ%22).Clear%0A%0Az%20%3D%203%0AFor%20i%20%3D%203%20To%2020%0A%0Aj%20%3D%20Application.WorksheetFunction.CountIf(Range(Cells(3%2C%205)%2C%20Cells(14%2C%205))%2C%20Cells(i%2C%202))%0A%0AIf%20j%20%3D%201%20Then%0A%0ARange(Cells(i%2C%202)%2C%20Cells(i%2C%203)).Copy%20Cells(z%2C%209)%0Az%20%3D%20z%20%2B%201%0A%0AElse%0A%0AIf%20j%20%3D%202%20Then%0A%0Aw%20%3D%20Application.WorksheetFunction.Match(Cells(i%2C%202)%2C%20Range(Cells(3%2C%205)%2C%20Cells(14%2C%205))%2C%200)%20%2B%202%0A%0AIf%20Cells(i%2C%203).Value%20%3D%20Cells(w%2C%206).Value%20Then%0A%0ARange(Cells(w%2C%205)%2C%20Cells(w%20%2B%201%2C%206)).Copy%20Range(Cells(z%2C%209)%2C%20Cells(z%20%2B%201%2C%2010))%0Az%20%3D%20z%20%2B%202%0A%0AElse%0ARange(Cells(i%2C%202)%2C%20Cells(i%2C%203)).Copy%20Cells(z%2C%209)%0Az%20%3D%20z%20%2B%201%0A%0AEnd%20If%0A%0AElse%0ARange(Cells(i%2C%202)%2C%20Cells(i%2C%203)).Copy%20Cells(z%2C%209)%0Az%20%3D%20z%20%2B%201%0A%0AEnd%20If%0A%0AEnd%20If%0A%0ANext%20i%0A%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EMaybe%20with%20these%20lines%20of%20code.%20You%20can%20click%20the%20button%20in%20cell%20M2%20in%20the%20attached%20file%20to%20start%20the%20macro.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETable2%20has%20to%20be%20sorted%20by%20the%20Level%20Codes%20and%20there%20can%20be%20duplicate%20Level%20Codes%20but%20no%20triplicates%2C%20quadruplicates%20and%20so%20on.%20If%20i%20understand%20your%20question%20correctly%20there%20must%20be%20an%20exact%20match%20for%20Level%20Code%20and%20Level%20ID.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3336746%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3336746%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1136972%22%20target%3D%22_blank%22%3E%40michaeldee%3C%2FA%3E%26nbsp%3BAs%20a%20variant%2C%20PowerQuery%20can%20do%20this%20swiftly%20without%20having%20to%20bother%20about%20VLOOKUP.%20Attached%20file%20contains%20a%20working%20example%20based%20on%20the%20data%20set%20you%20provided.%20PQ%20connects%20to%20the%20blue%20tables%20and%20spits%20out%20the%20green%20one%20(in%20the%20Upload%20sheet)%20upon%20a%20refresh.%20No%20VBA%20needed%20and%20much%20easier%20to%20maintain%20and%20adapt.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3337756%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3337756%22%20slang%3D%22en-US%22%3EThank%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1174419%22%20target%3D%22_blank%22%3E%40Quadruple_Pawn%3C%2FA%3E!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3337757%22%20slang%3D%22en-US%22%3ERe%3A%20Duplicating%20Rows%20based%20on%20Value%20from%20another%20table%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3337757%22%20slang%3D%22en-US%22%3EThank%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E.%20I%20was%20not%20very%20familiar%20with%20Power%20Query%20before%20today.%20However%2C%20I%20think%20I%20now%20have%20a%20solution%20as%20a%20result.%20Thank%20you%20very%20much%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi,

 

I have two tables of data. Both will be held on two separate sheets in a workbook. Where there is a duplicate value in Table 2, I need to duplicate the row of data which contains this value in table 1. Both duplicate values need to be recorded on table one.

 

Can this be done via Formulae or Conditional Formatting? Or would VBA be the best option?

 

Before:

michaeldee_0-1651862777866.png

 

 

After:

 

michaeldee_1-1651862798814.png

 

 

7 Replies
Could you upload a sample Workbook ? I don't quite understand from the images....

@Cambosity100 Sure. See attached.

 

On the sheet called "Data to Load" - This is a sheet which will be used to prepare data for loading into SQL. 

 

On the sheet called "Reference Data", this is data which will be used for referencing, in otherwords, we will perform a vlookups to this sheet to return the level ID associated with the Level Code.

 

In the example on the attached, there are two level codes with the value "SO" in the column called Level Code on the sheet called reference data. Both have two unique IDs and therefore, both are valid.

 

On the tab called "Data to Load", there is one record which contains the Level Code of SO in the column called Level Code. However, as there are actually two distinct records for SO (as shown on the reference data sheet), we would need to duplicate the line in yellow, one showing level ID 6 and the other showing level id 834.

 

I hope this explains 

 

 

@michaeldee 

Sub duplicates()

Dim i As Integer
Dim j As Integer
Dim w As Integer
Dim z As Integer

Range("I:J").Clear

z = 3
For i = 3 To 20

j = Application.WorksheetFunction.CountIf(Range(Cells(3, 5), Cells(14, 5)), Cells(i, 2))

If j = 1 Then

Range(Cells(i, 2), Cells(i, 3)).Copy Cells(z, 9)
z = z + 1

Else

If j = 2 Then

w = Application.WorksheetFunction.Match(Cells(i, 2), Range(Cells(3, 5), Cells(14, 5)), 0) + 2

If Cells(i, 3).Value = Cells(w, 6).Value Then

Range(Cells(w, 5), Cells(w + 1, 6)).Copy Range(Cells(z, 9), Cells(z + 1, 10))
z = z + 2

Else
Range(Cells(i, 2), Cells(i, 3)).Copy Cells(z, 9)
z = z + 1

End If

Else
Range(Cells(i, 2), Cells(i, 3)).Copy Cells(z, 9)
z = z + 1

End If

End If

Next i

End Sub

Maybe with these lines of code. You can click the button in cell M2 in the attached file to start the macro.

 

Table2 has to be sorted by the Level Codes and there can be duplicate Level Codes but no triplicates, quadruplicates and so on. If i understand your question correctly there must be an exact match for Level Code and Level ID. 

@michaeldee As a variant, PowerQuery can do this swiftly without having to bother about VLOOKUP. Attached file contains a working example based on the data set you provided. PQ connects to the blue tables and spits out the green one (in the Upload sheet) upon a refresh. No VBA needed and much easier to maintain and adapt.

Thank you @Riny_van_Eekelen. I was not very familiar with Power Query before today. However, I think I now have a solution as a result. Thank you very much