Excel VBA - looping formula array and pass it into variables - excel

i'm working on an exercise where i have the following columns
first column for primary key
second column for open date
third column for open price
fourth column for close date
fifth column for close price
The primary key will repeat multiple time due to different open date and close date through the year. So currently i'm able to use advance filtering function through vba and get an unique record of primary key, but i also need to find the min and max of open date and close date based on each primary key and identify the open price based on min of open date and closed price based on max of closed date.
can anyone help? because i can solve this with array formula within excel but no luck of converting each array formula into vba and pass it to variables.
sample list:
expected result:
array formulas used:
=MIN(IF(A1:A70926=J2,B1:B70926))
=INDEX(C:C,MATCH(1,(J2=$A:$A)*(M2=$B:$B),0))
=MAX(IF($A:$A=J2,$B:$B))
=INDEX(F:F,MATCH(1,(J2=$A:$A)*(O2=$B:$B),0))

I am not entirely clear on which max and mins you want but provided you convert your date strings to actual dates, you can use a pivottable to get any combination of max, mins from the date e.g.
With the above:
Helper column F has the formula in F2, which autofills down as source is set up as Excel table.
=DATE(LEFT(B2,4),MID(B2,5,2),RIGHT(B2,2))
This generates an actual date.
Pressing Ctrl+T, with a cell selected in the range, convertes the data into an Excel table, then Alt+N+V generates a pivottable from that table.
Then arrange as you wish. For example, with date in the rows area you can easily then select max and min dates via filter. You can have ticker either as a page field filter or add to the rows in front of the converted date. And then add open and closed fields to the values area. You can add the same field more than once. Make sure to right click on the first time you add and do value > field settings > min
Repeat for adding the same field and set as max.
Close up on pivot:
If you want the all time max and min value, simply remove the date field from the rows:
If you want for a particular year, when date field is in the rows area, right click and use the date filters functionality:

Related

Day calculation from PowerQuery not correclty transformed to excel table

I am using PowerQuery to create a certain date time duration column. I have the following xlsx table:
DateTime1: =A2+B2
DateTIme2=D2+E2
DateDiff=F2-C2
Now I want to transform Column G "DateDiff" with PowerQuery. I select the Range A to G and click on Data => From Table/Rage
I am asked where the data is and I use the prefilled entries and just click on ok. PowerQuery opens:
I select the column DateDiff and right-click and duplicate this column. A copy of this column appears and I select it. On datatype (where currently decimal is shown) I select "Duration". The values are transformed and I can see the following:
This is as expected. So first are the days, like for example 32 or 1 or 33 days, followed by the hours, minutes and seconds (and some rounding/calculation differnces in the seconds, but this can be ignored).
I click on close and load.
The table is added as follows:
The values were changed. Now column H does not show the 32,33 and 1 day anymore. The values are incorrectly shown as 1.07..., 2.08...
I do not understand why this happens? How can I get correct results here?
When I change the format to standard I get the correct numbers again. So the numbers were not changed. When I try to apply a custom format, like: T.hh:mm:ss the same wrong numbers shown. Leads to the identical problem.
It appears to be a formatting issue.
Most likely, you have col H formatted as d.hh:mm:ss
Unfortunately, Excel date d (day) format (or whatever the code for day is in your locale) will not display any value > 31. At that point it will wrap back to 1
One way to get the display you want is to turn it into a string:
=INT(A1) & "." & TEXT(MOD(A1,1),"hh:mm:ss")
You could also turn it into a string using Power Query
Add a custom column using this formula:
Text.From([DateDiff])
Then Split that column using the dot as the delimiter and select the right-most delimiter only.
Delete the unwanted columns.

Excel : MAX number in any 30 day period

