Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

diagonals tables transposed colu

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Diagonals on tables transposed to columns

Diagonals on tables transposed to columns

ResolvedVersion 2007

Ben has attended:
Excel VBA Intro Intermediate course

Diagonals on tables transposed to columns

Hi Anthony,

Great course. Great trainer! Many thanks for helping with the VBA code to convert diagonals sourced from a pivot table to columns. Be grateful if you could post the code and I'll see if I can get it working when I'm at work next week. Cheers Ben.

RE: Diagonals on tables transposed to columns

Hi Ben, thanks for the compliment! Here's the transposition code - you'll need to adjust the worksheet names if they are not "Sheet1" and "Sheet2" for source and destination data. One other thing, if you run this directly on a Pivot table you may need to disable the GetPivotData functionality. Let me know if you need help with this.

Here's the code:

*****

Option Explicit

Sub transpose_me()

Dim intTargetRowCount As Integer
Dim inttargetcolumncount As Integer
Dim diagonal As Integer
Dim shuntdown As Integer
Dim across As Integer

'set initial coordinates for first straightened diagonal

intTargetRowCount = 1
inttargetcolumncount = 1

'determine the number of diagonals

For diagonal = 1 To Sheets("sheet1").Range("a1").CurrentRegion.Columns.Count

shuntdown = 1

For across = diagonal To Sheets("Sheet1").Range("a1").CurrentRegion.Columns.Count

Sheets("sheet2").Cells(intTargetRowCount, inttargetcolumncount) = Cells(shuntdown, across + 1)
intTargetRowCount = intTargetRowCount + 1 'move down a row on destination sheet
shuntdown = shuntdown + 1 'increment the row number on source sheet

Next across 'move to next column

'reset coordinates for new straightened diagonal

inttargetcolumncount = inttargetcolumncount + 1
intTargetRowCount = 1

'move onto the next diagonal

Next diagonal


End Sub


*****

Hope this helps,

Anthony

Sat 9 Jul 2011: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Wrapping Text in a Cell in an Excel 2010 Workbook

When you have a lot of text you want to put in a particular cell but you can't decrease the font size to fit because the text will then become ineligible, then manually wrap the text in a cell by simply pressing ALT+ENTER.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.