DateDiff("m", "06/14/1982", "09,01,1982") = 3
A1 = 06/14/1982
A2 = 09/01/1982
=DATEDIF(A1, A2, "m") = 2
If I need DateDiff to round down like DATEDIF does, how do I accomplish this?
You can use the Evaluate function in VBA. However, when working with dates in this manner, you need to ensure that the value being seen by the formula is the number corresponding to the date, and not a VBA Date data type. If the date is stored in a worksheet cell, you would do something like:
Evaluate("DATEDIF(" & [a1].Value2 & "," & [a2].Value2 & ", ""m"")")
If the dates are stored as dates in VBA variables, then:
Evaluate("DATEDIF(" & CDbl(DT1) & "," & CDbl(DT2) & ", ""m"")")
= Application.WorksheetFunction.RoundDown(DateDiff("d", a1, a2) / 30, 0)
Related
Good Evening
I have a problem on VBA I have a variable as countif
Dim Y As Integer
Y = Application.WorksheetFunction.CountIf(Worksheets("Calc_Giac").Range("I11:EZ11"), ">0")
So If I use the variable in a formula
Range("B21").Formula = "=SumIF((I11:EZ11), "">0"") / (" & Y & ")"
The value is correct
But if use in a condition the result is the variable*-1
If ("(" & Y & ")" > 0) Then
Range("L19") = "(" & Y & ")"
Else
Range("L19") = "HELLO"
End If
Any Idea about it?
First: You have a strange if-condition. Let's assume that Y has the value 3 as a result of your CountIf: "(" & Y & ")" resolves in a String "(3)". After that, you check if the string "(3)" is greater that 0. That makes no sense. Try
If Y > 0 then
Second: You write the String "(3)" into a cell. Now depending on how the cell is formatted, either you have "(3)" (as String) in the cell (when the cell is formatted as Text), or Excel tries to convert the string "(3)" into a number. However, numbers in Excel that are entered with brackets are interpreted as negative numbers (try it by entering "(3") into a cell manually. Use:
Range("L19").Value = 3
However, within the formula, =SumIF((I11:EZ11), "">0"") / (3), the (3) is just part of the calculation and the brackets tells Excel to evaluate anything within the brackets first before continuing the calculation. As 3 is already evaluated, there is nothing to do so the brackets are ignored.
How can I perform this calculation (something like AVERAGEPRODUCT) and make it work? I want to calculate the sum the cells of column Z after multiplying them with the cells of column AC and then divide them with the last cell of column AC. I am sorry for providing so few data but literally, the only thing I need is a working method to perform the line of code below. The line is really messed up . sorry for that.
Picture!
.Range("Z" & i & ").Formula = "=SUM((Z2:Z" & cnt + 1 & ")*(AC2:AC" & cnt + 1 & ")/Range("AC" & i))"
The worksheet function might look something like this:
=SUMPRODUCT(AVERAGE(A1:A3*B1:B3))
With VBA you could use something like this instead:
Range("C1").Value = Evaluate("=Average(A1:A3*B1:B3)")
For you:
.Range("Z" & i).Value = Evaluate("=AVERAGE(Z2:Z" & cnt + 1 & "*AC2:AC" & cnt + 1 & ")")
I have a data import file that has dates on it without a comma (i.e. January 1 2015). Excel won't recognize this as a date, and I need to work with these dates to find other dates (i.e. Date + Length of trip to find the last day of the trip, etc.)
I'm trying to use VBA to accomplish this and have the logic down but I'm encountering an error.
I'm taking the length of the date (January 1 2015), the 5 right characters (2015), the left of the whole length minus the 5 right characters(JANUARY 1), and then combining these variables with a comma inserted: Left(value-5) & ", " & Right(value, 5)
I'm using this code:
'correct date from JANUARY 1 2000 to JANUARY 1, 2000 so excel can recognize as date
LengthTrpDpt = Len(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol)) 'length of date
LengthRightTrpDPt = Right(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol), 5) 'finds right 5 of date " 2015"
NewListedDate = Left(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol), LengthTrpDpt - 5) & ", " & LengthRightTrpDPt
The problem is with the NewListedDate variable. I can change the part LengthTrpDpt - 5 to a number and it works fine. For some reason I can't have an equation here though. I tried perform the equation in a separately saved variable (LengthMath = LengthTrpDpt - 5) and using LengthMath instead, but that doesn't work either.
LengthTrpDpt works fine and MsgBox's the correct number. Any ideas? Thanks!
You should just Split() the date string to get your three parts, then piece them back together however you'd like:
Dim a
a = Split(wb1.Sheets("BL Import").Cells(ioi, TrpDepCol))
NewListedDate = a(0) & " " & a(1) & ", " & a(2)
Use Range.TextToColumns method on the column of text-that-look-like-dates.
with worksheets("Sheet1").columns(1) '<-change this to reflect the actual worksheet and column
.TextToColumns Destination:=.cells(1,1), DataType:=xlFixedWidth, FieldInfo:=Array(0, 3)
end with
The 0 tells T2C that it should just put everything back into the original column. The 3 is the MDY TextFileColumnDataTypes property.
imho, it is ALWAYS better to work with numbers and dates rather than a string representation approximating them.
To simplify, I have two sheets in an Excel workbook.
If I write =sheet2!R3C2 in one of the cells of sheet1, the cell takes the value of the cell R3C2 of sheet2 correctly.
I would like to generalize that for many rows in VBA, so my code is:
row = XX a long
temp = "=sheet2!R" & row - 1 & "C2"
Range("c" & row).Value = temp
But when I use this code, the value of the cell(row,3) is =sheet2!'RXXC2'
How can I remove the single quotes ??
Range("C" & row).Formula = temp
would produce the correct formula in your cell.
What you should consider doing instead of looping is
Range("A1").Formula = "=Sheet2!$B1"
Range("A1").Resize(100, 1).Formula = Range("A1").Formula
The first line inserts a formula =Sheet2!$B1 in cell A1 of your active sheet. The $ dollar sign assures that the column will not be incremented (same applies with numbers)
Then the second line duplicates the formula across 100 rows down from A1 replacing the number after the B column
So now
A1 =Sheet2!B1
A2 =Sheet2!B2
A3 =Sheet2!B3
...
Also, it's a bit unclear what you're trying to actually do so consider another option which is saving the value of formula into another range using the Evaluate() function
Range("c" & row).Value = Evaluate(temp) Or Range("C" & Row).Value = [temp]
try to write Range("c" & row).FormulaR1C1=temp
You want to set the formula, not the value:
Range("c"&row).FormulaR1C1 = temp
I created a macro that changes the layout of a raw excel file and displays some usefull information to the user. But I am stuck now.
I want to display the sum of some cells in a column, but the cell where the sum is and the column itself is set dynamically.
My layout looks like this:
Year1 Year2 Year3
Sum1 100
Sum2 50 48
Sum3 72 81
Sum4 26
------------------------------------------
Total
I want to display the sum of (sum1, sum2, sum3, sum4) in the Total row under each Year.
The thing is my years are set dynamically depending on the value of a parameter (i.e. I can have 2, 3 or 5 years).
I know I can do it using Range.WorksheetFunction.Sum but the problem is my values are subject to some changes after the macro has been used and if I use this function the values won't change afterward.
That's why I want to use the function Range.Formula to display these values as I can enter the sum like I did for the values already in the sheet.
But unlike these values (that are the sum of values in an unique column easy to select), I am not able to select the colum dynamically.
Here is what I tried to give you an idea of what I want to do if it is unclear to you:
For i=0 to Duration-1
Sheet2.Range("D" & RNumber + 7 + Duration).Offset(0,1+2*i).Formula="=SUM(Sheet2!" & Range("D" & RNumber + 6).Offset(0,1+2*i) & ":Sheet2!" & Range("D" & RNumber + 6 + Duration).Offset(0,1+2*i) & ")"
Next i
But it doesn't work as the sum doesn't recognize the range. I know I should have something like
"=SUM(Sheet2!E" & Firstrow & ":Sheet2!E" & Lastrow & ")"
but then i won't be able to select my colum dynamically.
It looks like your problem stems from the fact that you can't do things like "Sheet2!" & D+3+1 for columns the same way you can do "Sheet2!D" & 5+6+2 for rows. My opinion is that this is one of the many flaws created by the insane A1 reference style, and the best way to attack this problem at the root is to simply switch to R1C1 at the beginning of the macro.
With Application
If .ReferenceStyle = xlA1 Then
.ReferenceStyle = xlR1C1
End If
End With
Some day, I plan to start a non-profit effort to rid the world of the mis-begotten $A4:B$3 craziness altogether. But too many people are used to A1. And if you send your boss or co-worker a spreadsheet with numbers for columns, she might get upset or confused.
So the other solution is to introduce some ADDRESS() elements into your SUMs. The ADDRESS function takes only numbers (well, string for sheet reference) and will return a cell reference in whichever style you want.
For i=0 to Duration-1
Sheet2.Cells(RNumber+7+Duration, 5+2*i).Formula = _
"=SUM(INDIRECT(ADDRESS(" & RNumber + 6 & "," & 5+(2*i) & ",1,TRUE,""Sheet2""),1):" _
& "INDIRECT(ADDRESS(" & RNumber + 6 + Duration & "," 5+(2*i) & _
",1,TRUE,""Sheet2""),1))"
Next i
Or you can use Application.WorksheetFunction.Address() and avoid having the INDIRECT()s cluttering up your fomulas.