0

I have two lines of code that seems extremely easy, but excel keeps giving me this Compile error telling me that object is required?

So basically I want to get the current time, and replace the spaces with underscores so I can use this string as the name of my log fime.

Dim name As String

'EXCEL GIVE ME compile error: object required
name = Replace(FormatDateTime(Now, DateFormat.LONGTIME), " ", "_")

What's wrong?!!

2 Answers 2

1

Replacing DateFormat.LONGTIME to 'vbLongTime' works for me.

name = Replace(FormatDateTime(Now, vbLongTime), " ", "_")
Sign up to request clarification or add additional context in comments.

Comments

1

You need to invoke Now as it is not a variable but a procedure

var now = str(Now());
name = Replace(FormatDateTime(now, DateFormat.LONGTIME), " ", "_")

should fix it

1 Comment

Your suggestion throws the same error as initial Q. See the answer of @SoulDZIN - it works.

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.