Function create_matrix_format(myRange As Range) As String
Dim firstCell As Boolean
Dim nextLineStr As String
Dim resultStr As String
firstCell = True
For Each mycell In myRange
If resultStr <> "" And firstCell Then resultStr = resultStr & ", "
If firstCell Then
nextLineStr = "[" & mycell.Value & ", "
firstCell = False
Else
nextLineStr = nextLineStr & mycell.Value & "]"
firstCell = True
resultStr = resultStr & nextLineStr
End If
Next mycell
create_matrix_format = resultStr
End Function
Function create_matrix(rng As Range) As String
Dim rw As Range, element As Range
output = "["
For Each rw In rng.Rows
For Each element In rw.Cells
output = output & element.Value & ", "
Next
output = Left(output, Len(output) - 2) & "], ["
Next
create_matrix = Left(output, Len(output) - 3)
End Function
4条答案
按热度按时间egmofgnx1#
(1)您可以使用Excel公式来解决此任务:
D2:=“[”& A2 &“,“& B2 &“]”
复制到D列的最后一个单元格
E2:= D2
E3:= E2 &“,“& D3
复制到E列的最后一个单元格
(2)使用vba
并在单元格中调用此函数:
例如D5:=create_matrix_format(A2:B4)
9lowa7mx2#
基于公式的替代方案:
roqulrg33#
在Excel vba中,您可以使用UDF:
下面是它的实际运行情况:
0s0u357o4#
你可以把它们读到一个dataframe中:
然后可以使用
zip()
或者