Excel复杂的多列&使用vba进行多行连接

时间:2014-04-24 20:13:38

标签: excel vba excel-vba virtuemart

我正在创建一个包含在线商店产品的电子表格,我真的需要其中一个领域的帮助。

我有一些含有多种儿童产品的母产品(想想不同尺寸和颜色的T恤)。

为了简单起见,我们可以使用两种颜色(红色和蓝色)和两种尺寸(小型和大型) - 这可以提供4种不同的产品组合(即小红色,小蓝色,大红色和大蓝色) )

此数据列在我的表格中,如下所示:

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

现在;这是一个棘手的部分 - 在父产品行上我需要将所有上述数据合并到一个单元格中,如下所示:

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

" ["将每个新的子产品名称(sku)与其选项和"#"分开。将儿童产品选项分开,然后还有一个" ["分开价格调整,然后是&#34 ;;"在每个儿童产品之间

上述甚至是否有意义?

我必须上传的第一个母产品有4个选项(尺寸,颜色,图案和材质),每个选项最多有9种选择(4种尺寸,9种颜色,6种图案和2种材料)。我想我正在查看(4x9x6x2 =)432个子产品,这些产品将用于非常长的手动连接工作。

我可以做一个简单的= A2& " [" &安培; B2& "#" &安培; C2 .....在我需要的单元格中,但我担心这将需要永远。

我希望能够如上所述列出子产品(大量复制和粘贴:o)然后使用vba合并到单个单元格中并添加[' s ,#,和;' s在所有正确的地方。

我想这样的事情:

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

我是VBA的新手,所以我真的不知道从哪里开始我害怕。 有谁能指出我正确的方向?

谢谢, 艾伦

3 个答案:

答案 0 :(得分:2)

根据评论,我会按如下方式进行:

在细胞E2中:

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

在单元格F2中

= F1 & E2

(这假定F1为空白)

然后拖动E2& F2缩短了数据的长度。您的最终值将是最后一行F单元格中的值。

希望这是有道理的。

<强>更新

现在,您知道您的最终值是F列中的最后一个单元格,但是您需要"周围的单元格,因此在您想要最终解决方案的单元格中,请输入以下公式:

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

这将找到F列中的最后一个值,并用所需的引号括起来。

答案 1 :(得分:1)

尝试类似的东西。我评论它在做什么。

   '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

答案 2 :(得分:1)

您需要遍历每一行中的每一列。每个列需要以不同的方式处理,因为每个列都需要不同的分隔符。下面的代码可以帮助您入门。您需要研究函数,以便您可以实际返回值以及函数以返回上次使用的行。这是非常硬编码的,但它应该让你开始。

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