Number in decimal Format in text file generate from Excel VBA

mamun_ges

Board Regular
Joined
Jul 21, 2016
Messages
52
Hi, I have searched a lot but find no answer. Hope someone helps me in this context.
I am using a vba code to create a text file from an excel sheet. The code works fine. But there is a number format in decimal in excel which converts as a number without a decimal place in the text file. below is the code I used.
VBA Code:
 Dim UsedRows As Long
 Dim UsedColumns As Long
 Dim R As Long, C As Long
 Dim Data    As Variant
  
 '// Define a suitable file name
 Open "E:\Folder\" & "TD.txt" For Output As #1
 With ActiveSheet
 UsedRows = .UsedRange.Rows.Count
 UsedColumns = .UsedRange.Columns.Count
 For R = 2 To UsedRows
 For C = 1 To UsedColumns
 Print #1, .Cells(R, C); "|";
                Set Cell = .Cells(R, C)
                    If IsNumeric(Cell) Then
                        Data = Data & Cell.NumberFormat = "#,##0.00"
                    Else
                        Data = Data & vbTab & Cell.Value
                    End If
 Next C
 Print #1, .Cells(R, UsedColumns)
 Next R
 End With
Hope someone helps me to overcome my difficulties and tell me what to change or add to the code.

Thanks in advance
 
Glad you were able to figure out what changes to make to get it to work on your data.
Thanks for letting me know. Happy I could help.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi, I am using the below code.
VBA Code:
Sub ExportTxt()

    Dim UsedRows As Long
    Dim UsedColumns As Long
    Dim R As Long, C As Long
   
    '// Define a suitable file name
    Open "E:\Folder\" & "TD.txt" For Output As #1
    With ActiveSheet
        UsedRows = .UsedRange.Rows.Count
        UsedColumns = .UsedRange.Columns.Count
        For R = 2 To UsedRows
            For C = 1 To UsedColumns
                Select Case C
                    Case 2              ' Numeric Column with 2 decimals
                        Print #1, Format(.Cells(R, C), "#.00"); "|";
                   
                    Case UsedColumns    ' Last Column end of line
                        Print #1, .Cells(R, C)
                   
                    Case Else
                        Print #1, .Cells(R, C); "|";
                   
                End Select
            Next C
        Next R
    End With

    Close #1

End Sub


The code creates the text file in a pipe-delimited format. Now the fact is I need to create the text file where pipe "|" will be after the last text.
I mean Like the below line (As an example)

20157|BISWA MANZIL|FADPUR| 50 |51500.00||22/09/2021|Cost|

What to change in the code to achieve the expected result.
Any suggestion?

Thanks and Best regards.
 
Last edited:
Upvote 0
As this is a significantly different question, you need to start a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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