+ Reply to Thread
Results 1 to 36 of 36

return lbound and ubound of 2d array

  1. #1
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    return lbound and ubound of 2d array

    so here is my code, it works in that it creates the 2d array. now i need to return the lbound and ubound of the array but having problems:

    Please Login or Register  to view this content.
    i tried this:

    Please Login or Register  to view this content.
    but it didn't print to the immediate window. what am i doing wrong?

    also tried this:

    Please Login or Register  to view this content.
    but again, nothing prints to the window
    Last edited by dmcgov; 10-15-2019 at 10:09 AM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: return lbound and ubound of 2d array

    In next statement
    Please Login or Register  to view this content.
    is missing an argument
    see
    https://docs.microsoft.com/fr-fr/off...bound-function
    - Battle without fear gives no glory - Just try

  3. #3
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    thanks pci, i read through that text a couple of times, don't see where my error is. can you help with that?

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: return lbound and ubound of 2d array

    You're trying to access the boundaries of an element of the array, but you need the array itself.

    Lbound(arr, 1) and UBound(arr, 1)

    for the first dimension and replace the 1 by 2 for the second dimension.

    However, your code won't work at all since you can only resize the upper boundary of the last dimension if you use Preserve with ReDim.
    Rory

  5. #5
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    OK, i see that the redim is not going to function the way that i want. so how can i get my code to work?

  6. #6
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    basically i have text on sheet (range I8) and i want the lowerbounds of the second argument to the array to be a specific row number (8 in this case) and the upperbounds of second array is (10)

    my array looks like this: arr(1,0)="1005" and arr(1,1)=8
    as well as: arr(3,0)="1015" and arr(3,1)=10

    so the second array number is the row number of the selected text and that is what i want returned (so i can sum up the rows based on arr(x,0) and put that in a cell). does any of this help?
    Last edited by dmcgov; 10-15-2019 at 10:40 AM.

  7. #7
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: return lbound and ubound of 2d array

    That has nothing to do with the title of your question. That's just returning a value from an array. To be honest, I can't really see why you need an array at all here since it seems you just want a start and stop row number.

  8. #8
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so i am using a second array for my row numbers, seems like a kludge though. is there a better way to do it?

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: return lbound and ubound of 2d array

    Probably. Why don't you describe what you're actually trying to achieve, in plain English?

  10. #10
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so i have this trial balance macro that i am writing. it looks in column I for text that is surround by parenthesis and grabs the associated row numbers to sum them up.

    so in column A i have the following:

    A8 is "1005 Operating"
    A9 is "1010 Payroll"
    A10 is "1015 Petty Cash"

    the text is I8 is "Cash (1000-1099)"
    so i need to capture the row number for all accounts that are in the range of (1000-1099)

    so my macro reads the text in I8 and parses it so include all accounts in the range. note that A8 does not start with 1000 and A10 does not end in 1099 but need to sum them up, like so: Range("I8").formula = "=sum(A8:A10)"

    note that this is just one of the many accounts so i include rows 8 to 22 in row I and match all rows and put them in a sum. does that help?
    Last edited by dmcgov; 10-15-2019 at 11:19 AM.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: return lbound and ubound of 2d array

    A couple of questions then:
    -Are the accounts always in order, or could you end up with discontiguous ranges that you need to sum?
    -Do you actually need a formula, or just the result?

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: return lbound and ubound of 2d array

    In the spirit of not reinventing the wheel, what you are describing sounds to me like a task that a pivot table or a SUMIFS() function would do. I would be curious why you are not using these existing tools to do this task.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  13. #13
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    rorya,

    yes the accounts are always sorted from least to most
    and yes, the requirements from the accountant is that it must be a formula

    as for myshorty, that is a great suggesstion. let me see if i can make sumifs work, that might be the ticket.

  14. #14
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,901

    Re: return lbound and ubound of 2d array

    In that case all you need is startRow and endRow variables that you can assign inside your loop. If the criteria are met and startRow is 0 (its initial value) assign the current row to it. If startRow is not 0, then assign the row to endRow and continue until the criteria aren't met anymore.

  15. #15
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so i figured out how to get my startrow and endrow but am having a problem setting the cell1 and cell2 address. if not found then it errors out. i will paste an image of what i need programmed.

    Please Login or Register  to view this content.
    and here is the image:

    Attachment 645544

    so what i need is to capture the numbers in parenthesis and find the lower and upper bounds of the account numbers. so for the formula for J8 is "=sum(B8:B10)" and so on down the line

  16. #16
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    any help? would be very appreciative.

    here is the sample workbook that you can review, the procedure is named "test_acct2"
    Attached Files Attached Files
    Last edited by dmcgov; 10-17-2019 at 07:44 AM.

  17. #17
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: return lbound and ubound of 2d array

    Not sure if you decided that this must be VBA. Here's how I did it with a SUMIFS() function:

    1) I need the account numbers out of the text string. I'm not very good with text manipulation functions, but it was pretty easy to do manually. I enter 1000 in P8, 1099 in Q8, P9 is 1100, Q9 is 1199, and so on. For rows where there is only one account number like row 13, enter that account number into both P and Q. I'm sure if you must have a formula for this step, the guys here can help with a text manipulation formula that will extract the lower and upper account numbers from the text in I.
    2) From there it's a simple SUMIFS() function. In J8 =SUMIFS(B$8:B$144,$A$8:$A$144,">="&$P8,$A$8:$A$144,"<="&$Q8). Note the mix of relative and absolute references for easy copying, then copy/paste/fill into J8:L22.

    Unless there is some external requirement to use VBA, that should work.

  18. #18
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so i have gotten pretty far, just have a few output lines that need fixing. here is my latest code, you can use the workbook in the previous post to test:

    Please Login or Register  to view this content.
    so my output in on the left, and the corrected version is on the right. how can i get this fixed?

    =sum(B8:B10) - =SUM(B8:B10)
    =sum(B11:B12) - =SUM(B11:B12)
    =sum(B13:B17) - =SUM(B13:B17)
    =sum(B18:B18) - this should not be a formula as it is not found should be just ""
    =sum(B19:B25) - =SUM(B18:B26)
    =sum(B28:B28) - =SUM(B27:B27)
    =sum(B28:B38) - =SUM(B28:B38)
    =sum(B41:B46) - =SUM(B39:B46)
    =sum(B47:B50) - =SUM(B47:B50)
    =sum(B51:B73) - =SUM(B51:B73)
    =sum(B74:B78) - =SUM(B74:B78)
    =sum(B79:B80) - =SUM(B79:B80)
    =sum(B81:B92) - =SUM(B81:B92)
    =sum(B93:B100) - =SUM(B93:B101)
    =sum(B102:B143) - =SUM(B102:B143)


    can anyone help me on this?

  19. #19
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    @myShorty

    thanks, that worked on test workbook but not on the real one. the reason is the the value in column A is a 4 digit number plus text. so A8 = "1005. PNC - Operating"

    how can i get this to work with your formula?

  20. #20
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: return lbound and ubound of 2d array

    I find that spreadsheets work best when each cell contains only one piece of information -- in this case, either the account number or the account description. My solution would be to add a helper column to the main database that extracts the account number from the account number & description field. If the account number is always the first 4 characters, then this might be something like =VALUE(LEFT(A8,4)). The LEFT() function extracts the first 4 characters from the text string, and the VALUE() function converts the number as text string to a real number. The criteria ranges in the SUMIFS() function would then point to this helper column.

  21. #21
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    ok, so i mocked up the worksheet and got the formulas working. issue is in trying to set column O to be the helper column that is the value of the left of A8. i think im missing something easy, what am i doing wrong?

    Please Login or Register  to view this content.
    so the issue is that there is nothing in column O when the routine finishes.

  22. #22
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    got that working by doing this:

    Please Login or Register  to view this content.
    now i am trying to get the code in parenthesis and having an issue with this one line, what am i doing wrong?

    Please Login or Register  to view this content.
    so got that going by doing this:

    Please Login or Register  to view this content.
    On to the next journey
    Last edited by dmcgov; 10-17-2019 at 11:31 AM.

  23. #23
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: return lbound and ubound of 2d array

    Glad you are making progress.

    One thing I would note -- because it often makes this kind of programming (writing excel formulas into a range of cells) simpler to program and faster to execute -- many learn to write formulas to cells from VBA using R1C1 notation. This often simplifies the programming because the R1C1 formula text string does not change from cell to cell. For example, your loop in post 21 can be replaced by the single statement
    Please Login or Register  to view this content.
    If you are in the mood to learn a little R1C1 notation, you may find that using R1C1 notation simplifies many of these VBA steps.

  24. #24
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so now im trying to write the sumifs on the consolidation table and getting an application error when i try this one line, what am i doing wrong?

    Please Login or Register  to view this content.
    MrShorty, can you help me with this. i think once this is done, i can finish this project.

    fyi, here is the code without being in a formula format
    Please Login or Register  to view this content.
    what am i doing wrong. can this be converted to R1C1 format?
    Last edited by dmcgov; 10-17-2019 at 01:37 PM.

  25. #25
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,809

    Re: return lbound and ubound of 2d array

    I expect that it is not an A1/R1C1 kind of thing. I expect that it has to do with the quotation marks that are in the criteria arguments. And, since I never really use VBA to add formulas to cells, I am not very good at getting the right number and placement of quotation marks in these kinds of things. I know we have others on here who are good at this sort of thing.

  26. #26
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    thanks mrshorty for bringing in the Calvary.

    here is what i have that is not working. note that if i put the formula straight in, then it copies down and to the right correctly.

    Please Login or Register  to view this content.

    and here is the formula in J8 that works
    Please Login or Register  to view this content.

  27. #27
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: return lbound and ubound of 2d array

    Please Login or Register  to view this content.
    note: I've not reviewed the code

  28. #28
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,147

    Re: return lbound and ubound of 2d array

    try:
    Please Login or Register  to view this content.

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  29. #29
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    thanks Xlent and benishiryo, that did the trick. now on to the next routine.

  30. #30
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so now i need two more helper rows, one for the firstrow and one for the lastrow in each account category. can't use sumifs, not the right command. what should i use to get the row numbers?

    cell r8 should look at the 4 digits in column a and see if it's in the range of p8 and get the first row number and should cycle through column a and search for the lastrow in column q to get the last row number.
    then cycle through r9 through r22. how can that be done?

    let me know if you want me to post an upload of the workbook.
    Last edited by dmcgov; 10-18-2019 at 07:48 AM.

  31. #31
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    so i researched the IFS command and that seems to be what i need for this portion of the code.

    need something like this:

    Please Login or Register  to view this content.
    how can that be done (the above doesn't work, i tried)

    the portion of the activecell really needs to be row number that is returned.
    Last edited by dmcgov; 10-18-2019 at 09:06 AM.

  32. #32
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    ok, now i need to solve these two issues and i can move on to the next issue.

    here is the functions for lowerBounds and upperBounds:

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.

    and here is the output of both functions

    Please Login or Register  to view this content.
    so the first issue in red is that this line is not found in helper column O so the result of both functions should be ""
    the other issue is that the uppperbounds for the lastrow is zero and should be the the lastrow in column O (which should evaluate to 143)

    can someone help me out with this?

  33. #33
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    got the last one solved (not happy with the coding though, so it needs to be fixed) just need that and the fourth row to be "" on both columns.

    any help is appreciated

  34. #34
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    thoughts anyone, am i missing something easy?

  35. #35
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,288

    Re: return lbound and ubound of 2d array

    Hi, Dan!

    You complained that there has been "no traction" on this thread. Unfortunately, and I suspect this is relevant, you have moved onto a new issue here, and for this, you should have started a new thread. I strongly suggest you mark this thread as SOLVED and start a new thread with an appropriately-worded title that will attract the help you need.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  36. #36
    Valued Forum Contributor dmcgov's Avatar
    Join Date
    11-11-2015
    Location
    Florida, USA
    MS-Off Ver
    Office 365 Business
    Posts
    1,518

    Re: return lbound and ubound of 2d array

    ali,

    just wanted to apologize to the responders that my comment of no traction was just about the last bit of code, not the great help that i got before that. thanks to everyone in the forum for your help, i truly appreciate it.

+ 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] LBound and UBound of an array
    By billj in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-14-2016, 05:05 PM
  2. [SOLVED] UBound and LBound
    By jonathan.haynes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2013, 05:55 AM
  3. Trouble with Lbound and Ubound (code help please)
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-07-2013, 12:35 PM
  4. [SOLVED] LBound/UBound Intersect together
    By janulikb in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-21-2013, 05:41 AM
  5. Finding LBound & UBound of 2D Arrays
    By Mordred in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2011, 11:22 AM
  6. Ubound and Lbound
    By hommer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2010, 11:58 AM
  7. [SOLVED] resize(Ubound, Lbound)
    By ina in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2006, 10:00 AM

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