Question

I am creating a spreadsheet full of products for an online shop and I really need some help with one of the fields.

I have a parent product with several child products (think T-shirts with different sizes and colours).

To keep things simple lets work with two colours (red & blue) and two sizes (small & large) - this gives 4 possible different combinations of product (ie small red, small blue, large red & large blue)

This data is listed on my sheet like so:

sku          colour      size       price
t-shirt-rs   red         small      0
t-shirt-rl   red         large      2
t-shirt-bs   blue        small      0
t-shirt-bl   blue        large      2

Now; here is the tricky part - on the parent product row I need to amalgamate all of the above data into one cell like this:

"t-shirt-rs[red#small[0;t-shirt-rl[red#large[2;t-shirt-bs[blue#small[0;t-shirt-bl[blue#large[2"

The "[" separates each new child-product name (sku) from their options and the "#" separates the child-product options then there is a further "[" to separate the price adjustment and then a ";" between each child-product

Does the above even make sense??

The 1st parent product that I have to upload has 4 options (size, colour, motif and material) and each option has up to 9 choices (4 sizes, 9 colours, 6 motifs & 2 materials). I think I'm looking at (4x9x6x2=) 432 child products which will make for a very long manual concatenation job.

I could do a simple =A2 & "[" & B2 & "#" & C2..... in the cell that I need but I fear that this will take forever.

I'm hoping to be able to list the child products out as above (with copious amounts of copying and pasting :o) and then use vba to amalgamate into the single cell and add the ['s, #'s and ;'s in all of the right places.

I suppose something like:

with the first row
(add " symbol?) & 1st cell & "[" & 2nd cell & "#" & 3rd cell & "#" & 4th cell .....
move down one row
same as above
keep going until I run out of child products??
add final " symbol

I'm new to VBA so don't really know where to start I'm afraid. Can anyone please point me in the right direction?

Thanks, Alan

Était-ce utile?

La solution

Following on from the comments, I would do it as follows:

In Cell E2:

= A2 & "[" & B2 & "#" & C2 & "#" & D2

In Cell F2:

= F1 & E2

(this assumes F1 is blank)

Then drag E2 & F2 down for the length of your data. Your final value will be the value in the last row, F cell.

Hope that makes sense.

UPDATE:

Now, you know your final value is the last cell in column F, but you need the " surrounding that, so in the cell you want your final solution, put in this formula:

="""" & OFFSET(F2,COUNTA(F2:F100000)-1,0) & """"

This will find the last value in column F and surround it with the quotation marks needed.

Autres conseils

try something like. I comment what it is doing.

   'Children either refers to a range or a 2-d array
Function GetDescriptor(children) As String
    Dim descriptor As String
    Dim i As Long
    Dim arr

    'query if a range
    If TypeOf children Is Range Then
        'a) single cell range returns a scalar and b) doesn't make sense here anyway
        If children.Areas(1).Count = 1 Then Exit Function
        'load the data into an array (quicker than looping through cells)
        arr = children.Value
    End If

    'loop through the data
    For i = LBound(arr, 1) To UBound(arr, 1)
        'join the row's data together as specified
        descriptor = descriptor & _
            arr(i, LBound(arr, 2)) & "[" & _
            arr(i, LBound(arr, 2) + 1) & "#" & _
            arr(i, LBound(arr, 2) + 2) & "[" & _
            arr(i, LBound(arr, 2) + 3) & _
            IIf(i < UBound(arr, 1), ";", "")
    Next i

    'return wrapped in "
    GetDescriptor = """" & descriptor & """"

End Function

You'll need to loop through each column in each row. Each column needs to be handled differently because you require a different delimiter for each one. The code below should get you started. You'll need to research functions so that you can actually return a value as well as functions to return the last used row. This is pretty hardcoded, but it should get you started.

Sub myConcat()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' change to appropriate sheet

    Dim col As Long ' keeps track of what column we're on
    Dim row As Long ' keeps track of what row we're on

    Dim str As String 'stores string as we build it across the columns
    Dim finalstring As String ' stores string as we build it across rows; this will be our     final return value

    For row = 2 To 5 'change 5 to last row that needs processed
        For col = 1 To 4 ' number of colums with data
            Select Case col
                Case 1
                    str = Cells(row, col).Value
                Case 2
                    str = str & "[" & Cells(row, col).Value
                Case 3
                    str = str & "#" & Cells(row, col).Value
                Case 4
                    str = str & "[" & Cells(row, col).Value & ";"
            End Select
            'Debug.Print str
        Next col
        finalstring = finalstring & str
        Debug.Print finalstring
    Next row

End Sub
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top