Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Range to Delimited Text File

Status
Not open for further replies.

JohnJoker

Technical User
Oct 19, 2010
16
GB
I am trying to export an Excel file to delimited text file. The code below works fine for one range. I would like to include others ranges. How can I do that.

CODE:

For example I want to export also B44:F44, B48:F48, H56 ...etc

Sub CallExport()
'ExportRange(range,where,delimiter)

Call ExportRange(Sheet1.Range("B26:D26"), _
"C:\Documents and Settings\Desktop\mark.txt", ",")
End Sub

Function ExportRange(WhatRange As Range, _
Where As String, Delimiter As String) As String

Dim HoldRow As Long 'test for new row variable
HoldRow = WhatRange.Row

Dim c As Range 'loop through range variable

For Each c In WhatRange
If HoldRow <> c.Row Then
'add linebreak and remove extra delimeter
ExportRange = Left(ExportRange, Len(ExportRange) - 1) _
& vbCrLf & c.Text & Delimiter
HoldRow = c.Row
Else
ExportRange = ExportRange & c.Text & Delimiter
End If
Next c

'Trim extra delimiter
ExportRange = Left(ExportRange, Len(ExportRange) - 1)

'Kill the file if it already exists
If Len(Dir(Where)) > 0 Then
Kill Where
End If

Open Where For Append As #1 'write the new file
Print #1, ExportRange
Close #1

End Function


 



Hi,

This forum is for MS Access VBA questions.

Excel VBA is addressed in forum707.

However your question does not need a VBA solution. Simply SaveAs a delimited file. No VBA required.


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top