export to text file from excel at fixed positions

Arafat

Board Regular
Joined
May 3, 2011
Messages
61
Hello,

I have got a code from one of the threads on Mr.Excel, but that code fails when it encounters a blank cell in a row and it add that value in the wrong position in the text file. What i want is even if a blank cell is found ,the next cell value should be at the correct specified position.

here is the code snippet.

Sub exporttext()
Dim i As Long
Dim fso As FileSystemObject
Dim hope As TextStream

Set fso = New FileSystemObject
Set hope = fso.CreateTextFile("C:\Users\Arafat\Desktop\os\test.txt", True)
With hope
i = 1
While Cells(i, 1).Value <> ""
.WriteLine Format(Cells(i, 1), "!" & String(10, "@")) & Format(Cells(i, 2), String(20, "@")) _
& Format(Cells(i, 3), "!" & String(5, "@"))
i = i + 1
Wend
.Close
End With

Dim x
x = Shell("notepad.exe C:\Users\Arafat\Desktop\os\test.txt", 1)
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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