VBA is complaining about something but I don't know what that is.
My code:
Sub datesnstuff()
Const today = Now()
Const yesterday = Date(Year(today),Month(today),Day(today) - 1)
End Sub
And it gives me the following error:
Compile error:
Expected: )
I think it is clear what I want it to do, but Excel is being a pain. It won't even let me use Date(2015,1,1).
Any help would be appreciated.
Date is a property and does not accept parameters.
You want DateSerial().
The Date() function does not take any parameters, and returns the current system date. Try:
yesterday = DateAdd("d", -1, today)
Also, you cannot declare a Const with a function on the right hand side of the = sign. It must be a constant value.
Related
I need help to change the following function into VBA code. This will be part of a larger code.
IF((WEEKDAY($B12)=7),$I12,"")
There are probably more than 5 ways to do what you want, depending on what exactly do you need. One of these ways is to build a simple custom formula like this:
Public Function changingIfAndWeekday() As Variant
Application.Volatile
If Weekday(Range("B12")) = 7 Then
changingIfAndWeekday = Range("I12")
Else
changingIfAndWeekday = ""
End If
End Function
You could also do it like so (if you want the result on cell C12):
Sheet1.range("C12").value = "=IF(Weekday(Sheet1.range("B12").value = 7),Sheet1.range("I12").value,"")
You could also do it like so (if you want the result on a variable):
Variable = "=IF(Weekday(Sheet1.range("B12").value = 7),Sheet1.range("I12").value,"")
Is there a way to override Now() in VBA for testing purposes? Something like this here - What's a good way to overwrite DateTime.Now during testing?, but for VBA.
Thus, I would need Now() to return a predefined value and not the current PC time.
What I do not want is something like this:
Option Explicit
Public Function NowDefined() As Date
'NowDefined = Now()
NowDefined = Format("2016-01-15 15:01:01", "yyyy-MM-dd hh:mm:ss")
End Function
and simply changing in the whole code Now() to NowDefined().
Rather than creating a String variable and then making Excel try to cast it to a Date, you would be better off generating the actual date to start with:
Function Now() As Date
Now = DateSerial(2016, 1, 15) + TimeSerial(15, 1, 2)
End Function
Vityata's answer works, but it is a bit fragile and can get cumbersome: you must remember to remove or comment out the entire function declaration when you're done testing; and put it back in whenever you need to do more testing... and then remember to remove it again when you release... etc.
I'd do something like this:
Const IN_TESTING_MODE As Boolean = True ' Set this to false when done testing
Function Now() As Date
If IN_TESTING_MODE Then
Now = ... 'whatever date you want
Else
Now = VBA.Now() ' back to default behavior
End If
End Function
Here you only have to change one thing to go in and out of testing mode: the value of the constant IN_TESTING_MODE. You can reuse this constant in other functions as well for which you want a different behavior while testing.
Actually this works:
Option Explicit
Public Function Now() As Date
Now = Format("2016-01-15 15:01:01", "yyyy-MM-dd hh:mm:ss")
End Function
I am getting an error in the below code.
Dim CurrentTime As Date
CurrentTime = Format(Now(), "hh:mm AM/PM")
If CurrentTime = ActiveSheet.Range("I1") Then
Call SendEMail
End If
When the time is right, then the macro is debugging and Now is highlighted.
Could anyone solve this problem?
You are not getting an actual error are you? It is just not working as expected.
Matt Cremeens has identified your issue I believe, you have declared CurrentTime as a date data type. A date data type stores a number representing a time/date, however you are asking for it to store string information (AM/PM) too which is can't so it is getting stripped out.
The results is cell one may hold a value like '10:30 AM' but the Format(Now(), "hh:mm AM/PM") code going into the Date variable is resulting in '10:30:00', so the two are never matching as strings. Copy as Matt has suggested and this should work (Dim CurrentTime As String).
Better yet, use the date comparison function DateDiff:-
If DateDiff("s",ActiveSheet.Range("I1"),Time()) > 0 then
SendEmail
End If
This is saying if the time now is greater than the value in I1 (to the second) then run SendEmail.
I don't have the environment to test the solution right now but from what I remember you don't need the brackets in 2007 and also you don't need the format.
Try the following code and see if that fits your need:
If Hour(Now) = ActiveSheet.Range("I1") Then
(...)
End If
I am trying to execute the following string as code using Evaluate but get Error 2029. Anyone know why?
Help much appreciated.
Calculation = "Format(""21/08/2012"", ""MMM"")"
Value = Evaluate(Calculation)
Try instead
Calculation = "TEXT(""21/08/2012"", ""MMM"")"
EVALUATE converts formulas to results, and FORMAT is a VBA function. The formula equivalent is TEXT.
You can also skip the evaluate and use the FORMAT function on the date directly.
You can use most worksheet functions in VBA directly with Application.WorksheetFunction. - for example - try this out:
Sub DateExample()
Dim StringTest As String
StringTest = Application.WorksheetFunction.Text("12/08/2012", "MMM")
Cells(1, 1).Value = StringTest
End Sub
Good Luck
I want to select some values through VBA in Pivot Table which is linked to OLAP Cube.
As I know such modification can be realised by typing:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = Array("value1","value2","value3")
Since get list of parameters from cells in Excel sheet, I wrote simple function which - In mentioned example - returns:
""value1","value2","value3""
I can't use such string as parameter for Array function (as it recognize it as one string), so I've tried to convert it to Array of Variant, typing above code:
Dim tableVar() As Variant
myVar = Replace(myVar, Chr(34), "")
myVar = Split(myVar, ",")
lowerB =LBound(myVar)
upperB = UBound(myVar)
ReDim tablica(lowerB To upperB)
For i = lowerB To upperB
tableVar(i) = myVar(i)
Next i
Unfortunately it changes nothing - when I'm calling:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = tableVar
I'm still receiving an error message.
Could you help me, please?
you have a typo in your code, daty should say myVar.
(Either that or we're missing more details)
Stupid thing, but error message is simply correct - there's no such items in Cube:
Run-time error '1004': The item could not be found in the OLAP Cube
I gave incorrect parameter here:
ActiveSheet.PivotTables("PivotTable1").PivotFields("[parameter].[parameter]").VisibleItemsList = tableVar
My code was unnecessary complicated - sorry for wasting your time.
Now my problem will be - how to check if specific dimensions or whole Cube exist...
Thanks once more for help.