"Type mismatch" has me stumped...

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
485
Office Version
  1. 365
Platform
  1. Windows
Good morning! I'm trying to figure out why this seemingly simple code gives me a "Type mismatch" error.

This works:

VBA Code:
Range("Load!G1").Value = "'" & [Text(Now(), "M/D" & " at " & "H:MM AM/PM")]

This does NOT:

VBA Code:
Range("Load!G1").Value = "'" & [Text(Now(), "H:MM AM/PM" & " on " & "M/D")]

What am I doing wrong?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try it like
VBA Code:
Range("Load!G1").Value = "'" & [Text(Now(), "H:MM AM/PM" & " o\n " & "M/D")]
 
Upvote 1
Solution
If you put the second formula in a cell, it will return #VALUE!. I have not dug down to understand why.

I prefer to use built-in VBA functions rather than evaluating a worksheet formula. Also there is no reason to prefix with "'" because you are creating a text string anyway.

This will work:

VBA Code:
Range("Load!G1"").Value = Format(Now, "h:mm AM/PM") & " on " & Format(Date, "m/d")
 
Upvote 0
I'm guessing that the "n" has a special meaning to the text function & so it needs to be escaped using the "\"
 
Upvote 0
After seeing Fluff's answer, I streamlined my version. The same format strings apply to the Format function.

VBA Code:
Range("Load!G1"").Value = Format(Now, "h:mm AM/PM o\n m/d")
 
Upvote 0
I'm guessing that the "n" has a special meaning to the text function & so it needs to be escaped using the "\"
In certain programs, I have seen them use "n" to present "months", as they already use "m" for minutes.
 
Upvote 0
In both VBA Format and the worksheet TEXT function, Excel parses the string for context. It recognizes if "m" is used as part of a time expression, and if so uses minutes. Otherwise it treats it as month. I cannot find any documentation for custom formatting or the TEXT function that mentions "n" as a format code.

Using "n" in a format string for TEXT results in a #VALUE! error. However, using it in the VBA function Format it means "minutes".
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top