エクセルマクロ「縦型を横型に」

未分類

シート1枚目の縦に項目とデータを列挙してある表を2枚目のシートに最初の行にタイトルと横書きにデータを列挙しなおすマクロ

Sub main()

Dim tempMasterData As Worksheet
Dim tempSlaveData As Worksheet

Set tempMasterData = ActiveWorkbook.Worksheets(1)
Set tempSlaveData = ActiveWorkbook.Worksheets(2)

Dim i As Integer
Dim j As Integer
Dim k As Integer

j = 1
k = 1

For i = 1 To 1591
    If tempMasterData.Cells(i, 1).Value = "" Then Exit For
    
    If tempMasterData.Cells(i, 1).Value = "BEGIN" Then
        k = k + 1
    ElseIf tempMasterData.Cells(i, 1).Value = "END" Then
        j = 1
    Else
        If tempSlaveData.Cells(1, j).Value = "" Then tempSlaveData.Cells(1, j).Value = tempMasterData.Cells(i, 1).Value
        tempSlaveData.Cells(k, j).Value = tempMasterData.Cells(i, 2).Value
        j = j + 1
    End If
    
    
Next i


End Sub