Excel复杂多列&;用vba实现多行拼接
我正在为一家网上商店创建一个电子表格,里面有很多产品,我真的需要一些关于其中一个领域的帮助 我有一个母产品和几个子产品(想想不同尺寸和颜色的T恤) 为了保持简单,让我们使用两种颜色(红色和蓝色)和两种尺寸(小和大)-这将提供4种不同的产品组合(即小红色、小蓝色、大红色和大蓝色) 这些数据列在我的工作表上,如下所示:Excel复杂多列&;用vba实现多行拼接,vba,excel,virtuemart,Vba,Excel,Virtuemart,我正在为一家网上商店创建一个电子表格,里面有很多产品,我真的需要一些关于其中一个领域的帮助 我有一个母产品和几个子产品(想想不同尺寸和颜色的T恤) 为了保持简单,让我们使用两种颜色(红色和蓝色)和两种尺寸(小和大)-这将提供4种不同的产品组合(即小红色、小蓝色、大红色和大蓝色) 这些数据列在我的工作表上,如下所示: sku colour size price t-shirt-rs red small 0 t-shirt-rl
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)与其选项分开,“#”将子产品选项分开,然后在每个子产品之间还有一个“[”将价格调整分开,然后是一个“;”
以上这些有意义吗
我必须上传的第一个父产品有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新手,所以我真的不知道从哪里开始。
谁能给我指一下正确的方向吗
谢谢,
艾伦试试类似的方法。我评论它在做什么
'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
”子项指的是范围或二维数组
函数GetDescriptor(子项)作为字符串
作为字符串的Dim描述符
我想我会坚持多久
暗淡的边缘
'查询是否存在一个范围
如果子项的类型是范围,则
“a)单个单元格范围返回标量,b)在这里无论如何都没有意义
如果children.area(1).Count=1,则退出函数
'将数据加载到数组中(比在单元格中循环更快)
arr=子项。值
如果结束
'循环浏览数据
对于i=LBound(arr,1)到UBound(arr,1)
'按指定将行的数据连接在一起
描述符=描述符&_
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
试试类似的方法。我会评论它在做什么
'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
”子项指的是范围或二维数组
函数GetDescriptor(子项)作为字符串
作为字符串的Dim描述符
我想我会坚持多久
暗淡的边缘
'查询是否存在一个范围
如果子项的类型是范围,则
“a)单个单元格范围返回标量,b)在这里无论如何都没有意义
如果children.area(1).Count=1,则退出函数
'将数据加载到数组中(比在单元格中循环更快)
arr=子项。值
如果结束
'循环浏览数据
对于i=LBound(arr,1)到UBound(arr,1)
'按指定将行的数据连接在一起
描述符=描述符&_
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
根据评论,我将按如下方式进行:
在单元格E2中:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
在单元格F2中:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
(假设F1
为空)
然后向下拖动E2
和F2
查看数据的长度。最终值将是最后一行F
单元格中的值
希望这是有道理的
更新:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
现在,您知道您的最终值是F列中的最后一个单元格,但您需要在其周围加上“
,因此在您想要最终解的单元格中,输入以下公式:
="""" & OFFSET(F2,COUNTA(F2:F100000)-1,0) & """"
这将在F列中找到最后一个值,并用所需的引号将其括起来。根据注释,我将按如下操作: 在单元格E2中:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
在单元格F2中:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
(假设F1
为空)
然后向下拖动E2
和F2
查看数据的长度。最终值将是最后一行F
单元格中的值
希望这是有道理的
更新:
= A2 & "[" & B2 & "#" & C2 & "#" & D2
= F1 & E2
现在,您知道您的最终值是F列中的最后一个单元格,但您需要在其周围加上“
,因此在您想要最终解的单元格中,输入以下公式:
="""" & OFFSET(F2,COUNTA(F2:F100000)-1,0) & """"
这将在F列中找到最后一个值,并用所需的引号将其括起来。您需要在每行的每一列中循环。每一列需要不同的处理方式,因为您需要为每一列使用不同的分隔符。下面的代码应该可以让您开始使用。您需要研究函数,以便能够实际使用y返回一个值以及返回上次使用的行的函数。这是相当硬的编码,但应该可以让您开始
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
您需要循环遍历每行中的每一列。每一列都需要不同的处理方式,因为您需要为每一列使用不同的分隔符。下面的代码应该可以让您开始学习。您需要研究函数,以便能够实际返回值以及返回最后使用的行的函数。这是相当硬的编码,但是这应该让你开始
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
为什么你的
=A2&“[”的最初想法&