|
楼主 |
发表于 2023-12-15 22:22
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 转换数据()
Dim wsSource As Worksheet, wsTarget As Worksheet
Set wsSource = ThisWorkbook.Sheets("Sheet1") ' 源数据的工作表
Set wsTarget = ThisWorkbook.Sheets("Sheet2") ' 目标数据的工作表
Dim lastRow As Long, targetRow As Long
lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
targetRow = wsTarget.Cells(wsTarget.Rows.Count, "A").End(xlUp).Row + 1
Dim i As Long, j As Long
Dim projectName As String, name As String
Dim monthYear As String
Dim monthValue As Double
' 遍历数据
For i = 2 To lastRow ' 假设数据从第二行开始
projectName = wsSource.Cells(i, 1).Value
name = wsSource.Cells(i, 2).Value
' 遍历月份列
For j = 3 To 14 ' 假设月份数据从第3列到第14列
monthYear = wsSource.Cells(1, j).Value
monthValue = wsSource.Cells(i, j).Value
' 将数据插入新的行
wsTarget.Cells(targetRow, "A").Value = projectName
wsTarget.Cells(targetRow, "B").Value = name
wsTarget.Cells(targetRow, "C").Value = monthYear
wsTarget.Cells(targetRow, "D").Value = monthValue
targetRow = targetRow + 1
Next j
Next i
End Sub |
|