Tab delimited text file - formatting issue

Self_Taught

New Member
Joined
Nov 20, 2013
Messages
6
I'm attempting to output data from Excel into a tab delimited text file. I've used this macro successfully before but in this case the users have data that displays as a percentage in Column 4. e.g. value is 73.3%.

When I run this code against it the text file created displays the value as 0.733. How can I get this to output "73.3%"?

Here is my code:
Sub format_txt_col(Control As IRibbonControl)
'label columns
Cells(3, 1).Value = "Municode"
Cells(3, 3).Value = "LG_Custom1"
Cells(3, 4).Value = "LG_Custom2"
Cells(3, 5).Value = "LG_Custom3"
Cells(3, 6).Value = "LG_Custom4"
Cells(3, 7).Value = "LG_Custom5"

'save as tab delimetd text file
ImportFile = "H:\Import on " & Month(Date) & "-" & Day(Date) & _
"-" & Year(Date) & ".txt"
'deletes old copy
On Error Resume Next
Kill (ImportFile)
On Error GoTo 0
'Open new file
Open ImportFile For Output As #1
FinalRow = Range("A654536").End(xlUp).Row
'write to file
For j = 3 To FinalRow
Print #1, Cells(j, 1).Value & Chr(9) & Cells(j, 3).Value & Chr(9) & Cells(j, 4).Value & Chr(9) & Cells(j, 5).Value & Chr(9) & Cells(j, 6).Value & Chr(9) & Cells(j, 7).Value
Next j
'close workbook and notify user where file is located
MyMsg = "A file named Import on <today> has been created on your H drive. This workbook will now close."
response = MsgBox(MyMsg, vbOKOnly, "Closing Workbook")
Select Case response
Case Is = vbOK
ActiveWorkbook.Close (False)
End Select
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Thanks for the idea and I'm sure this could work but is there something more flexible than hard coding the format? I use this macro against various sets of data so it may not always be percentages. It could be text, numbers, dates, etc.

Hi
Welcome to the board

Try:

Code:
Format(Cells(j, 4).Value, "0.0%")
 
Upvote 0
If the source cell has the format you want in the result, use what is displayed instead of the value:

Code:
Cells(j, 4).Text
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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