Error formatting with type "General"

Ecrivaine

New Member
Joined
Dec 24, 2016
Messages
2
Hi,

I ran into an unexpected behaviour of the Format function in Excel-vba.
I want to read the content of a cell into a string parameter exactly in the way the user sees this content in his worksheet. To do this, I use the Format function and the cells NumberFormat. If the cell contains a number and the cell format is "General", the Format function returns "Ge0eral" i.s.o the number. So when a number is present in cell A1 and the format of cell A1 is set to "General" the function:
Format(Range("A1").Value, Range("A1".NumberFormat)
returns: Ge0eral.

The same occurs when typing: Format(2016,"General") in the Direct window of the Visual Basic editor.
This problem occurs on two different computers using different releases of Excel.

Can anybody explain this behaviour and/or knows a solution?

Thanks.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

I ran into an unexpected behaviour of the Format function in Excel-vba.
I want to read the content of a cell into a string parameter exactly in the way the user sees this content in his worksheet. To do this, I use the Format function and the cells NumberFormat. If the cell contains a number and the cell format is "General", the Format function returns "Ge0eral" i.s.o the number. So when a number is present in cell A1 and the format of cell A1 is set to "General" the function:
Format(Range("A1").Value, Range("A1".NumberFormat)
returns: Ge0eral.

The same occurs when typing: Format(2016,"General") in the Direct window of the Visual Basic editor.
This problem occurs on two different computers using different releases of Excel.
The VB Format function is not related in anyway to the worksheet, so it does not respond to cell formatting Type patterns. You should call out to the worksheet's TEXT function instead...

StringVariable = Application.Text(Range("A1").Value, Range("A1").NumberFormat)
 
Upvote 0
The VB Format function is not related in anyway to the worksheet, so it does not respond to cell formatting Type patterns. You should call out to the worksheet's TEXT function instead...

StringVariable = Application.Text(Range("A1").Value, Range("A1").NumberFormat)

Hi Rick,

Indeed that is the solution. Fantastic.
Thanks for your fast response! I really appreciate it. It kept me puzzled for quite a while but never thought of this reason.
Hope you have a very nice Chrismas.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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