Need to combine information from two different worksheets on new worksheet

evisu18

New Member
Joined
Mar 23, 2012
Messages
14
Hello,

Let's say I have

Sheet1 with a column that has a list of colors and Sheet2 has a 4 row x 4 column template with quantities and articles of clothing.

15284999.jpg



I need my macro to

1. Generate Sheet3 and fill in Row 1 with these column names: "T-shirts" , "Sweaters", "Jackets", and "Pants". (This step can be skipped, but it is preferred)
13643524.jpg


2. Copy and paste the template from Sheet2 to Sheet3.
3. Find and Replace 'Blank' with the color column starting with the first. 'Red', 'Yellow', 'Orange'. and so on.
32655061.jpg


4. Macro ends when it reaches a blank value on the color column.

Attached images of screenshots for further clarification.

Thank you for stopping by and your help is very much appreciated!

Also posted on another forum

Need to combine information from two different worksheets on new worksheet
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I've got a neat very flexible solution, but needs a little bit more work. Hope to post it tomorrow(European time)
 
Upvote 0
try this code. It is quite flexible in that it isn't restricted to the number of colours or the number of types of clothing or sizes. And don't blink, it is very fast.

Code:
Option Explicit


Sub CreateClothingOptions()
    Const DELIM = "'Blank'"       '  'Blank' will be replaced by colours
    Dim wsCnS As Worksheet, wsC As Worksheet, wsNL As Worksheet
    Dim rCol As Range, rTempl As Range, rOutp As Range
    Dim arCloth As Variant, arCol As Variant, arNewL As Variant
    Dim j As Long, i As Long, k As Long, lCol As Long, lSize As Long, lTypes As Long
    
    On Error GoTo MisSheet
        Set wsCnS = Sheets("Colors and stuff")
        Set wsC = Sheets("Clothes")
        Set wsNL = Worksheets.Add(After:=wsC)
   '     wsNL.Name = "NewList"
    On Error GoTo 0
    
    Set rCol = wsCnS.[B2]
    Set rTempl = wsC.[A1]
    Set rOutp = wsNL.[A1]
    
    'dimension our working array
    lSize = rTempl.CurrentRegion.Rows.Count
    lTypes = rTempl.CurrentRegion.Columns.Count
    lCol = rCol.CurrentRegion.Rows.Count - 1
    ReDim arNewL(0 To lSize * lCol, 1 To lTypes) 'row 0 for the headers
    
    arCloth = rTempl.CurrentRegion
    arCol = rCol.Resize(lCol, 1)
    
    ' create the headers and add in row 0
    For i = 1 To lTypes
        arNewL(0, i) = GetType(CStr(arCloth(1, i)), DELIM)
    Next i
    
    For i = 1 To lCol
        For j = 1 To lSize
            For k = 1 To lTypes
                arNewL(j + (i - 1) * lSize, k) = Application.WorksheetFunction.Substitute( _
                CStr(arCloth(j, k)), DELIM, CStr(arCol(i, 1)))
                    'SUBSTITUTE(Text,Old_Text,New_Text)
            Next k
        Next j
    Next i
    rOutp.Resize(lSize * lCol + 1, lTypes).Value = arNewL
    Exit Sub
MisSheet:
    MsgBox "Sheets missing or output sheet already exists"
End Sub


Function GetType(sInp As String, sDel As String) As String
    Dim i As Integer, sRes As String
    
    i = InStr(1, sInp, sDel)
    sRes = Right(sInp, Len(sInp) - i - Len(sDel))
    
    GetType = sRes
End Function
 
Last edited:
Upvote 0
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves/> <w:TrackFormatting/> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--> Very impressive! I have only started with visual basic recently, but have never seen code like that. It looks interesting and it is definitely neat. Thank you so much!
But now I have another problem. I changed the first column called “Code” with code numbers to match the colors.
64426777.jpg




The output sheet then has a “Code” column with the code matching the items in each row. It should look like this:
22080955.jpg



Any help will be appreciated. Thanks in advance.
 
Last edited:
Upvote 0
By working in arrays you can do input and output to the sheet real quick. No slow copy pasting, no individual reads or writes. Just load a whole range into an array, do all your calcs in an array and the write an array back to a range.

