VBA macro for combining values from different categories

jjxl1

New Member
Joined
Oct 14, 2013
Messages
10
I have data organized in Sheet1 in range A1:G4 with the first table below; row 1 is a header row. Columns A and B have the lower and upper bounds of a range of Sizes. C:E have Colors and F:G have Shapes

Consider the following data in Sheet1:

ABCDEFG
1SizeSizeColorColorColorShapeShape
213
368RedBlue
456RedBlueWhiteSquareCircle

<tbody>
</tbody>

I am trying to create a macro that will populate Rows in Sheet2 with 1 Size, 1 Color and 1 Shape (where applicable). Note that there will always be a Size but not always Color and Shape. Also, if it helps, there will only be Shape if there is Color. So either (Size Color Shape), (Size Color) or just (Size).

I am trying to get an output in Sheet 2 that looks like what I have below. Since the Size indicate the lower and upper range of Size, I need to fill in between those sizes in increments of 0.5.

Output I am looking for:

ABC
11
21.5
32
42.5
53
66Red
76.5Red
87Red
97.5Red
108Red
116Blue
126.5Blue
137Blue
147.5Blue
158Blue
165RedSquare
175.5RedSquare
186RedSquare
195RedCircle
205.5RedCircle
216RedCircle
225BlueSquare
235.5BlueSquare
246BlueSquare
265BlueCircle
275.5BlueCircle
286BlueCircle
295WhiteSquare
305.5WhiteSquare
316WhiteSquare
325WhiteCircle
335.5WhiteCircle
346WhiteCircle

<tbody>
</tbody>

In Sheet 1, I have hundreds of rows that have various combinations of Size, Color and Shape and am hoping to find a VBA macro that can help do this quickly. Thank you so much in advance!
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
try the following macro. it has worked for me with your data.
Sub arrange_JJXL1()
b = 2
Sheets("sheet2").UsedRange.ClearContents
x = Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For a = 3 To x
e = WorksheetFunction.CountA(Sheets("sheet1").Range("D" & a & ":F" & a))
g = WorksheetFunction.CountA(Sheets("sheet1").Range("g" & a & ":H" & a))

For f = 0 To e
For h = 0 To g
For d = Sheets("sheet1").Cells(a, 2) To Sheets("sheet1").Cells(a, 3) Step 0.5

Sheets("sheet2").Cells(b, 1) = b - 1
Sheets("sheet2").Cells(b, 2) = d
If f <> 0 Then
Sheets("sheet2").Cells(b, 3) = Sheets("sheet1").Cells(a, f + 3)
Sheets("sheet2").Cells(b, 4) = Sheets("sheet1").Cells(a, h + 6)
End If
b = b + 1
Next d
Next h
Next f
Next a
MsgBox "complete"
End Sub
ravishankar
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,938
Members
449,197
Latest member
k_bs

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