+ Reply to Thread
Results 1 to 14 of 14

Populating Array

  1. #1
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Populating Array

    Please Login or Register  to view this content.
    Data is in columns A and B of worksheet.
    A1:A4 is filled with numbers :2,3,5,3

    B1:B4 is filled with names : jim, jim,david, jim

    This line of code [/CODE] numbers(i) = Cells(x, 2).Offset(0, -1).Value [/CODE]picks up the numbers in Column A
    based on this statement : [/CODE] If Cells(x, 2) = "jim" Then [/CODE]

    So I only want the numbers for jim to fill the array.

    Unfortunately when results are displayed via the message box they are : 3,0,0.
    So the array appears to be filled with the last number for Jim (3) and then two zeroes which do not correspond to Jim.

    Any help solving this problem would be greatly appreciated.

    Many thanks for reading

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Populating Array

    Try removing this line.
    Please Login or Register  to view this content.
    What is your objective with the macro? There should be a better way to do it.
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Thank you for your reply millz.

    However removing the line :ReDim Preserve numbers(i)

    Does not help and the output of the messagebox is the same : 3,0,0

    My objective is to only fill numbers for jim into array : 2,3,3

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populating Array

    See
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Thank you so much Jindon.

    Both worked Superbly!

    Could you please explain:


    n = n + 1: numbers(n) = Cells(i, 1).Value
    MsgBox Join(numbers, vbLf)


    numbers = Filter(.Parent.Evaluate("transpose(if(" & .Columns(2).Address & "=""jim""," & .Address & "))"), False, 0)

    ありがとう
    Arigatou Gozaimashita!

  6. #6
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Populating Array

    Quote Originally Posted by jim808 View Post
    My objective is to only fill numbers for jim into array : 2,3,3
    Objective as in, what happens after getting the numbers? Simply to display?

    If that is the case you may not need arrays at all.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populating Array

    1) test loop through the cells to find "jim" in column B, and when it finds increase the counter (n) and store the corresponding value to that index(n) of array(numbers)

    2) Evaluate method accepts 2d array so when the formula creates array, you can store the calculated values directly.

  8. #8
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Sorry I should be more clear. This has been deleted
    Last edited by jim808; 08-16-2016 at 08:48 AM.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Populating Array

    Now you are asking completely different question based on your thread title.

    Mark this thread as solved and start a new thread for your new question.

  10. #10
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Many thanks Jindon.

  11. #11
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Ah okay sorry. I meant thanks for last explanation.

  12. #12
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Sorry where to mark as solved ?

  13. #13
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Quote Originally Posted by jindon View Post
    1) test loop through the cells to find "jim" in column B, and when it finds increase the counter (n) and store the corresponding value to that index(n) of array(numbers)

    2) Evaluate method accepts 2d array so when the formula creates array, you can store the calculated values directly.
    Hi Jindon.

    I wondered why my line :

    If Cells(x, 2) = "jim" Then

    numbers(i) = Cells(x, 2).Offset(0, -1).Value

    does not work?

    I thought the logic was the same as yours which works :

    n = n + 1: numbers(n) = Cells(i, 1).Value
    MsgBox Join(numbers, vbLf)

  14. #14
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: Populating Array

    Quote Originally Posted by jindon View Post
    See
    Please Login or Register  to view this content.


    Dear Jindon, I understand evaluate function is being used instead of looping :

    numbers = Filter(.Parent.Evaluate("transpose(if(" & .Columns(2).Address & "=""jim""," & .Address & "))"), False, 0)

    I have problem understanding the evaluate function I have searched on google but it has yielded little to help my understanding.

    Filter(inputstrings,value[,include[,compare]]) .......could you please explain where values are being transposed to after being filtered?

    Range(a1.Parent.Evaluate.....?
    Last edited by jim808; 08-17-2016 at 04:05 AM.

+ 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] Is there a more efficient way of populating a VBA array
    By Philb1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-27-2014, 06:40 PM
  2. [SOLVED] Populating an Array with Values
    By fredrs05 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-29-2013, 12:19 PM
  3. Populating VBA array from excel
    By zurich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-01-2012, 03:32 PM
  4. Populating an array from a range
    By barryleajo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-26-2011, 02:48 PM
  5. Populating Array in VBA
    By phil_b in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-13-2010, 10:15 AM
  6. [SOLVED] populating a listbox from an array
    By Graham Whitehead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2006, 08:15 AM
  7. [SOLVED] Populating Two Dimensional Array
    By willz99ta via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-15-2006, 11:25 AM

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