here independent on how many colours, clothes and sizes you have, there are only two read accesses and one write access to the sheet.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> CreateClothingOptions()<br>    <SPAN style="color:#00007F">Const</SPAN> DELIM = "'Blank'"       <SPAN style="color:#007F00">'  'Blank' will be replaced by colours</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsCnS <SPAN style="color:#00007F">As</SPAN> Worksheet, wsC <SPAN style="color:#00007F">As</SPAN> Worksheet, wsNL <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> rCol <SPAN style="color:#00007F">As</SPAN> Range, rTempl <SPAN style="color:#00007F">As</SPAN> Range, rOutp <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> arCloth <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, arCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, arNewL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lTypes <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> MisSheet<br>        <SPAN style="color:#00007F">Set</SPAN> wsCnS = Sheets("Colors and stuff")<br>        <SPAN style="color:#00007F">Set</SPAN> wsC = Sheets("Clothes")<br>        <SPAN style="color:#00007F">Set</SPAN> wsNL = Worksheets.Add(After:=wsC)<br>   <SPAN style="color:#007F00">'     wsNL.Name = "NewList"</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rCol = wsCnS.[A2]<br>    <SPAN style="color:#00007F">Set</SPAN> rTempl = wsC.[A1]<br>    <SPAN style="color:#00007F">Set</SPAN> rOutp = wsNL.[A1]<br>    <br>    <SPAN style="color:#007F00">'dimension our working array</SPAN><br>    lSize = rTempl.CurrentRegion.Rows.Count<br>    lTypes = rTempl.CurrentRegion.Columns.Count<br>    lCol = rCol.CurrentRegion.Rows.Count - 1<br>    <SPAN style="color:#00007F">ReDim</SPAN> arNewL(0 <SPAN style="color:#00007F">To</SPAN> lSize * lCol, 1 <SPAN style="color:#00007F">To</SPAN> lTypes + 1) <SPAN style="color:#007F00">'row 0 for the headers</SPAN><br>    <br>    arCloth = rTempl.CurrentRegion<br>    arCol = rCol.Resize(lCol, 2)    <SPAN style="color:#007F00">'code & color</SPAN><br>    <br>    <SPAN style="color:#007F00">' create the headers and add in row 0</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> lTypes<br>        arNewL(0, i) = GetType(CStr(arCloth(1, i)), DELIM)<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    arNewL(0, i) = "ColCode"<br>    <br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> lCol<br>        <SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> lSize<br>            <SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> lTypes<br>                arNewL(j + (i - 1) * lSize, k) = Application.WorksheetFunction.Substitute( _<br>                <SPAN style="color:#00007F">CStr</SPAN>(arCloth(j, k)), DELIM, <SPAN style="color:#00007F">CStr</SPAN>(arCol(i, 2)))<br>                    <SPAN style="color:#007F00">'SUBSTITUTE(Text,Old_Text,New_Text)</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> k<br>            arNewL(j + (i - 1) * lSize, k) = arCol(i, 1)<br>        <SPAN style="color:#00007F">Next</SPAN> j<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    rOutp.Resize(lSize * lCol + 1, lTypes + 1).Value = arNewL<br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>MisSheet:<br>    MsgBox "Sheets missing or output sheet already exists"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Function</SPAN> GetType(sInp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sDel <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, sRes <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <br>    i = InStr(1, sInp, sDel)<br>    sRes = Right(sInp, Len(sInp) - i - Len(sDel))<br>    <br>    GetType = sRes<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN><br><br></FONT>
 
Upvote 0
From Private Message:
evisu18 said:
Thank you very much for the help with the Macro. It is very neat and I have actually never seen this method used before. But now I have another problem. Instead of the cell values like 10 'Blank' Sweaters, it will have formulas like ='Blank'!$A$1 or ='Blank'!$D$9-'Blank'!$F$9. But the problem is I do not want Excel to try to calculate the formulas, I only want the equations to have the 'Blank' replaced. Do you know how to fix it? I would be very grateful for you help.

Thanks!

Do you also have sheets for each of the colours? I mean if we were to replace the formula
='Blank'!$A$1 with
='Red'!$A$1
would that result in an error, or is that what you want to achieve?
 
Upvote 0
It will help if you can either post a sample spreadsheet somewhere (dropbox or so) or use ExcelGenie to post exactly what is in the various sheets and formulas, else i will be chasing my tail here.
 
Upvote 0
Hello,

Sorry I was away for a while so I did not see your post.

This was the problem that I was trying to solve.

From this Worksheet "Name List",

1. The values in the "New Name" column will be used to replace the 'BLANK' fields in the next worksheet which is called "Template".
2. The corresponding "Code Name" should be filled in the "Template" worksheet

12361371.png



83937495.png



This is a sample with the first two sets filled in. The Equations should not compute, it will be used later for Google Docs. The "New Name" values will actually be names of worksheets in which Google Doc will derive values from.

56693111.png


I uploaded a sample on Dropbox here

https://www.dropbox.com/s/dnrxy7bhk1zh3i8/Sample051613.xlsm


Thank you for your help I appreciate it very much!
 
