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
Related
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 working on code that runs within a userform in Excel 2013. It is supposed to convert dates to string values to pass into text boxes within the form.
I have done this before and have never had any trouble with it. I am now getting a Runtime Error 13 "type mismatch" whenever I attempt to execute the following code:
Me.ListingDate.Value = Format(Now(), "mm/dd/yyyy")
The objective of this line is to format today's date and pass it to the textbox. I tried breaking it down into individual steps:
Dim MyDate As Date
Dim MyString As String
MyDate = Now()
MyString = Format(MyDate, "mm/dd/yy")
Me.ListingDate.Value = MyString
This still triggers a type mismatch error in the line where the Format() function is invoked on a date value.
I have run this code for over a year and never had any problems. There are several other forms in the file that use a similar syntax and it works.
Is it possible for a userform to become corrupted and falsely generate a runtime error? What other factors may be causing the error?
I have been using this piece of code on my PC running windows and Excel 2013 to return a date.
As shown in the code, the systems prompts for the use of current system date VS use of user-input date.
Function GetRegDate() As Date
Dim RegToday As String
RegToday = MsgBox("Are the transaction(s) of today (System time)?",vbYesNo)
If RegToday = vbYes Then
' Use Current System Date
GetRegDate = Date
Else
' Use user-input date
Dim RegYear, RegMonth, RegDay As Long
RegYear = InputBox("Please enter the year of the transaction.")
RegMonth = InputBox("Please enter the month of the transaction (in number).")
RegDay = InputBox("Please enter the day of the transaction.")
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
End If
' Some other codes
End Function
The piece of code has been consistently successful on my PC. But it isn't the case when my friend uses it on his Mac running Excel 2011.
Taking 26 Mar 2016 As an example, on my PC, by using the current system date or the user-input date method, the value "#26/3/2016#" is stored in the variable 'GetRegDate', as reflected from the local variable windows.
But as my friend told me, when using current system date, the value "##" is stored. When using the user-input date method, an error occurs at the line
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
with the runtime error 13 type mismatch
I have no idea why this happened. Any help is appreciated!
This is a known bug. From Microsoft Communities:
This is known bug with some of the region settings, working OK when
you use the Dutch region but not for example when you use the French
settings
Got one user that fix it by running this in the Terminal window
defaults write NSGlobalDomain AppleICUDateFormatStrings -dict 1
dd/MM/yy
I hope they fix it soon
That said, you shouldn't implicitly cast to a Date with GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear. Use DateSerial to avoid region issues:
GetRegDate = DateSerial(RegYear, RegMonth, RegDay)
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.
My vba code is to replace current time and date to 4_17_2014 8_09_00 PM format
But i am getting type mismatch error while running below VBA code
Function Current_Date_Time_Stamp()
Dim CurrTime As Date
CurrTime = Now
MsgBox Now
CurrTime = Replace(CurrTime, "-", "_")
MsgBox CurrTime
CurrTime = Replace(CurrTime, ":", "_")
Current_Date_Time_Stamp = CurrTime
End Function
Can anybody help me why i am getting error
As #Tim Williams mentioned in comments, Replace works only with string variables, but CurrTime is date (actually, date variables doesn't store format of date, but only date itself. Format when displaying date depends on regional settings and number format of your cell).
However you can use function with single line of code like this:
Function Current_Date_Time_Stamp()
Current_Date_Time_Stamp = Format(Now, "mm_dd_yyyy h_mm_ss AM/PM;#")
End Function