+ Reply to Thread
Results 1 to 13 of 13

Using VBA to Append Names to an Array (Excel)

  1. #1
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Using VBA to Append Names to an Array (Excel)

    Hi,
    I am attempting to manipulate data that is stored in a pivot table. The data in the pivot table is a quote system, showing each quote that has been processed and which employee has processed it. I am simply trying to loop through the list to store each employees name in the same array, and then print these names onto the page (it should only add each name to the array once, there are no duplicates). I have coded it into a test workbook and attached a test workbook, but I am having trouble understanding how arrays work on VBA, as my loop is clearly not working.

    Any help is appreciated
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using VBA to Append Names to an Array (Excel)

    For a unique list, I'd suggest a dictionary (or collection) rather than an array. Your code is then much simpler:

    Please Login or Register  to view this content.
    for example.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Re: Using VBA to Append Names to an Array (Excel)

    Thank you very much. Would there be a way to assign a value to each of the names in the dictionary? for example the total revenue bought in by "James".

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using VBA to Append Names to an Array (Excel)

    Yes, although you appear to be beasically replicating the functions of a pivot table.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using VBA to Append Names to an Array (Excel)

    Quote Originally Posted by georgedixon
    Would there be a way to assign a value to each of the names in the dictionary? for example the total revenue bought in by "James".
    To expand on Rory's code.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Re: Using VBA to Append Names to an Array (Excel)

    Yes haha, I have never used pivot tables before but am reasonably familiar with VBA hence was attempting to do it this way. Thanks again, that works perfectly.

  7. #7
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Re: Using VBA to Append Names to an Array (Excel)

    Quote Originally Posted by Norie View Post
    To expand on Rory's code.
    Please Login or Register  to view this content.
    Hello again, I was just transferring the code to my other sheet and realised that when it stores and sums the sales values, it stores them as strings (with the £ sign), I was just wondering if there was a way to purely store the numerical value? as the it seems to convert them to text otherwise and I can not manipulate them further.

  8. #8
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Re: Using VBA to Append Names to an Array (Excel)

    Ignore my previous comment, I found a way round it, Thanks again

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using VBA to Append Names to an Array (Excel)

    That's totally bizarre, nowhere in the code is the pound sign being added and if the pound sign was being included when adding the values in the dictionary I would expect to get type mismatch errors.

    Also, when I check in the immediate window none of the items in the dictionary seem to have a pound sign.

    Even weirder if I change the code to this,
    Please Login or Register  to view this content.
    you still get the pound signs but the values are numeric.

  10. #10
    Registered User
    Join Date
    07-25-2017
    Location
    England
    MS-Off Ver
    2013
    Posts
    86

    Re: Using VBA to Append Names to an Array (Excel)

    I think that originally because the code was retrieving values that had been formatted as currency it was taking them to be part of the value for some reason and then storing it as a string. My way around it was just to add in another macro after the lists had been generated that removes them

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Using VBA to Append Names to an Array (Excel)

    As far as I could see when debugging the values being retrieved weren't formatted as currency.

    If they were then the addition in the code would have failed with a type mismatch.

    I'm totally mystified as to where the £s are coming from in the final result.

    Perhaps we should get somebody outwith the UK to give it a try and see what happens.

  12. #12
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,286

    Re: Using VBA to Append Names to an Array (Excel)

    Hi Norie
    in Ireland but I get no £ or currency at all from the data or code

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,247

    Re: Using VBA to Append Names to an Array (Excel)

    The cells are formatted with a currency format, so the .Value property returns a Currency data type. That's why you get the formatting when it's assigned to a cell (and also rounding to 2DP). Using .Value2 instead would remove that behaviour.

+ 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] Copy and append an array of cells based on a separate date table
    By concatch in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 01-09-2018, 06:23 PM
  2. Reading closed spreadsheet in as an array to check and append result to email subject
    By davepoth in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 05-31-2015, 11:57 AM
  3. Replies: 0
    Last Post: 08-29-2013, 10:07 PM
  4. [SOLVED] append multiple worksheets data to a single multi-dimensional array
    By junoon in forum Excel Programming / VBA / Macros
    Replies: 36
    Last Post: 04-10-2012, 02:28 PM
  5. Create array of file names/sheet names
    By BVHis in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-06-2008, 11:30 AM
  6. [SOLVED] Trying to append to file using an array
    By Susan Hayes in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2006, 05:00 AM
  7. Append data from a column to separate table array
    By miss_q in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-12-2005, 07:01 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