+ Reply to Thread
Results 1 to 15 of 15

VBA Create Yearly Extract Using Column Headings

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Create Yearly Extract Using Column Headings

    Hi, I wonder whether someone could possibly help me please.

    I'm using the code below to extract data from a 'Source' sheet to populate a "Yearly Extract Summary" 'destination sheet. With the unique distinct values copied from column I on the 'Source' ("All Data") sheet to column B on the 'Destination' ("Desired Output") sheet. In addition the values from column J on the 'Source' sheet are summed and paste under the relevant month on the 'Destination' sheet.

    Please Login or Register  to view this content.
    The code works fine and the correct figures populate the correct columns and rows on the 'Destination' sheet.

    As you can see from the code above, the monthly values have to be hard coded to match the column headings and this is fine when using a static 12 month period. But I'm now wanting to use a rolling 12 month period, which, at the moment, necessitates the need for me to change the code each month so I'd like to change the code but unsure where to even begin, having had help guidance to produce the initial script.

    I just wondered whether someone may be able to look at this please and offer some guidance on how I may go about achieving this.

    Although I appreciate that the whole script would need to be tweaked, and I'd still like maintain the existing functionality in this section of code:

    Please Login or Register  to view this content.
    To help I have attached a file which contain 3 sheets.
    • The "All Data" 'Source' sheet,
    • The "Output" sheet, used for testing, and
    • The "Desired Output" sheet which shows the results using the current code

    To run the code, please use the button at the top of the "All Data" sheet.

    Many thanks and kind regards

    Chris
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    12-07-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: VBA Create Yearly Extract Using Column Headings

    Hi

    Not sure how but I attached my response to your earlier post

    Re: VBa Dynamic Monthly Column Headings

    repeated here

    Replace your select case with
    Please Login or Register  to view this content.
    See attached, I've also added some code to set up a new year in the on change event of "Desired Output" sheet and clear the output.

    Regards
    Ian
    Attached Files Attached Files

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @ianxwood, thank you so much for taking the time to reply to my post, it is greatly appreciated.

    However, I'm very sorry, but unfortunately after testing I've found that the solution doesn't work. If you look at the results in the "Output" sheet they are different to those in the "Desired Output".

    May I also ask if you could clear the 'On Change' event please because I just really need to match the 12 month period I set on the sheet.

    The "Output" sheet is correct, but sadly the "Desired Output" sheet isn't.

    You will see that I've been fortunate enough to receive a working solution from @Leith Ross, so I leave this up to you whether you wish to pursue? From my perspective and out of courtesy it may be nice to see someone else's take on this, but I very much appreciate it if you feel you no longer wish to pursue.

    Many thanks and kind regards

    Chris
    Last edited by hobbiton73; 05-04-2014 at 10:19 AM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Create Yearly Extract Using Column Headings

    Hello hobbiton73,

    Your macro has been modified to scan the header on "Desired Output" for the dates. These are saved in a Collection object. This replaces the Select Case statement. The value of "m" is 1 for the first date, 2 for the second date, etc. This is gonig from left to right. The collection object will return "m" for a date if it is in the collection. If not, it generates an error (this is normal behaviour), which is trapped and the code continues.

    Update Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    12-07-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: VBA Create Yearly Extract Using Column Headings

    Hi Chris

    Wrong version got attached, see attached
    Surely you need to reset ever month but feel free to remove or comment out the code in the on change event

    Regards
    Ian
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @ianxwood, thank you very much for coming back to me with this so quickly.

    I've tested the updated code you kindly provide, although the problems I initially experienced are no longer there, thank you.

    Whilst testing though, I'm sorry but I've noticed another issue. I changed some of the date values around (column J on the All Data) sheet, just to make sure everything was ok. I've attached an updated file with an illustration of the issue and the row in question is highlighted in orange on the "All Data" sheet.

    If you look, the date in column J on the highlighted row, is "Feb 14", but when I run the macro, the figure appears under the "Feb 15" column on the "Desired Output" sheet, whereas this column should be blank.

    I'm very sorry to have to trouble you again with this, but I just wondered whether you may be able to look at this please.

    Many thanks and kind regards

    Chris
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @Leith Ross thank you so much for taking the time to reply to my post and for putting the code together which works brilliantly, thank you so much.

    You will no doubt see my last post to @ianxwood, so I hope this doesn't offend you, but I felt it only right that I should offer the courtesy in allowing him to continue to work on his take of the solution

    May I ask though, because I'm eager to learn from this, may I ask if you could insert comments into the code, so I can understand the additions.

    All the best and kind regards

    Chris

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA Create Yearly Extract Using Column Headings

    Hello Chris,

    No problem I understand.

    The additions I made automatically scan the header row, row 7, of the "Desired Output" worksheet from column "D" to the last the last cell in that row with a column.

    The text of each cell in the header row is saved into the Collection "ColDates" as its key. The key in a collection allows you to randomly reference an element in the collection. If the key exists then a stored value is returned. Otherwise, the error "Subscript out of range" error is thrown. That is accomplished by this code seggement...
    Please Login or Register  to view this content.
    You were using a "Select Case" statement that contain hard code column headers. The collection object "ColDates" replaces this by using "Pdate" to return the column offset "m" directly. You no longer have to search one by one for a possible match. If any entry does no exist and error will be thrown an the error object "Err" will be a number other than zero and no cells are updated.. If "Err" is zero then the value is copied to the correct column. Here is the code segment to perform those checks...
    Please Login or Register  to view this content.
    Collections offer you a way to randomly the access the elements and adapt to dynamic environments since elements can be added or deleted at any time. Random access elements having to walk through an array of elements to find the one you want. Eliminating time consuming search loops can greatly improve speed and flexibility of your code.

    I hope this answers some of your questions. Let me know if you have any more questions.

  9. #9
    Registered User
    Join Date
    12-07-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: VBA Create Yearly Extract Using Column Headings

    Hi Chris

    New version attached, this checks that pdate is within the range D7 to O7 on desired output before calculating the offset.

    Regards
    Ian
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @ianxwood, thank you very much for taking the time to come back to me with this. Thank you also for the code which works great!

    May I ask, if it's not too much trouble, could you possibly add some comments to the code, so I can learn from this.

    All the best and kind regards

    Chris

  11. #11
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @Leith Ross, thank you very much for taking the time to come back to me with this, it's exactly what I was after!

    All the best and kind regards

    Chris

  12. #12
    Registered User
    Join Date
    12-07-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    31

    Re: VBA Create Yearly Extract Using Column Headings

    Hi Chris

    New version attached with comments added.

    Regards
    Ian
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @Leith Ross, I hope you are well, and I wonder whether you may be able to help me please.

    You will no doubt remember that you kindly helped me with a solution to the post I made a few days ago.

    I'm sorry to trouble you but while I've been testing this I've come across a slight issue, which I'm hoping you may be able to help me with please.

    I've noticed that in the case of where there is a row of data on the "All Data" sheet with a date in column J prior to my first column on the "Desired Output" sheet i.e. "April 14", although the value is correctly not paste to this sheet, the "Description", column I on the "All Data" Sheet is still shown in column B on the "Desired Output" sheet, thus creating a blank row.

    I've tried to rectify this over that day or so, but I've been unable to do so. I just wondered whether you could possibly take a look at this please.

    To help I've re-attached the last file you post with the code and highlighted the row on the "All Data" and "Desired Output" sheets.

    Many thanks and kind regards

    Chris
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi @ianxwood, thank you very much for this, I really appreciate it.

    Many thanks and kind regards

    Chris

  15. #15
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Yearly Extract Using Column Headings

    Hi all, I was unable to contact the original author of the code so I made a post here:http://www.vbaexpress.com/forum/show...act&highlight= where you will see I received that little bit of help I needed.

    Many thanks and kind regards

    Chris

+ 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 macro to copy data using column headings
    By zkhan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2013, 02:01 AM
  2. Macros to lookup column and row headings and extract data
    By ramliedt in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-04-2013, 11:33 AM
  3. Trying to create a new sheet based on column headings selected
    By davidhale87 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-11-2013, 01:47 PM
  4. Create Headings from Column Data
    By alphag_25 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-02-2006, 05:45 AM
  5. [SOLVED] How do I create column headings in Excel in the A, B, C etc column
    By eliannaj in forum Excel General
    Replies: 2
    Last Post: 11-23-2005, 02:55 PM

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