Why is my macro truncating my output??

sajjaddaya

New Member
Joined
Oct 26, 2011
Messages
14
Hi Guys,

I have a macro below that outputs a text file that is pipe delimited, and has quotation marks around the output - this part is perfect!

The problem I am having is that the output in each cell is truncated after a certain point. I have some cells that have GIGANTIC amounts of data in them - how do I modify this macro to output everything in the cell without any truncating?

You're help will be VERY appreciated!

Code:
    Public Sub OutputQuotedCSV()
        Const QSTR As String = """"
        Const DELIMITER As String = "|"
        Dim myRecord As Range
        Dim myField As Range
        Dim nFileNum As Long
        Dim sOut As String
        
        nFileNum = FreeFile
        Open "File1.txt" For Output As #nFileNum
        For Each myRecord In Range("A1:A" & _
                    Range("A" & Rows.Count).End(xlUp).Row)
            With myRecord
                For Each myField In Range(.Cells(1), _
                            Cells(.Row, 256).End(xlToLeft))
                    sOut = sOut & DELIMITER & QSTR & _
                        Replace(myField.Text, QSTR, QSTR & QSTR) & QSTR
                Next myField
                Print #nFileNum, Mid(sOut, 2)
                sOut = Empty
            End With
        Next myRecord
        Close #nFileNum
    End Sub

Thanks
Sajjad
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The Text property of a cell only returns the first 1024 characters. If that's what's causing the problem, use the Value2 property instead.
 
Upvote 0
Thanks - that certainly worked for the truncated output, and fixed that particular issue.

I am having another problem. We were only test 4 records, and those 4 records are now exporting correctly. When we try and export the full file - about 5000 records, it looks as though the macro is running for about a minute, and then it ends - as you would expect. However, no file is generated.

Any idea why the macro would not work for 5000 records but would work for 4 records?
 
Upvote 0
No idea.

It creates the file in the current directory, wherever that is. Have you looked around?
 
Upvote 0
Yeah, I've run a search on the whole computer - no file outputted called file1.txt. Usually it drops it into the My Docs folder, but when there are 5000 records, no file outputted.

Bizzare!
 
Upvote 0
What if you write 5 lines? 50? 500?
 
Upvote 0
I copied the data to another sheet and tried to export - and it worked!

Not sure what the problem was?

Thanks for all your help, it is greatly appreciated!
 
Upvote 0
Good job, glad you got it sorted.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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