VBA to Export WorkSheet without SAVE AS as it is(Same content and format) to text file.

mktmohit

New Member
Joined
Mar 9, 2013
Messages
15
Hi,

I wanted to export worksheet content to text file, with save AS it is working perfectly, but it is convertin to .txt file and if i again run the macro it is throwing error "Subscript out of range" as it will not be able to find the Desired sheet.

I thr any vba code which will export the sheet content as it is.

Sheet content Sample
==============

diskLayout=[
{ part=/boot, size=512 }
{ part=/, size=16384 }
{ part=/tmp, size=20480 }
{ part=/var, size=3072 }
{ part=/usr, size=20480 }
{ part=/opt, size=10240 }


Code which i am using with SAVE AS
=======================
Dim tmpFile As String
Dim MyData As String, strData() As String
Dim entireline As String
Dim filesize As Integer
tmpFile = TempPath & Format(Now, "ddmmyyyyhhmmss") & ".txt"
Sheet2.Select


ActiveWorkbook.SaveAs Filename:=tmpFile _
, FileFormat:=xlText, CreateBackup:=False


'~~> Read the entire file in 1 Go!
Open tmpFile For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)


'~~> Get a free file handle
filesize = FreeFile()


'~~> Open your file
Open FlName For Output As #filesize


For i = LBound(strData) To UBound(strData)
entireline = Replace(strData(i), """", "")
'~~> Export Text
Print #filesize, entireline
Next i

Close #1






Thank you in advance
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,216,018
Messages
6,128,303
Members
449,439
Latest member
laurenwydo

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