+ Reply to Thread
Results 1 to 6 of 6

VBA: Create Arrays based on an Array

  1. #1
    Registered User
    Join Date
    01-31-2018
    Location
    Switzerland
    MS-Off Ver
    2016
    Posts
    3

    VBA: Create Arrays based on an Array

    Hi guys

    I have a question. I have an items list in Excel with different categories. Now I would like to create an array with all the different categories. Then I would like to create an array for each category to put in the items.

    For example:

    Item 1 - Cat 1
    Item 2 - Cat 1
    Item 3 - Cat 1
    Item 4 - Cat 2
    Item 5 - Cat 1

    Now I read all the categories (Cat1, Cat 2, ...) in an array called arrCategories. After that I would like to dynamically create the arrays arrCat1, arrCat2.

    Do you have an idea how to do that?

    Thank you very much!

    Regards
    Lucas

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

    Re: VBA: Create Arrays based on an Array

    It's not clear to me exactly what you are trying to do. However, it sounds a lot like a concept I learned called a "jagged array":
    https://en.wikipedia.org/wiki/Jagged_array
    This is for standalone visual studio, so VBA may not be exactly like this: https://msdn.microsoft.com/en-us/lib...(v=vs.90).aspx
    a thread I started: https://www.excelforum.com/excel-pro...-practice.html

    Again, I am not sure exactly what you are trying to do, but I could see something like
    Please Login or Register  to view this content.
    could represent the table of data you show here. A better description of what you are trying to do might help us understand better, but that might be a reasonable starting point.

    I will note that I am not familiar with programming concepts like scripting dictionaries and userdefined types and such that might also be useful for the kinds of things you may be trying to do here.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    01-31-2018
    Location
    Switzerland
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA: Create Arrays based on an Array

    Thanks so far. I'm just trying to get back into Excel scripting - this will take me some time.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Create Arrays based on an Array

    What do you intend to do with the data afterwards? If you need to be able to refer to an array using the Category name, I would suggest a Dictionary is a good way to go. You can use the category names as keys and populate it with arrays for each key, or delimited strings, or even another dictionary.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Registered User
    Join Date
    01-31-2018
    Location
    Switzerland
    MS-Off Ver
    2016
    Posts
    3

    Re: VBA: Create Arrays based on an Array

    Hi

    I had to get warm again with VBA a little bit
    My question didn't make too much sense.

    But thank you!

    I had a list, like a stock list, with several 100 items with unsorted categories. My goal was to create a new workbook and enter the stock list formated per category in order to send these lists to customers with some additional calculation fields etc.

    My (easy) solution
    1) Read through all the lines and store them in an array (e.g. arrStockList)
    2) Read through the category field in the array arrStockList and create a dynamic array arrCategories
    3) Loop through arrCategories, create a heading (etc.) for each category in the new workbook
    4) Loop through arrStockList, add and format the item

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

    Re: VBA: Create Arrays based on an Array

    It sounds like it is working for you then. One thing I note, the process you describe sounds a lot like what a pivot table would do. If I am correct, I could see some value for you to learn what pivot tables can do -- especially if this kind of report creation is going to be a frequent thing you need to do: http://www.excel-easy.com/data-analy...ot-tables.html

+ 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. Create four arrays from one array
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 11-24-2016, 05:16 AM
  2. Replies: 13
    Last Post: 04-19-2015, 11:51 PM
  3. Replies: 6
    Last Post: 09-25-2013, 10:08 PM
  4. Replies: 3
    Last Post: 01-22-2013, 03:22 PM
  5. [SOLVED] How would one create a single list of #'s (array) from multiple lists (arrays)?
    By Rstrand in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-08-2013, 07:32 PM
  6. Replies: 2
    Last Post: 12-21-2012, 09:19 PM
  7. Replies: 4
    Last Post: 01-15-2011, 02:10 PM

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