+ Reply to Thread
Results 1 to 13 of 13

Using array for cleaner code

  1. #1
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Using array for cleaner code

    Hi,

    I am using a macro to consolidate data from multiple sheet in a workbook. It all works fine but a portion of the code is too long. I am new to all this but understand that array can be used to have a cleaner code. Can someone please help me on how I can shorten the code below using array? Thanks much in advance.


    .AddDataField .PivotFields(26), "Jan", xlSum
    .AddDataField .PivotFields(27), "Feb", xlSum
    .AddDataField .PivotFields(28), "Mar", xlSum
    .AddDataField .PivotFields(29), "Apr", xlSum
    .AddDataField .PivotFields(30), "May", xlSum
    .AddDataField .PivotFields(31), "Jun", xlSum
    .AddDataField .PivotFields(32), "Jul", xlSum
    .AddDataField .PivotFields(33), "Aug", xlSum
    .AddDataField .PivotFields(34), "Sep", xlSum
    .AddDataField .PivotFields(35), "Oct", xlSum
    .AddDataField .PivotFields(36), "Nov", xlSum
    .AddDataField .PivotFields(37), "Dec", xlSum

    With .PivotFields( _
    "Jan")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Feb")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Mar")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Apr")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "May")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Jun")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Jul")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Aug")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Sep")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Oct")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Nov")
    .NumberFormat = "###,0"
    End With

    With .PivotFields( _
    "Dec")
    .NumberFormat = "###,0"
    End With

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    Maybe this?

    Please Login or Register  to view this content.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    Hi Japan,

    Thanks much for the solution. That worked out nicely.

    One more question is what changes do I need to make to the code you provided to include a non-contiguous column? For example, field number 15 and Caption is "My Subs" like below. Of course, the non-contiguous one will have the same number format as others.
    .AddDataField .PivotFields(15), "My Subs", xlSum
    .AddDataField .PivotFields(26), "Jan", xlSum
    .AddDataField .PivotFields(27), "Feb", xlSum
    .AddDataField .PivotFields(28), "Mar", xlSum
    .
    .
    .
    .
    Raj

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    There is not much you can do with that, you will just have to deal with it separately.

  5. #5
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    Thanks Japan.

    Is there any way to create two arrays (one for the .PivotFields and one for captions and make this work? Just thinking out loud. If that can be done, it would be wonderful and I can take you for a beer!

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    No,No,No.. Don't offer the resident Excel Forum drunk a beer, all hell will break loose!

    Read my sig and click the PUI link.

    As for you Q, I am not sure I understand what you mean?

  7. #7
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    HA HA.....ok, no beer for you!!! Will introduce you to the hookah bar instead.

    In any case, I was wondering if two arrays can be created which would look something like

    d = Array (15, 26, 27, 28, 29, 37, 40,.........)
    e = Array (Total X, Jan, Feb, Mar, ..........)

    "d" containing column field number and "e" contains the column Caption in the pivot field

    And then use both of these arrays together in a loop to get the result needed which is to
    - Get data from the columns in array "d"
    - Use xlSum for each of them
    - Caption the pivot columns as per array "e"
    - Display data using the described number format

    I have the idea but very limited VBA knowledge. I know you wizards can do this.
    Last edited by rajeshkp; 07-04-2013 at 04:16 PM.

  8. #8
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    Well ,if you are only dealing with one item in the array, try this.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    Hi Japan,

    I haven't given your latest solution a try but if we have more than one non-adjacent column which we are trying to pull data from, can we somehow use "Case" somewhere in the code above? Sorry to keep bugging you but trying to see if you might have a solution. Thanks.

    Raj

  10. #10
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    Post the whole code and I will have a look at it.

  11. #11
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    Hello Japan,

    I used your suggestion with little modification. Kept getting error. Being a newbie, it took me a long time to figure it out. But here is what worked. Just needed to assign correct value to that darn "j"

    Please Login or Register  to view this content.
    Well, that was my work around. You might even have a better way to make it work.

    Really appreciate your help.

    Raj
    Last edited by rajeshkp; 07-04-2013 at 11:50 PM.

  12. #12
    Registered User
    Join Date
    07-02-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using array for cleaner code

    By the way, here is my complete code. If it can be somehow cleaned up and shortened, even better.

    Please Login or Register  to view this content.
    Raj Padhi
    http://www.linkedin.com/in/rpadhi
    Last edited by rajeshkp; 07-05-2013 at 12:18 AM.

  13. #13
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Using array for cleaner code

    Can you post a dummy workbook with sensitive data taken out?

+ 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