How to sum data in one column based on the same month in another column using macros - vba

I am using macros in excel 2007 for my work. I am working with many data and I need to sum data from 2 or more rows in the same coloumn according to the same month. However the month column is expressed as date.
for example, i have series of data
A B
2/10/2008 2
2/10/2008 3
4/10/2008 3
5/11/2008 4
5/11/2008 5
I want the result to be displayed in column C and D as followed
C D
Oct/08 8
Nov/08 9
I am very thankful if anyone can help me.
regards,
Tifu

A B C D E F
1 10/ 1/2008 24106 1 Oct-08 24106 8
2 10/31/2008 24106 7 Nov-08 24107 11
3 11/ 1/2008 24107 8 Dec-08 24108 6
4 11/30/2008 24107 3
5 12/ 1/2008 24108 2
6 12/ 2/2008 24108 4
B1 =MONTH(A1)+YEAR(A1)*12
E1 =MONTH(D1)+YEAR(D1)*12
F1 =SUMIF(B$1:B$6,CONCATENATE("=",E1),C$1:C$6)
I had to overcome two problems to solve this. First, SUMIF can only do direct comparison, it cannot run a function on the source location (except for range functions, which the date and time functions are not), so I had to add the B column. The E column is optional, it could be implemented as part of the formula in F, but I left it independent for illustrative purposes. Second, SUMIF takes a string parameter describing the comparison, so I built the necessary string (it is "=24106" for F1) on the fly.

using array functions:
C1:
=date(2008,small(month($A$1:$A$10),1),1)
C2:
=date(2008,small(month($A$1:$A$10),2),1)
right click on these cells and format them as mmm/yy
D1:
=sum(if(month($C1)=month($A$1:$A$10),($B$1:$B$10),0))
make sure to press ctrl-shift-enter when done writing this formula.
then copy an paste it down as needed.

