I have a range of cells like the below:
Col A | Col B | Col C | Col D
apple, orange | red, green | 5 | 10
pear, banana | blue | 20 | 40
I want to get a result that looks like:
Col A
apple-red-5-10
apple-green-5-10
orange-red-5-10
orange-green-5-10
pear-blue-20-40
banana-blue-20-40
I am concatenating values in each row across multiple columns, but if a cell contains a comma separated list of an item, I need to treat each entry as if they belong in a new row with other column data included.
I first reference the range belonging to the four columns I am interested in concatenating.
Dim cellRef1 As Range
Set cellRef1 = ThisWorkbook.Worksheets(1).Range("I2:I60")
Dim cellRef2 As Range
Set cellRef2 = ThisWorkbook.Worksheets(1).Range("E2:E60")
Dim cellRef3 As Range
Set cellRef3 = ThisWorkbook.Worksheets(1).Range("D2:D60")
Dim cellRef4 As Range
Set cellRef4 = ThisWorkbook.Worksheets(1).Range("H2:H60")
Then for the columns which would have comma separated entries (second and fourth), I create an array to store each value e.g.:
Dim Array_Two As Variant
Array_Two = cellRef2.Value
So I would have an array along the lines of
("apple, orange", "pear,banana")
I wanted to loop through each array to create a sub-array where I want to split the entries by a comma and store them in a new array.
e.g. I want:
Array_Two_A = ("apple", "orange")
Array_Two_B = ("pear", "banana)
So I can later access each entry and concatenate them.
I wanted to do this using:
Dim Array_Two_New() As String
For x = LBound(Array_Two) To UBound(Array_Two)
Array_Two_New = Split(Array_Two(x, 1), ",")
Next x
Which kind of works, but the problem is I can't figure out how to dynamically create an array that I can later access.
e.g. as is my code will keep overwriting the Array_Two_New array with the first value the string in Array_Two(x,1) is split into.
I thought I could add Array_Two_New(x) in my code so that I get a result like:
Array_Two_New(1) = ("apple", "orange")
Array_Two_New(2) = ("pear", "banana")
And I could later access the value I want e.g.
Array_Two_New(1)(1) = "apple"
Array_Two_New(1)(2) = "orange"
Array_Two_New(2)(1) = "pear" // etc.
I think I'm confusing the syntax here.
I think I need to use an object but I am not too familiar with them.
I found these articles but I couldn't follow the explanations.
VBA: Split cell values into multiple rows and keep other data
Splitting an array into multiple rows