考虑一个表:
a b c d
key1 value1 value2 value3
key2 value1a value3a
我需要把它转换成
Key1 Value1
Key1 Value2
Key1 Value3
Key2 Value1a
Key2
key2 Value3a
此代码用于将所有数据放入单个列中,包括所需的空格,但我需要将第一列保留为键,而且我是 Excel 中的 VBA 新手。
Sub MultiColsToA()
Dim rCell As Range
Dim lRows As Long
Dim lCols As Long
Dim lCol As Long
Dim ws As Worksheet
Dim wsNew As Worksheet
lCols = Columns.Count
lRows = Rows.Count
Set wsNew = Sheets.Add()
For Each ws In Worksheets
With ws
For Each rCell In .Range("B1", .Cells(1, lCols).End(xlToLeft))
.Range(rCell, .Cells(lRows, rCell.Column).End(xlUp)).Copy _
wsNew.Cells(lRows, 1).End(xlUp)(2, 1)
Next rCell
End With
Next ws
End Sub
这些表大约有 55 行,12 到 30 列。理想情况下,我还需要以相同的方式转换 20 张左右的表格,因此以编程方式执行此操作将是理想的,可以帮忙吗?