我正在考虑使用以下结构通过Macro或VBA转换excel
1 | X | X | X | X | X
2 | Y | Y |
3 | Y | Y |
4 | X | X | X | X | X
5 | Y | Y |
6 | Y | Y |
我需要将其更改为以下格式
1 | X | X | X | X | Y | Y | Y | Y |
2 | X | X | X | X | Y | Y | Y | Y |
需要循环使用excel文件并为多个列/行执行此操作。
我有什么建议吗?
由于
答案 0 :(得分:0)
如果格式完全如您的示例所示,并且永远不会更改,并且位于从单元格A1开始的工作表1中:
Sub doFormatData()
Dim shtIn As Worksheet
Dim shtOut As Worksheet
Dim rIn As Long
Dim rOut As Long
Dim c As Long
Set shtIn = Sheets("Sheet1")
Set shtOut = ActiveSheet
rIn = 1
rOut = 1
Do
For c = 1 To 4
shtOut.Cells(rOut, c).Value = shtIn.Cells(rIn, c).Value
Next c
For c = 5 To 6
shtOut.Cells(rOut, c).Value = shtIn.Cells(rIn + 1, c - 4).Value
Next c
For c = 7 To 8
shtOut.Cells(rOut, c).Value = shtIn.Cells(rIn + 2, c - 6).Value
Next c
rIn = rIn + 3
rOut = rOut + 1
Loop Until shtIn.Cells(rIn, 1).Value = ""
End Sub
从Sheet1以外的任何空工作表运行它。带数据的第一个单元格为A1。