+ Reply to Thread
Results 1 to 7 of 7

Transposing column range to row while excluding blanks

  1. #1
    Registered User
    Join Date
    12-12-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Transposing column range to row while excluding blanks

    Hi,

    This may end up being a bit of a noob question, but I'm just getting started with VBA.

    I have a worksheet full of data that has been formatted for ease of viewing, but from which I need to pull data into a more suitable format for analysis and visualization in Spotfire. To do this, I need to copy non-blank cells from a source column and transpose these cells into a contiguous row in a different worksheet. In the long run there will be a lot more logic behind this operation, but this is just a first step.

    I hacked around a bit and have come up with the following code, which successfully accomplishes my task:
    Please Login or Register  to view this content.
    My question relates to speed and elegance of the solution. Because it uses a for loop to go through all the cells the operation is fairly slow. Eventually this operation may be applied multiple times at the push of a button, leading to a loop that cycles through and copies literally thousands of cells - very time consuming.

    It seems like there should be a more elegant way to handle this. Something along the lines of copying the entire range as a single item (rather than looping through each cell) and removing the blanks along the way.

    I've attached the sheet I'm working with as an example. Any suggestions will be greatly appreciated.
    Example _ transpose without blanks.xlsm

    Thanks in advance,
    James

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Transposing column range to row while excluding blanks

    You could try directy assigning instead of copying.

    Please Login or Register  to view this content.
    Martin

  3. #3
    Registered User
    Join Date
    12-12-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Transposing column range to row while excluding blanks

    Hi Martin,

    Thank you for the feedback. That solution loses the number formatting, but I noticed I can get around that by pre-formatting the destination cells.

    The trouble is it is only marginally faster (only shaves a couple seconds off the operation).

    If you have any other suggestions I'd certainly be grateful.

    Thanks,
    James

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Transposing column range to row while excluding blanks

    Have you got any complex calculations going on in this sheet? If so, the slowness could be due to a recalculation for each entry copied across. Setting the calculation mode to manual either manually or in the code could help.

  5. #5
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Transposing column range to row while excluding blanks

    Hi -

    Please Login or Register  to view this content.
    before: 5.0313 seconds
    after: 1.4844 seconds

    Regards,
    Event

  6. #6
    Registered User
    Join Date
    12-12-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Transposing column range to row while excluding blanks

    Hey Event & mrice, thank you for the extra information.

    In terms of speed, shutting off the screenupdating and cutcopymode certainly helped. A User on another forum was able to suggest this for me however, which has really taken the cake:

    Please Login or Register  to view this content.
    Thanks for all the help!
    James

  7. #7
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    818

    Re: Transposing column range to row while excluding blanks

    Much faster.
    Please Login or Register  to view this content.
    And shorter:
    Please Login or Register  to view this content.
    Last edited by HSV; 12-15-2012 at 01:52 PM.
    Kind regards, Harry.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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