+ Reply to Thread
Results 1 to 8 of 8

Average data based on unique change in date, ignoring time

  1. #1
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Average data based on unique change in date, ignoring time

    Hi,

    I have a question that is probably a combination of VBA & Excel formulas, so if I put this in the wrong location, please let me know & I'll move it.

    Anyway, I have a CSV data file with 3 columns:

    Column 1 Column 2 Column 3
    (Text) Date (in the following format) Data (floating point)
    a 9/19/2013 12:02:00 AM 6.58667
    b 9/19/2013 12:03:00 AM 6.44361
    c 9/19/2013 12:04:00 AM 6.32134
    d 9/19/2013 12:05:00 AM 6.54321
    e 9/20/2013 7:26:00 PM 10.11111
    f 9/20/2013 7:27:00 PM 10.23345
    g 9/20/2013 7:28:00 PM 10.54321
    h 9/20/2013 7:29:00 PM 10.67890
    i 9/21/2013 11:58:00 PM 3.12345
    j 9/21/2013 11:59:00 AM 3.54321
    k 9/21/2013 12:00:00 PM 4.11111
    l 9/21/2013 12:01:00 PM 4.54321



    The goal is to take an average of the Data in Column 3 for each instance when the Date in Column 2 changes, ignoring the time.

    So, in the example above, the result would be 2 more columns, each with the following data:

    Column 4 Column 5
    (Unique Dates) (Average of each Date)
    9/19/2013 6.4737075
    9/20/2013 10.3916675
    9/21/2013 3.830245

    If this can be automated such that, when the CSV file is pulled into Excel, a macro can be run to produce Columns 4 & 5, that would be the most ideal solution.

    My confusion lies in the fact that the number of rows can change, so I'm thinking a macro would be the preferred method - but the commands in VBA are not the same as in straight Excel, and I'm not sure how to do it via a macro. In straight Excel, I could use something like:

    =TEXT(A1,"d/mm/yyyy")

    Or even "int" - but what would the equivalent in VBA be?

    If not, any assistance would be appreciated.

    Thanks in advance,
    Rob

    P.S. Sorry, the formatting isn't coming out correctly.
    Last edited by rbrookov; 10-16-2013 at 11:46 AM. Reason: formatting, adding what I've already tried. Marked as SOLVED

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

    Re: Average data based on unique change in date, ignoring time

    Try this macro, run it after you open the CSV file:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Average data based on unique change in date, ignoring time

    Millz,

    That works perfectly - thank you very much!!

    Might I ask - how did you get so good at VBA? Do you use the macro recorder? I have been looking around for some tutorials, but none showed exactly what I needed as far as knowing how to average only the dates which changed, ignoring the times.

    Would you mind explaining (such that I can learn & be better at Excel VBA) what some of the lines mean? More specifically, why did you use:

    RC[-2] - what does the "-2" do?

    Also, why did you put this into a pivot table? Was it just because it was easier that way? Or is there an advantage? Can it be accomplished without using a pivot table?

    I have another question - this macro runs on the tab where the "raw" data resides. What if I want to run the macro on another tab (called "output") and have it grab the data from the raw data tab (called "raw_data"). How would the macro need to be changed?

    And while I'm at it - the data from "output" is used to make a chart - how can I get the chart to update automatically without having to go in & redefine the data ranges (basically, have it be smart enough to know that the range is dynamic)?

    Anyway, I really appreciate this!!

    Thank you!
    Rob
    Last edited by rbrookov; 10-08-2013 at 05:40 PM. Reason: question about what some of the VBA code does in order to learn

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

    Re: Average data based on unique change in date, ignoring time

    Hi Rob, I wouldn't say I am 'so good' at VBA, and yes I use macro recorder ever since I started learning VBA ~3 years ago. I learned most of my stuff through the recorder and the help file. I still use the record often, when I am unsure of certain syntax, like if I were to mess with setting borders to cells now, I would first record it.

    First I would say I use pivot table because it is indeed a much easier way to get 'unique' records, in this case the dates. And you wanted average data of the unique records, that's the exact purpose of pivot tables - to get summaries, be it Sum, Count, Average, etc.

    Below are some explanation to the code:
    Please Login or Register  to view this content.
    Will get back to you regarding running it on a different sheet, I don't have time to show the code now.

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

    Re: Average data based on unique change in date, ignoring time

    Actually, it would be good if you can post a sample workbook. By "tab", do you mean worksheet or workbook? Because I think CSV files can only contain a single worksheet.

    If you meant worksheet, you want the macro the create a new worksheet and display the output?

    As for making chart to 'update' source data dynamically, I usually set it through VBA code, but that's just my style. It can also be done by using named ranges. There is a good trick shown here by SoulPrisoner regarding dynamic named ranges, maybe you can look into it too.

  6. #6
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Average data based on unique change in date, ignoring time

    Millz,

    wow, thank you very much for the comments in the code - that really helps others to gain an understanding of what each line does!! I now can understand what is happening, and improve my skills from there, as well as explain to others who will be using this what is going on!!

    As far as the dynamic ranges - I *think* I figured it out by just using COUNT(start cell: end cell). For some reason though, even Microsoft's help guide was instructing to use a combination of "COUNT" and "OFFSET" (see here: http://support.microsoft.com/kb/830287 ). I couldn't get that to work for me, nor could I understand why someone would need to use OFFSET with COUNT when COUNT seems to work fine.

    As far as "tabs" - yes, I mean different tabs within 1 workbook (I guess "tabs" are also called "worksheets"). For sake of argument, let's call a WHOLE spreadsheet (.xls file) a "workbook" while within ONE workbook, there can be multiple "tabs" which we'll call "worksheets." Also, while the actual data is too long & boring to post here (not to mention the company probably doesn't want us posting their raw data here), suffice it to say that the data in it's "raw" CSV format looks very much like the example I presented in my 1st post - it's just ONE file with a 3-4 columns & a BUNCH of rows (which can vary from file to file).

    Anyway, the user pulls that data into Excel from the CSV file & puts it into the 1st worksheet (or "tab") - let's call this worksheet "raw_data." Since we would like to keep that data "pure" (meaning, not do any manipulations to it on THAT worksheet, or tab), we would want another worksheet (or tab) to be the "output" worksheet.

    The "output" worksheet is where I would want the macro to run from, and where I would like the output to be pasted to, but it would need to be smart enough to go over to the "raw_data" worksheet, perform it's commands, then paste the parsed & manipulated data back onto the "output" worksheet (or tab).

    Hopefully this makes sense??

    Anyway, thank you for all you have done thus far!!

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

    Re: Average data based on unique change in date, ignoring time

    Sorry was kinda busy.

    Yes it makes sense, hope this is what you want. Run it right after you open the CSV file. A new worksheet will be added.
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    04-14-2011
    Location
    Bay Area, CA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Average data based on unique change in date, ignoring time

    Quote Originally Posted by millz View Post
    Sorry was kinda busy.

    Yes it makes sense, hope this is what you want. Run it right after you open the CSV file. A new worksheet will be added.
    <SNIP CODE>
    Millz - yes, this works perfectly!! Thank you very much!! I will now close this thread as "SOLVED" and add reps to your account!! Again, THANK YOU!!

+ 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. Average data consecutively between range of date-time
    By paluaja in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-14-2013, 12:27 AM
  2. [SOLVED] Creating an average from a set of data based on an initial start and end date and time
    By MKF2013 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-09-2013, 02:01 PM
  3. Average of cells matching date criteria ignoring DIV/0
    By wiggum in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2011, 04:48 AM
  4. change date based on time
    By kdp145 in forum Excel General
    Replies: 7
    Last Post: 12-13-2005, 10:10 PM
  5. [SOLVED] Creating a unique ID number based on date and time
    By WillRn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-03-2005, 09:06 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