Upvote 0
OK, using the file on the dropbox (similar to the screenshots above) this is the code you need. It outputs the formulas as text, not as formulas. Let me know if you need them as formulas.

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> CreateFormulas()<br>    <SPAN style="color:#00007F">Const</SPAN> DELIM = "BLANK"       <SPAN style="color:#007F00">'  'Blank' will be replaced by colours</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsNLst <SPAN style="color:#00007F">As</SPAN> Worksheet, wsTplt <SPAN style="color:#00007F">As</SPAN> Worksheet, wsNL <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <SPAN style="color:#00007F">Dim</SPAN> rNN <SPAN style="color:#00007F">As</SPAN> Range, rTempl <SPAN style="color:#00007F">As</SPAN> Range, rOutp <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> arEquations <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, arNN <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, arNewL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>, arHdr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, k <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lShts <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lSize <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lEquations <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> MisSheet<br>        <SPAN style="color:#00007F">Set</SPAN> wsNLst = Sheets("Name List")<br>        <SPAN style="color:#00007F">Set</SPAN> wsTplt = Sheets("Template")<br>        <SPAN style="color:#00007F">Set</SPAN> wsNL = Worksheets.Add(After:=wsTplt)<br>   <SPAN style="color:#007F00">'     wsNL.Name = "NewList"</SPAN><br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rNN = wsNLst.[A2]<br>    <SPAN style="color:#00007F">Set</SPAN> rTempl = wsTplt.[A2]<br>    <SPAN style="color:#00007F">Set</SPAN> rOutp = wsNL.[A1]<br>    <br>    <SPAN style="color:#007F00">' dimension our working array</SPAN><br>    lSize = rTempl.CurrentRegion.Rows.Count - 1<br>    lEquations = rTempl.CurrentRegion.Columns.Count<br>    lShts = rNN.CurrentRegion.Rows.Count - 1<br>    <br>    <SPAN style="color:#00007F">ReDim</SPAN> arNewL(1 <SPAN style="color:#00007F">To</SPAN> lSize * lShts, 1 <SPAN style="color:#00007F">To</SPAN> lEquations)<br>    <br>    <SPAN style="color:#007F00">' Load template and codes into arrays</SPAN><br>    arEquations = rTempl.Resize(lSize, lEquations)<br>    arNN = rNN.Resize(lShts, 2)    <SPAN style="color:#007F00">'code nr & code name</SPAN><br>    <br>    <SPAN style="color:#007F00">' create the headers into the output sheet</SPAN><br>    rOutp.Resize(1, lEquations).Value = rTempl.Offset(-1, 0).Resize(1, lEquations).Value<br>    <br>    <SPAN style="color:#007F00">' populate the otput array with the formulas _<br>      by doing this in arrays it is blindingly fast</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> lShts<br>        <SPAN style="color:#00007F">For</SPAN> j = 1 <SPAN style="color:#00007F">To</SPAN> lSize<br>            <SPAN style="color:#00007F">For</SPAN> k = 1 <SPAN style="color:#00007F">To</SPAN> lEquations<br>                arNewL(j + (i - 1) * lSize, k) = "'" & Application.WorksheetFunction.Substitute( _<br>                CStr(arEquations(j, k)), DELIM, MakeWorksheetName(CStr(arNN(i, 1)), CStr(arNN(i, 2))))<br>                    <SPAN style="color:#007F00">'SUBSTITUTE(Text,Old_Text,New_Text) _<br>                    as the functions expect strings, and the arrays are variants, you have to _<br>                    cast the array members to string with CStr()</SPAN><br>            <SPAN style="color:#00007F">Next</SPAN> k<br>            <SPAN style="color:#007F00">' Put te code number in column 2 of the array</SPAN><br>            arNewL(j + (i - 1) * lSize, 2) = arNN(i, 1)<br>        <SPAN style="color:#00007F">Next</SPAN> j<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <br>    <SPAN style="color:#007F00">' Now output the array to the sheet, one line below the header</SPAN><br>    rOutp.Offset(1, 0).Resize(lSize * lShts + 1, lEquations).Value = arNewL<br>    <br>    <SPAN style="color:#007F00">'finished</SPAN><br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>MisSheet:<br>    MsgBox "Sheets missing or output sheet already exists"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br><br><SPAN style="color:#00007F">Function</SPAN> MakeWorksheetName(sCdNr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, sCdNm <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br> <br>    MakeWorksheetName = sCdNr & Mid(sCdNm, 1, 18) & "_EMS"<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>
 
Upvote 0
I forgot to mention: you don't need to combine the code & code number first, this will be done in the macro with the function 'MakeSheetName' which is based on the macro you had to do it on the sheet. Now it is done on the fly.
 
Upvote 0

Forum statistics

Threads
1,207,392
Messages
6,078,219
Members
446,322
Latest member
pebuje

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