Macro for Calculating the No of months(count) between Two months - vba

I want to write a macro to calculate the no of Months(Count) between two months (strings)
Means, I want to know how many months are there between January to August
The result should show as 8.. I know how to write using Dates(Numbers), But using Month strings i have no idea..It would be of great help if anyone guide me on this ?

The question isn't too clear, but if I understand you correctly then you can use the following formula:
=MONTH(DATEVALUE("1" & A1 &"2014"))-MONTH(DATEVALUE("1" & B1 &"2014"))
Where A1 and B1 each contain a month name as a string. So for example if A1 was August and B1 was January, then the formula would show '7' as the output.
Using VBA, you can use the same code, replacing A1, B1 and C1 with the relevant ranges:
Range("C1").Formula = "=MONTH(DATEVALUE("1" & A1 &"2014"))-MONTH(DATEVALUE("1" & B1 &"2014"))"

Related

Dynamically select a column using month and year, then sumif the values based on criteria in another column

Here is an image of what I'm trying to achieve:
I am not well-versed in creating my own VBA script, but can understand code written by someone else if given time to dissect it. If this objective can be resolved without VBA, that would be my preference. I've explored using INDEX and MATCH but have gotten nowhere. Any suggestions?
I'm using Excel 2013.
I would "cheat"
I have my months list was in AI1:AI12, and year list in AJ1:AJ2 (for the dropdowns in A1 and B1 respectively). Move them elsewhere for more years.
Then in A2:
=MATCH(A1,AI1:AI12,0)
In B2:
=IF(MATCH(B1,AJ1:AJ2,0)=1,0,12 *( MATCH(B1,AJ1:AJ2,0)-1))
In D1O drag across to AA10:
=SUMIF($C$3:$C$8,"Plan",D3:D8)
Note: You could remove hardcoded "Plan" and reference a cell instead.
Result in C1 with
=OFFSET(D10,,B2+A2-1,1,1)
You are basically using a formula to determine how far to offset from month 1, year1 to get the current month and year conditional sum in row 10.
Example of finding the correct column, as described in my comment (untested code):
arr = array("Jan","Feb") 'you can do the rest
x = 2018 'first year in question, to make the math easier
s = 4 'column your first month is in
y = cells(1,2).value - x
z = application.match(left(cells(1,1).value,3),arr,0)
'all that gets you to:
c = s+(y*12)+z
You can then use the final column number, c, to do your Application.Sumifs() where you specify actual/plan as one of your criteria.

Excel: A function to replicate built-in change in relative references when copying formulas

I need a function that will do what Excel does automatically when you dreag a formula: change the referneces automatically.
For example:
In A1 I have "= A2 + A3"
If i copy this to say C3 it will have: "= C4 + C5"
I need to WRITE a formula in C3 that will produce this.
Any ideas? VBA solution is also welcome
CLARIFICATION:
In need this to be as general as possible.
Meaning A1 can contain ANY formula of any type, containing references to other cells.
for example: "= A2 + A3" or "= VLOOKUP(A2, $C$1:$E$7, 2, True)"
In need to move have this formula, whatever it is, copied to another cell (say C3), w/o the built in copy/paste, and have the references (that aren't set with $) change relatively.
I thought there might be a function to write in the destination file to do this.
I have tried writing an Eval function, and i managed to copy the formula from A1 and have it evaluated in C3, but the references would not change
This question lacks a bit of clarity, but I think this might be what you're after:
=SUM(OFFSET(C3,1,0,2))
This will sum the two cells directly below the given cell (in this case, cell C3). That is, it offsets C3 by 1 row, 0 columns, and grabs a height of 2 cells and then passes the result to the SUM function.
This VBA code would do what you are looking by setting the formula in the active cell:
ActiveCell.FormulaR1C1 = "=R[+1]C+R[+2]C"
You can use the Indirect() function using relative reference style.
For example, if you were in A1 and wanted to sum B1 & C1, it would look as follows:
A1: =INDIRECT("RC[1]",0)+INDIRECT("RC[2]",0)
That will change as you move the cell around to always sum the 2 cells to the left of the cell.
For your specific example (A1 = A2 + A3 || C3 = C4 + C5), it would be as follows:
=INDIRECT("R[1]C",0)+INDIRECT("R[2]C",0)
Hope that does the trick!!

Trying to compare two strings in excel

I have something like this:
A1: 0069
B1: 030069
In every case, I need to check if A1 is equal to B1 if I were to ignore the 03. I have a lot of cells, and the 03 is just there because its the format of the data. However, A1 can be multiple different lengths, so I can't just use the MID formula and just check B1 after the 03 because I would need to make multiple cases depending on how long A1 is.
Originally, I get the A1 data by using the formula:
D1 =FIND("-",C1), where C1 is A1 in the format: 0069-XXXX-XX.
I then use:
A1 =LEFT(C1,D1-1), which gives me the number 0069 as you see above. This way, I'll always have the correct length when I actually start the real problem.
My question is, can I use some kind of concatenation to just add a 03 to A1 and then see if it is equal to B1. I've tried using & and CONCATENATE, but because they are in general terms, I get the #VALUE error. Maybe I'm just not typing it in correctly, but yea.
Thanks!
You can just use & correctly in A1:
="03" & LEFT(C1, D1 - 1)
To compare these values you could use a formula like below:
=IF(A1=RIGHT(B1;4);"true";"false")
Actually it is not necessary to use the formula in A1 nor D1, you can use your data from column C straight away:
=IF(LEFT(C1;4)=RIGHT(B1;4);"true";"false")
If the pattern is Column C is nnnn-XXXX-XX then no helper column (Columns A and D) is necessary:
=MID(B1,3,4)=LEFT(C1,4)

Counting multiple criteria

I'm trying to count multiple criteria and got the code to work somehow with SUMPRODUCT.
Range("i2") = [SUMPRODUCT(($CZ$2:$CZ$61410="condition 1")*($DD$2:$DD$61410="condition 2")*($CU$2:$CU$61410=$A$2 <-/date/)*($CY$2:$CY$61410="condition 3"))]
There's around 40 colums with different conditions. So I have to code for each condition. But that's not really working for me, because I have to do the same thing for the whole month.
Explanation
Cells A2 to A32 hold dates, let's say from 1st of August 'til 31 of August.
Raw data table (on the same sheet) has dates on the CU column (might be 60000+ lines).
Column CZ, DD, CY, etc. have conditions that have to be met to be counted.
Question
What I want to do is if the date in column CU = the date in A2 then it will be counted to row 2, if the date in column CU = the date in A3 then it will be counted to row 3.
That way I can write one code for the whole month, not each day separately.
I offer first a solution of a formula (WorksheetFunction if preferred) of this type:
=COUNTIFS(CZ:CZ,"=1",DD:DD,"=2",CY:CY,"=3", ... CU:CU,"="&A2)
and an image showing what this results in (the yellow cell) depending upon the date chosen:
This counts the number of instances, on the date chosen in A2, that match the hard-coded criteria displayed in Row1 for illustrative purposes.
Without the hard-coding of criteria but with possibly a slightly better 'match' to the OP and with more examples:
Apart from the highlighting, the contents of C6 were entered via the following macro:
Sub Macro1()
Range("C6").Select
ActiveCell.FormulaR1C1 = "=COUNTIFS(C[101],R1C[101],C[105],R1C[105],C[100],R1C[100],C[96],""=""&RC[-2])"
End Sub

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

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.)