从Excel工作表生成字符串

时间:2015-04-20 14:37:47

标签: excel vba excel-vba

我有一个excel有三列和三行,如下所示:

Field1,Field2,Field3

Row1Value1,Row1Value2,Row1Value3

Row2Value1,Row2Value2,Row2Value3

Row3Value1,Row3Value2,Row3Value3

我想写一个VBA宏来生成如下输出:

Field1=Row1Value1,Field2=Row1Value2,Field3=Row1Value3
Field1=Row2Value1,Field2=Row2Value2,Field3=Row2Value3
Field1=Row3Value1,Field2=Row3Value2,Field3=Row3Value3

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:1)

是的,你可以在VBA中非常简单地做到这一点(我们可以在你分享尝试后帮助你)。如果您只想要最终结果,那么在表格中使用基本(非VBA)公式会更快。

= A$1 & "=" & B1 & "," & A$2 & "=" & B2 & "," & A$3 & "=" & B3

(假设表中左上角的单元格为A1)

答案 1 :(得分:1)

想想我的工作:

Sub Transform()
Dim i As Integer, j As Integer
For i = 2 To Worksheets("Input").UsedRange.Rows.Count
Dim row As String
    row = ""
    For j = 1 To Worksheets("Input").UsedRange.Columns.Count
        row = row & Worksheets("Input").Cells(1, j) & "=" & Worksheets("Input").Cells(i, j) & ","

    Next j
    Worksheets("Result").Cells(i, 1) = Left(row, Len(row) - 1)
Next i
End Sub