+ Reply to Thread
Results 1 to 7 of 7

Problems with tranposing an array in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2015
    Location
    Utrecht, Netherlands
    MS-Off Ver
    2010
    Posts
    69

    Problems with tranposing an array in VBA

    Hi,

    I'm building an array with info form different other arrays. Since it is not possible to write rows to an array but only columns, i'm using the following trick:

    NewColumn = NewColumn + 1
    ReDim Preserve MyZACostPriceTable(1 To 10, 1 To NewColumn)
    MyZACostPriceTable(1, NewColumn) = MyIndirectCC
    MyZACostPriceTable(2, NewColumn) = MyIndirectCC
    MyZACostPriceTable(3, NewColumn) = MyIndirectCCName
    MyZACostPriceTable(4, NewColumn) = MyIndirectCC
    MyZACostPriceTable(5, NewColumn) = MyIndirectCCName
    MyZACostPriceTable(6, NewColumn) = MyTargetZA
    MyZACostPriceTable(7, NewColumn) = MyTargetZAName
    MyZACostPriceTable(8, NewColumn) = MyTargetZAAmount
    MyZACostPriceTable(9, NewColumn) = GetTargetZaCosts
    MyZACostPriceTable(10, NewColumn) = GetTargetZaCosts / MyTargetZAAmount
    This works fine, but when i want to transpose the array at the end of the code:

    MyZACostPriceTable = Application.Transpose(MyZACostPriceTable)
    it gives me a data type mismatch error :-(. The total amount of columns after the total array has been filled is 120.645. Can this ne too much?

    Regards

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Problems with tranposing an array in VBA

    Transpose is based on the Worksheet function TRANSPOSE. Since there are fewer that 100 thousand column on a worksheet, that's why the Transpose fails in your case.

    I'm guessing that MYZACostPriceTable varies in size. But what approx. final row count, final column count is expected?
    Can you clearly say "There will be X or fewer rows and Y or fewer columns"?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    08-18-2015
    Location
    Utrecht, Netherlands
    MS-Off Ver
    2010
    Posts
    69

    Re: Problems with tranposing an array in VBA

    Hi,

    More like: it will always be 10 columns (after transpose) and > 100.000 rows..

    Regards..

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Problems with tranposing an array in VBA

    ReDim Preserve MyZACostPriceTable(1 To 10, 1 To NewColumn)
    Why not reverse the rows and columns in the array when it's declared, and eliminate the need for the transpose?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Problems with tranposing an array in VBA

    In case of Application.Transpose ..
    it fails.. when any NULL value occurs in between..
    validate those NULL values or handle accordingly..
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  6. #6
    Registered User
    Join Date
    08-18-2015
    Location
    Utrecht, Netherlands
    MS-Off Ver
    2010
    Posts
    69

    Re: Problems with tranposing an array in VBA

    Solved it, found another function which i slightly modified:

    Function TransposeDim(v As Variant) As Variant
    ' Custom Function to Transpose a 0-based array (v)
    
        Dim X As Long, Y As Long, Xupper As Long, Yupper As Long
        Dim tempArray() As Variant
    
        Xupper = UBound(v, 2)
        Xlower = LBound(v, 2)
        Yupper = UBound(v, 1)
        Ylower = LBound(v, 1)
    
        ReDim tempArray(Xlower To Xupper, Ylower To Yupper)
        For X = Xlower To Xupper
            For Y = Ylower To Yupper
                tempArray(X, Y) = v(Y, X)
            Next Y
        Next X
    
        TransposeDim = tempArray
    
    End Function
    Thanks for all the repleys!

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Problems with tranposing an array in VBA

    WorksheetFunction.Transpose is limited to 64K array elements.

+ 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. [SOLVED] Tranposing text with no spaces.. HELP
    By BenCrockett in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-11-2013, 10:32 PM
  2. Replies: 2
    Last Post: 06-12-2012, 01:26 PM
  3. [SOLVED] Tranposing dates into new lines of data
    By FSUMBA97 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-22-2011, 10:31 AM
  4. Need Help Tranposing Values
    By excelor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-26-2011, 08:46 PM
  5. Tranposing data in a multi-column table
    By meowzers in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-26-2010, 07:57 PM
  6. Tranposing an array of cells
    By pickslides in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-29-2009, 11:24 PM
  7. Tranposing data to multiple worksheets
    By Goldentrout in forum Excel General
    Replies: 1
    Last Post: 09-08-2009, 03:12 PM
  8. sorting and tranposing of data
    By bgunawan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-08-2009, 11:45 PM

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