I`m trying to work out a formula, on how to get the MAX amount, for any 30 day consumption period, for a certain item, when all the criteria are in one data table. See picture below. Date is in column A.
I have table with a formula where I copy and paste data manually
=SUM(IF([Date]>[#Date]-30,IF([Date]<=[#Date],[Amount])))
At the end- I would like to have the list of all the item numbers in one column and the one next to it should have - max amount used in any 30 day period.
In my Master file I have around 1300 Item numbers, which is time-consuming to get the MAX data on-by-one.
Can anyone help?
Instead of copy and paste, you can do a pivot table on the data in the first screenshot. Use the dates in rows, use the Amount in values. If you want to do this for item numbers, use them in rows before the date.
You should then have a nice table with dates and amounts per date, no duplicate dates.
If you don't want to use pivot tables, create the date column manually, without duplicates, and use a Sumifs formula to sum the data from the original sheet. Enter the start date, then use the fill handle and drag down. That will automatically increment the date. Let's say the dates are in column A, first row has labels, then use in B2 and copy down:
=sumifs('the other sheet'!H:H,'the other sheet'!A:A,A2)
Next, in C2 you can use a MAXIFS function along the lines of
=MAXIFS(B:B,A:A,"<="&A2-30)

Is it possible to output the values of a column in excel based on a dynamic date value?

In the following table I have weeks as columns, employees as rows and the values are the amount of holiday days per employee per week taken.
I want to create a separate table as a dashboard where the output will change depending on the date I input into the date selector. In the example below, I have chosen the 2/11/2015 as my week and I would like to only see the corresponding values for that week in the output table. How do I go about doing this?
Try this in B5:
=Index($B$34:$F$48,MATCH($A5,$A$34:$A$48,0),MATCH($C$1,$B$33:$F$33,0))
Then copy down.
Change the F column References in the formula to get the extent of you columns of data.
As a Note: If you are in a country that use ; as the delimiter between criteria instead of , here is the formula with those delimiters:
=Index($B$34:$F$48;MATCH($A5;$A$34:$A$48;0);MATCH($C$1;$B$33:$F$33;0))

Excel average values in time step

I have a problem with analyzing data measurement in Excel. Here is my issue.
In column A input is time (day.month hour), in column B it is day in year. From column C to I are numbers from measurement analyzer.
As you can see from the Excel Prnt Scrn, in single day we measured values in different time. I have to calculate average of all values (from value 1 to value 7) for specific day.
Example: average of values in Time 1.1.2012 (average values 1.1.2012 at 0:59, 1:59, 2:59 ….).
The problem is that I have more than 9000 rows with different time steps. I tried with Filter function, but I only manage to get time from older to newest!
Appreciate for any useful advice.
First, assign a value for all cells in the Day column using the Date number type. You will need to use Find and Replace to change all the "." to "/" in your Time column for this to work (i.e. DD.MM.YY HH:MM becomes DD/MM/YY HH:MM). Copy the Time column into the Day column. Highlight the Day column, right click, and select Format Cells. Change the format to Date, and it will cut off the time values. If you want a number for Julian Day and not the date, insert a new column and see this article for calculating day of the year: http://inanecoding.co.uk/2012/10/converting-excel-date-to-julian-day-of-year-number/
Second, use the Subtotal function under the Data tab to average each day's values. The this will work with date or Julian Day (or any column containing more than one unique value). This will change the appearance of the sheet but all the base data will still be there. However, for convenience of switching between raw and averaged data, I'd make a copy of the sheet before proceeding. Click Subtotal and in the dropdown menu "For each change in:" select which column (Date or Day) you want to use as a grouping. For the "Use function" menu, select Average. Below, check the columns you want to find averages for (Value 1-7). Click okay.
Since you have 9000 records, this may take a few seconds. Play around with the 1,2,3 boxes in the upper left corner to see the level of detail you want in the resulting spreadsheet.

Getting average of data with multiple entries for a single date using pivot table in Excel

I have tried this, but I am unsuccessful. I have the data dump in an excel sheet (Excel 2010). The first column has month. The second has the dates. The third column has numerical data. I have applied filters on each column to get the data on the Pivot Table. There are entries for each date. There are sometimes multiple entries for a single date. Now, when I run the Pivot, I get the proper sum, but when I select average, the average is accurate only for the single entries associated with a date. I need to calculate average of multiple data entries for a single date. Here is an example:
November 11/1/13 30
November 11/2/13 25
November 11/3/13 20
November 11/3/13 25
Now, when I run the Pivot table and select to average the data, it calculates the average as (30+25+20+25)/4 = 20 (since there are 4 entries, so the sum gets divided by 4).
This is not desirable.
I require the calculation to be made as taking the 3rd and 4th entry as one for the date 11/3/13. So, the required output is (30+25+(20+25))/3 = 33.33.
I hope the example gives an clear idea of the requirement. I need a solution for this. How do we modify the Pivot so that the average is calculated not based on just the number of entries, but the average is calculated for the number of days.
Create the pivot table described by Ron (with Sum only, not Average), then copy & paste special (values) this pivot table, then pivot that for averages.
Put your dates in the "Row Labels" area. It will Sum and Average by Date
Edit: To get the average of the sum of the daily values, you can either enter an Average formula someplace for the Pivot Table Sum Column, or you could use the following array-entered formula:
=SUM(Values)/SUM(1/COUNTIF(Dates,Dates))
Where Dates is the range where you have your list of Dates (on the original data sheet) and Values is the range where you have your original list of Values.
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
while hitting . If you did this
correctly, Excel will place braces {...} around the formula.
SUM(1/COUNTIF(Dates,Dates)) will return a count of the Unique dates, so it will return 3 for the dates in question in your example.
This proposed solution uses a working column named: Value (U)
Assuming that your data is located at [B4:B8].
Add a working column at [E4:E8]
Enter this formula at [E5:E8]:
= IF( COUNTIF($C$5:$C5,$C5) > 1, 0,
SUMIF($C$5:$C$8,$C5,$D$5:$D$8) )
Then use the working column Value (U) to sum and average the values, also add Value (U) as Page Field to filter out the Value (U)=0.

Resources