VBA & Macro Problems

Export Excel Cell Values in a Fixed Number Format with VBA

Muhammad TalhaMuhammad Talha Nov 22, 2026 868 views

Question details

Export Excel cell values to a text file in a specified decimal format for use by FORTRAN-based software.

Export Excel Cell Values in a Fixed Number Format with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the workbook, go to Developer > Visual Basic, and select Insert > Module.

2
Add the export macro

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

3
Adjust and run the macro

Change List1, the row limits, or the format string as needed. Run ExportFixedDecimals from Developer > Macros, choose a file name, and save it.

4
Check the output

Open the saved text file and confirm that each line has the required prefix and decimal notation, such as @ 2.00.

Use VBA Format to Export Fixed Decimal Places
Check the receiving format: Format can use a locale-dependent decimal separator. Verify that the file uses the separator expected by the FORTRAN-based program. Remove or change the @ prefix if the program does not require it.
Free Microsoft Office alternative

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.

Apply number formats to spreadsheet cellsOpen and edit compatible Microsoft Excel XLSX filesUse a familiar spreadsheet interfaceMove existing Office documents into a lightweight workspace
microsoft office alternative - wps office

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.