Export Excel Cell Values in a Fixed Number Format with VBA
Question details
Export Excel cell values to a text file in a specified decimal format for use by FORTRAN-based software.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A VBA macro needs to write worksheet values as text in a numeric notation accepted by another program.
- Observed behavior
- The desired output uses decimal notation, such as 2.00, rather than relying on an uncontrolled representation of the cell value.
Identify the worksheet and cell range to export, and check the receiving program's requirements for decimal places, decimal separators, and any line prefix. Enable macros only if you trust the workbook and its code.
Use VBA Format to Export Fixed Decimal Places
Use the Format function when the text file must have a consistent number of decimal places regardless of worksheet formatting.
In Excel, open the workbook, go to Developer > Visual Basic, and select Insert > Module.
Paste this example into the module. It exports cells A1:A10 on the List1 worksheet with two decimal places: Sub ExportFixedDecimals() Dim outputPath As Variant Dim fileNum As Integer Dim i As Long outputPath = Application.GetSaveAsFilename("values.txt", "Text Files (*.txt), *.txt") If VarType(outputPath) = vbBoolean Then Exit Sub fileNum = FreeFile Open CStr(outputPath) For Output As #fileNum For i = 1 To 10 Print #fileNum, "@ " & Format(Worksheets("List1").Cells(i, 1).Value, "0.00") Next i Close #fileNum End Sub
Change List1, the row limits, or the format string as needed. Run ExportFixedDecimals from Developer > Macros, choose a file name, and save it.
Open the saved text file and confirm that each line has the required prefix and decimal notation, such as @ 2.00.

Export the Cell's Displayed Value with the Text Property
Use Text when the output should match the number formatting already applied to the worksheet cells.
Work with Formatted Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative for working with spreadsheets through a familiar interface. Open compatible Excel files and continue editing with minimal disruption; Microsoft Excel VBA behavior is not guaranteed.

Frequently Asked Questions
What is the difference between Value and Text in Excel VBA?
Value reads the underlying cell value, while Text reads its displayed representation. Use Cells(i, 1).Text to export the worksheet's displayed number format.
How do I export three decimal places instead of two?
In the VBA Print statement, change Format(cell.Value, "0.00") to Format(cell.Value, "0.000"). Check the resulting text file for the expected decimal separator.
Why does my exported value contain hash marks?
When using the Text property, a column that is too narrow can display hash marks. Widen the column in Excel and rerun the export macro.
Can I remove the @ at the start of each line?
Yes. If the receiving program does not require it, remove "@ " & from the Print statement and export only the formatted value.




