Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

excel - Is it possible to restructure multi header one drive table using automate in one drive?

I am trying to restructure inside one drive using automate a function, but not sure if multi header table can be restructured. Thank you in advance for your help. enter image description here

I want to restructure it in this format -

Blockquote

Customer

question from:https://stackoverflow.com/questions/65895842/is-it-possible-to-restructure-multi-header-one-drive-table-using-automate-in-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can loop through the dates in 1st row and create a nested loop which goes through the customers one by one and get the values according to the actual column and row. You should save the result on a new sheet.

Sub Format_Table()
    lastRowOnNewFormatSheet = 2 'last empty row on newSheet
    'Loop through the columns with step 3
    For i = 2 To Cells(1, Columns.Count).End(xlToLeft).Column Step 3
        'Loop through the rows at every column
        For j = 3 To Cells(Rows.Count, 1).End(xlUp).Row
            Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 1).Value = Sheets("oldFormat").Cells(j, 1) 'customer name
            Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 2).Value = Sheets("oldFormat").Cells(1, i) 'date
            Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 3).Value = Sheets("oldFormat").Cells(j, i) 'budget
            Sheets("newFormat").Cells(lastRowOnNewFormatSheet, 4).Value = Sheets("oldFormat").Cells(j, i + 1) 'actual
            lastRowOnNewFormatSheet = lastRowOnNewFormatSheet + 1 'update last empty row on newSheet
        Next j
    Next i
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...