+ Reply to Thread
Results 1 to 23 of 23

Input/output values into/from an array

  1. #1
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Input/output values into/from an array

    I have a long list of values, a few thousand lines, that I need to input into an array so I can run a formula on the values in the array and then take those results and put them into another array which I can then call to output down a different column. Is there a faster way to accomplish this than the typical For Loop with the activecell.value and activecell.offset commands, that just takes forever.

    This is currently my output code loop:
    Please Login or Register  to view this content.
    Last edited by Brane Ded; 11-20-2009 at 02:00 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Hello Brane Ded,

    Welcome to the Forum!

    To make your posts easier to read, copy, and edit please wrap your code. I did it for you this time. Here is how you can do it next time.

    How to wrap your Code
    1. Select all your code using the mouse.
    2. Click on the # icon on the toolbar in the Message window. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post.
    3. To do it manually...
    Use the Bulletin Board Code Tags
    [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] at the end of the last line.


    To learn more about BB codes used in this forum, just click on the link below...

    Bulletin Board Codes Tags
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Sorry about that, I should've remembered about the code tags like any other phpbb forum. I just copied it in quickly before leaving the house and didn't review my post before taking off. Thanks for the fix.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Not very clear but you can use a Range to fill an Array if that's your question ?

    Please Login or Register  to view this content.
    The above will by default be a 2-dimensional array ... if the source is say a vertical vector and you prefer a single dimension then Transpose the range

    Please Login or Register  to view this content.
    If the vector is horizontal transpose twice for single dimension array.
    Last edited by DonkeyOte; 11-07-2009 at 04:22 AM. Reason: added note re: transposition

  5. #5
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    It is the 1 dimensional array I'm using but that code did not add the ranged values to an array that I can call each value one by one. Let me try to explain it further;

    Dims:
    Please Login or Register  to view this content.
    In column D I have a vertical series of numbers (over 10,000) which are the output values of my function. I used
    Please Login or Register  to view this content.
    to assign them to the variable "D_Range".

    In column O I have another vertical series of input numbers and I used
    Please Login or Register  to view this content.
    to assign them to the variable "O_Range".

    I used
    Please Login or Register  to view this content.
    to find out how many inputs my function will have and assign them to the value n for my For Loop's counting purposes.

    I then use a For Loop to input all the numbers into the array "O_Array" to be called one by one.
    Please Login or Register  to view this content.
    I take those "O_Array" values and use them in this loop
    Please Login or Register  to view this content.
    where it takes the first "O_Array" value, enters it into my function and repopulates the D column values. U_Max finds the highest of the D column values and assigns it to the array "U_Max_Array."

    I then call the "U_Max_Array" values to be output into another column with another loop.
    Please Login or Register  to view this content.
    There's nothing wrong per say with this code other than it's really slow as it goes through over 30,000 numbers and calculations (It runs 3 times through functions). the parts slowing it down are primarily the array loops and if I can get rid of them by mass input/output of arrays I can significantly speed it up.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    It is the 1 dimensional array I'm using but that code did not add the ranged values to an array that I can call each value one by one
    I'm afraid the above makes little sense to me. You can pass the entire range to the Array and iterate the Array as you would any other. However that said I confess there's quite a bit I don't follow in your code so it's hard for me to pass much comment.

    Dim i, n As Integer, D_Range, O_Range As Range, U_max As Single
    Dim O_Array(10000), U_Max_Array(10000) As Single
    In the above: n is declared as an Integer, O_Range as a Range, U_max As Single, U_Max_Array as a single Array ... all other variables are declared as Variants. In VBA you must explicitly declare each variable... eg

    Please Login or Register  to view this content.
    VBA is not option explicit by default (XL is not designed for programmers per se) and as such any variable not explicitly declared will default to a Variant and Variants can hold any value so your code will work without issue. Use Option Explicit at the head of your module if you prefer to check declarations etc...

    My next point would be you Dim your Arrays as having 10001 entries, then establish a count against a (smaller) range iterate & populate the Array.
    The nature of the iteration would imply the numbers are in sequential order in the O_range.
    Based on the above you would be far better served (IMO) leaving the size of the dimension unspecified initially (as Variant) and populate based on the count

    Please Login or Register  to view this content.
    In all honesty I can't really follow the code after that but it's late and I'm tired so perhaps a few further reads and the penny will drop at which point if I can I will pass further comment (if desired).
    Last edited by DonkeyOte; 11-07-2009 at 04:47 PM. Reason: typos... no ReDim required

  7. #7
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    It work with "Option Explicit" on by default. Setting O_Array as a variant and using the application.transpose section worked to inout the values into an array. I cannot seem to output them properly in a similar manner though.
    Last edited by Brane Ded; 11-07-2009 at 06:25 PM. Reason: Typo

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Sorry, yes, I should have been clearer... the variables are declared (so will not debug) but they're not being declared quite how you think... if you bring up the Locals window when you iterate your code you will see how i for ex. is being initialised as a Variant rather than an Integer (as I suspect you suspect it is).

    I will look into this thread again tomorrow in regard to your output question - if not resolved in the meantime by others.

    If you're able to post a watered down version of your file (in terms of data quantity) but which outlines what it is you're working with and the desired output this will invariably help - as is we're trying to visualise which given our poor imagination can lead to difficulties! A file generally speaks a dozen posts.
    Last edited by DonkeyOte; 11-07-2009 at 06:32 PM. Reason: typo... a recurring theme!

  9. #9
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    I'll cut the file down and upload a copy.

  10. #10
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Sure runs really fast when there's almost no data
    Lab_10.xlsm

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

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Try this:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    This works well (8 seconds faster than mine) but I do not understand most of the content after the variable declaration. Could you explain it? Particularly:
    Please Login or Register  to view this content.
    On this line
    Please Login or Register  to view this content.
    is it possible to replace "Rectangular" with something to default the process to the currently open sheet? This would save me a lot of copy and paste time.

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

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Please Login or Register  to view this content.
    The dot means that the reference is qualified by the reference in the With statement, equivalent to
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    refers to the last cell in Col D; End(xlup) is the equivalent of doing Ctrl+Up from that cell in the user interface.
    is it possible to replace "Rectangular" with something to default the process to the currently open sheet?
    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Odd problem just occured, if I run the macro by holding down the F8 key it works perfectly but if I run the program either by hitting the green triangle play button or the excel run button all my output values end up wrong. They all end up as the same value when they are all supposed to be different. I tried starting over and it happened the same way, tried restarting the computer and still messes up. What just happened?

    *Edit* The error only appears to happen if I click in the vba editor after hitting the green play button. The entire process speeds up significantly as well.
    Last edited by Brane Ded; 11-08-2009 at 02:46 AM.

  15. #15
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    I just realized that the max of my output values can also be negative and I need to be looking for the absolute values of my outputs. I tried to combine the worksheetfunction.max with some sort of abs variant but nothing I tried worked, what is the correct method to accomplishing this?

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Quote Originally Posted by Brane Ded
    I just realized that the max of my output values can also be negative and I need to be looking for the absolute values of my outputs. I tried to combine the worksheetfunction.max with some sort of abs variant but nothing I tried worked, what is the correct method to accomplishing this?
    there are a few routes you could go down... the simplest perhaps (given size of real ranges) would be along lines of:

    Please Login or Register  to view this content.
    (else you're looking at running an Evaluate call with SUMPRODUCT / Array which could be quite slow)

    In terms of improving overall performance I would strongly advise adding the below prior to the With statement

    Please Login or Register  to view this content.
    and subsequently (though not technically required) the below prior to End Sub

    Please Login or Register  to view this content.
    Deactivating the repaint should lead to a significant improvement in performance (not saying it's resolving underlying performance issues but it will lessen the pain to some extent).

    On an aside you mention oddities... if you're running in ManualCalc mode you will want to enforce a calculation on the requisite ranges (ie all those impacted by alteration to B12)
    Without knowing as to whether or not you have other Volatile functions in your file (and/or other formulae linked to B12 not incl. in sample) it's hard to know whether reverting to a Manual method with Range calculate will make a significant impact on performance or not.
    Last edited by DonkeyOte; 11-08-2009 at 07:33 AM. Reason: added note re: calculation

  17. #17
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    The Abs function above worked great. I ahd to run out to the 7th decimal to find out that my low and high end spectrums wern't the same and therefore needed to find its absolute value.

    I tried running in both manual calc and auto and neither one did anything to affect the time so I left it in auto.
    Though the screen updating worked well for pausing the graphs until the end, nice plus there.

    I still cannot figure out why clicking in the vba window after hitting the green play arrow cause it to speed up 10x and then throw out the same number in each output cell.

  18. #18
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    hi all,

    and subsequently (though not technically required) the below prior to End Sub
    Please Login or Register  to view this content.
    (post # 16)
    DonkeyOte, I'm curious, for which version of Excel is the screenupdate reset "not technically required"?

    Brane,
    If you are using "application.transpose" your error of the same value being put into all cells could possibly (?) relate to Transpose's size/item limitation when transferring from VBA/memory to a spreadsheet range as discussed in KB177991 & in: http://www.excelforum.com/excel-prog...transpose.html
    http://www.dailydoseofexcel.com/arch...nge-using-vba/

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  19. #19
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Rob, I think you've shown me up [again]

    I think now based on this: http://www.mrexcel.com/td0133.html that the default reset ceased post XL2000 ... luckily I always code a reset out of habit after all the same logic applied to other App settings like Events would of couse be horrendous!

    Thanks for tripping me up though - I only like to give out so much false information on any one given day

    (That said I'm off to do some testing... )

  20. #20
    Valued Forum Contributor
    Join Date
    05-14-2009
    Location
    gold coast
    MS-Off Ver
    Excel 2007
    Posts
    843

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    off topic a little, though with the screen updating. Ive found that xl 2007 will reset this back to true at the end of the procudure.
    I ran this and didnot see any changes untill the last sub had finished running.
    Please Login or Register  to view this content.
    So from what i read in the link it seam microsoft may have gone back to how things wherer 2000 & prior.
    I have only used 2007 so i couldnot compare to anythink.


  21. #21
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    At the risk of thread hijacking...

    Thanks for the clarification DonkeyOte (& Dave for the example)
    I've learnt something new for the day & I may have to get myself a copy of Excel 2000!
    - over different projects, I've literally spent hours wondering why Excel won't respond to me and when debugging code I now often have a module of my personal.xls open in the VBE so I can easily run a wee RefreshAppSettings macro (I haven't gotten around to adding it to a button).

    Yes, I agree the impact could be horrendous. I think that requiring it to be explicitly reset is good because it is then consistent with other app' level settings.



    Rob

  22. #22
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    As I recall I believe DisplayAlerts is another App level that did/does reset by default.... I must try and find some definitive examples (or get someone who actually knows definitively to chip in!)

  23. #23
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Is there a way to mass input/output values into/from an array in VBA for Excel?

    Thank you for the help, shg's info and the screen updating were the biggest help to getting this finished.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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