If you are unfamiliar with VBA, I would start off by recording a macro while doing what you want to do by using the Subtotals feature under the Data menu (i.e. through Excel's interface).
Once the macro is recorded, you can look at the VBA code produced, and alter it to suit your needs.

This should be possible to do using regular excel formulas:
extract the month of the date (some Month-function)
then do a conditional sum (function sumwhen (?)) like this:
= SumWhen(A:B, ReferenceDate, B:B)
(with ReferenceDate = C2 C3 etc.)
(All I have is a German excel and the function names got translated as well, so my function names may be off. Maybe someone can check against an English excel and update if necessary.)

Related

Find duplicates Value and sum a duplicates using index number Using VBA

I need a little help here for excel and VBA, lets says my column like this
No Name Rank Point
1 Foo 1
2 Bar 1
3 Jhon 1
4 Doe 1
i need to fill a point using adds the same rank by using index of number and then divide by the number of duplicates value, point = (1+2+3+4)/4 i need an help for using a VBA script, any of help will be appreciate
You dont need VBA for thet. If your data is from A1 to C5 (with Point in Column D), lke in your example, put the following formula in D2 and pull it down:
=SUMIF($C$2:$C$5,C2,$A$2:$A$5)/COUNTIF($C$2:$C$5,C2)
If you need VBA, just add Application.Worksheetfunction to both formula parts.

(VBA) Extract and copy and paste column values for specified date range for corresponding row values

I've been stuck on this for a while now. What I need is the title - what I have is:
Sheet 1:
1 billion mostly unnecessary columns.
Column D: Values I need Column F: Column M:
Revenue Names Date
12 John 1/24/2016 2:40:02 AM
15 Sarah 3/2/2016 4:35:17 PM
14 Sarah 7/17/2016 1:50:10 PM
20 Matt 8/20/2012 4:16:12 AM
10 John 11/19/2015 5:04:05 AM
etc. etc.
Current Sheet 2: Pivot Table*
Row Label:
Sarah
Matt
John
etc.
Desired Sheet 2 Pivot Table+*
Row Label: Column __:
Revenue
Sarah 29
Matt 0 *(note: see below, but = 0 because Matts value corresponded to date 2012)
John 22
etc.
The important thing about Sheet 2 is that I would like to tell VBA to find the nearest emptiest column within the sheet, and input the values from Column D from S1 (and sum for duplicates) but only sum and extract values that range from today's date to 11 months prior. Currently I made a module to automatically create a pivot table for the next sheet, but I have wanted to add the above for a while, just stuck.
I'm also assuming I won't have to specify sum if I tell VBA to extract data for corresponding row label names? And I was thinking about putting a Begin Date and End Date cells on Sheet 2 to refer to, or using the TODAY() function somehow for the date, but not sure how to specify to extract and sum values for the date range from today to 11 months prior.
Edit: *Please note this is a watered down version of my current situation, everything is bigger so to avoid confusion, pivots are necessary, but I choose to dilute it for the sake of this issue.
Do you really need a pivot table?
If you dont, just create a new sheet with all the names, set a cell for the starting date you want to consider and do =sumifs
For example
On Sheet2 B1 you put 1/24/2016.
Then on A2 you insert the name (Sarah, for example)
On B2 you put the formula
=Sumifs(Sheet1!D:D,Sheet1!F:F,A2,Sheet1!M:M,">"&B$1)
After that just make a list of name and drag the formula beside every name.
*My excel is in portuguese so maybe the formula needs some other ajustments to work.

VBA to check for blank cells in columns based on value in another column

Given
O 1 2 3 A
A 4 5 6 B
B 7 8 9 D
O 3
C 15
T 18
I'm looking for VBA code to validate that when column A contains a value that the remaining columns also contain values and when it doesn't contain a value, that columns 2 & 5 also contain values but 3 & 4 don't.
I've simplified the example, in a real sheet there will be many more columns and rows to check.
I've considered COUNTIF and INDEX/MATCH and array forumlas but from my understanding these all work on single columns at a time.
I want to do something like WHEN A1:An<>"" THEN COUNTBLANK(B:E) ELSE COUNTA (C:D)
Is the best way to use autofilter using blanks in A and then countblank and then a second autofilter for values in A.
Thanks
You can do it with a couple of nested IF formulae as follows:
=IF(A1<>"",
"A not empty, "&IF(COUNTBLANK(B1:E1)=0,
"B:E not blank",
"B:E have blanks"),
"A blank, "&IF(AND(COUNTBLANK(B1)+COUNTBLANK(E1)=0,
COUNTBLANK(C1)+COUNTBLANK(D1)=2),
"Columns 2&5 have values and Columns 3&4 don't",
"but condition not met"))
The reason for going down the VBA route is that I want a generic reusable function as opposed to a formula I copy between cells and sheets changing the columns etc along the way ending up with a lot of duplicate code.
So something that takes a column to test and a value to test it with. Third parameter would be a range of columns to validate, and the fourth parameter the validation.
I don't want any solution to have the columns hard coded and I don't want intermediate totals at the end of rows. This is fairly easily achieved in Excel itself...
The reason for trying to use countblank is that I can apply it to a range.
After a lot of searching I discovered this (the columns don't match the original example)
=SUMPRODUCT((A2:A19<>"")*(B2:D19=""))
=SUMPRODUCT((A2:A19="")*(D2:D19=""))
=SUMPRODUCT((A2:A19="")*(B2:C19<>""))
Nice huh? I just need to convert it into VBA now.
Thanks

IF construct in Excel

Can anyone help me perform the following...
example:
A B C D
row 1 ODZN 2 3
row 2 EAXO 3 4
I need a IF then statement which gives this logic...
If A:1 is ODZN, then D1=(B:1)*(C:1)*5 , else if
If A:2 is EAXO, then D1=(B:2)*(C:2)*20 ... and so on with different variables....
every different variable has a different multiplier.......
and I'll copy and drag the formula down a large set of data and eventually into a macro
could anyone help.. and possibly advise me what I need to define as a variable...
The multiplier is set by me manually. The variables will be defined in excel.
Thanks!!
I suppose you mean:
for a row, if value in column A is ODZN / EAXO, then value in column D gets a multiplier 5 / 20.
Answer:
=B1*C1*IF(A1="ODZN",5,IF(A1="EAXO",20,0))
and then drag.
You can nest as many IF as you want. (Here I use I an unnecessary 0 to show how IF can be nested, it can be simplified as IF(A1="ODZN",5,20)).
Of course, you can write a function, then it will be like
=B1*C1*func(A1)
However, macros need authentication to run in later versions of Excel. I would recommend the formula solution if the value variation is not that many.

Excel countif Pulling apart a cell to do different things

Excel 2007
I have a row of cells with variation of numbers and letters (which all mean something.. not random.)
It's basically a timesheet. If they take a sick day they put in S, if they take a partial sick day they put in PS. The problem is they also put in the hours they did work too. They put it in this format: (number)/PS.
Now if it were just letters I could just do =countif(range,"S") to keep track of how many s / ps cells there are. How would I keep track if they are PS where it also has a number separated by a slash then PS.... I also still need to be able to use that number to add to a total. Is it even possible or will I have to format things different to be able to keep track of all this stuff.
Assuming this is something like what your data looks like:
A B C D E
1 1 2 S 4/PS 8
...then you could do this:
1- add a column that just totals the "S" entries with a COUNTIF function.
2- add a hidden row beneath each real data row that will copy the numerical part of the PS entries only with this function in each column:
=IF(RIGHT(B1,2)="PS",IF(ISERROR(LEFT(B1,LEN(B1)-SEARCH("/",B1)-1)),"",INT(LEFT(B1,LEN(B1)-SEARCH("/",B1)-1))),"")
3- add another column to the right that just totals the "PS" entries by summing the hidden row from step 2.
3- add another column that totals everything by just summing the data row. that will ignore the text entries automagically.
4- have a grand total column that adds those three columns up
If you don't want to see the "S" and "PS" total columns, you can of course just hide them.
So in the end, the sheet would look like this:
A B C D E F G H I J
1 1 2 S 4/PS 8 1 4 11 16
2 4 <--- hidden row
HTH...
My quick take on this is:
pass the cell value into a CSTR function, so no matter what is entered you will be working with a string.
parse the information. Look for S, PS, or any other code you deem to be valid. Use Left or Right functions if you need to look at partial string.
check for number by testing the ascii value, or trying a CINT function, which will only work if the string can be converted to integer.
If you can show a sample of your cells with variation of numbers and letters I can give you more help. Hope this works out.
-- Mike