+ Reply to Thread
Results 1 to 13 of 13

How to transpose a recordset from a MySQL query

  1. #1
    Registered User
    Join Date
    11-26-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    11

    How to transpose a recordset from a MySQL query

    I have a script (below) which selects an array from a database and copies the values into excel okay. However, I want to transpose the table from this:

    FY year Revenues
    FY0 2013 11,019
    FY1 2014 11,329
    FY2 2015 11,787
    FY3 2016 12,359

    to This:

    FY FY0 FY1 FY2 FY3
    year 2013 2014 2015 2016
    Revenues 11,019 11,329 11,787 12,359

    I have tried playing around with the "Application.Transpose" function and also by calling a transpose function (shown), but with no success. Would someone be able to advise me of the correct syntax to use in the below example to transpose the recordset?

    thanks


    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to transpose a recordset from a MySQL query

    Have you tried using the query as an external source for a pivot table?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-26-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to transpose a recordset from a MySQL query

    Thanks, but a pivot table is not suitable for my application and I am also aware of the transpose function in excel which is also not suitable. Hence my question was specific in asking for help on the VBA syntax for transposing a recordset and why I provided the context of the script that I am trying to incorporate it into. If anyone knows the answer for this I would be most grateful for their input.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to transpose a recordset from a MySQL query

    Have you tried using Copy/Paste Special/Transpose on each column instead of the whole range/array?

    Something like this where the recordset resides in A1:C5 and the result appears in F1:J3.
    Please Login or Register  to view this content.

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to transpose a recordset from a MySQL query

    You may try:
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  6. #6
    Registered User
    Join Date
    11-26-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to transpose a recordset from a MySQL query

    Hi Izandol, you're clearly on the right track, although your code amendments just get me the original table rather than transposing it. I think it may be because 'GetRows' already transposes the data. I therefore changed the code as the function 'TransposeDim' is probably now redundant - from:

    Please Login or Register  to view this content.
    This seems to transpose the bulk of the table, but field names are still showing up at the top of the columns rather than down the left margin of the rows

    Table is now coming thru as:-
    FY year Revenues
    FY0 FY1 FY2
    2013 2014 2015
    11,019m 11,329m 11,787m


    Whereas is should be:-

    FY FY0 FY1 FY2
    Year 2013 2014 2015
    Revenues 11,019m 11,329m 11,787m

    I think you are just an inch away from cracking it

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to transpose a recordset from a MySQL query

    Of course!

    You must change this:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-26-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to transpose a recordset from a MySQL query

    Hi Izandol, you're clearly on the right track, although your code amendments just get me the original table rather than transposing it. I think it may be because 'GetRows' already transposes the data. I therefore changed the code as the function 'TransposeDim' is probably now redundant - from:

    Please Login or Register  to view this content.
    This seems to transpose the bulk of the table, but field names are still showing up at the top of the columns rather than down the left margin of the rows

    Table is now coming thru as:-
    FY year Revenues
    FY0 FY1 FY2
    2013 2014 2015
    11,019m 11,329m 11,787m


    Whereas is should be:-

    FY FY0 FY1 FY2
    Year 2013 2014 2015
    Revenues 11,019m 11,329m 11,787m

    I think you are just an inch away from cracking it

  9. #9
    Registered User
    Join Date
    11-26-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to transpose a recordset from a MySQL query

    Izandol, you're a genius. Very many thanks indeed. This is so much easier than trying to transpose directly from MySQL. If you ever have a simple way of doing that I would love to know.

  10. #10
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to transpose a recordset from a MySQL query

    You are welcome.

    Please remember to select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to transpose a recordset from a MySQL query

    Izandol

    Why not transpose the data and fields at the same time?

    To do that you could pass the recordset to the function rather than the result of GetRows.

  12. #12
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How to transpose a recordset from a MySQL query

    @Norie,

    We are not in fact using the function because GetRows will transpose data already (which I forgot in original reply).

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Oops, I forgot that GetRows transposed - too busy looking for an SQL solution.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. excel vba populate listbox from mysql recordset
    By rodriguez76 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2013, 02:24 PM
  2. Dynamic Query Tables for mySQL
    By chdmwu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2010, 12:57 PM
  3. Refresh a mysql query
    By mintmin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-11-2009, 09:55 PM
  4. mySQL Microsoft Query Criteria Failure
    By hitea in forum Excel General
    Replies: 0
    Last Post: 11-06-2008, 01:22 PM
  5. how to query mysql and get result into excel?
    By xianwinwin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-04-2007, 10:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1