1

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

3
  • On which line are you getting the error? Commented May 8, 2014 at 4:31
  • 1
    Replace operates on Strings, and you're giving it a Date. Try looking at Format() if you want a specific representation of your Date value. Commented May 8, 2014 at 4:32
  • Tim Williams, I am getting error in 5th line Commented May 8, 2014 at 5:20

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.