Export range to text file

M

Matthew Scheperle

I need to export a range from excel to a text file with a delimiter such as a
comma or semicolon. I want to be able to have some kind of macro that will
check each row and if a row is blank then it will stop there and only export
the rows that has data. Which is why I was thinking of using a range, or
array.

I do have some formulas in the cells so do not want that to show up in the
export.

Thanks,
 
A

Alejandro Medinilla elMedex

I have this code that I use to create a csv for later update some values in a
system I hope it works

Dim ColId As Integer
Dim ColIn As Integer
Dim ColDur As Integer
Dim Colseg As Integer

Sub Initialize()
ColId = 1
ColIn = 2
ColDur = 3
Colseg = 4
End Sub

Sub MakeFile()
Dim CVSFile As String
Dim NumFile As Integer
Dim Fecha As Date
Dim Reng As Long
Dim Cadena As String
Initialize
NumFile = FreeFile
Open ActiveWorkbook.Path & "\Myfile.txt" For Output As #NumFile
Reng = 2
While Trim(ThisWorkbook.Worksheets(1).Cells(Reng, ColId).Value) <> ""
Cadena = CreateString(Reng)
Print #NumFile, Cadena
Reng = Reng + 1
Wend
Cadena = ""
Print #NumFile, Cadena
Close #NumFile
End Sub

Function CreateString(R As Long) As String
Dim Cad As String
Cad = "00,"
Cad = Cad & Trim(Trim(ThisWorkbook.Worksheets(1).Cells(R, ColId).Value))
& "," 'COLID
Cad = Cad & Trim(Trim(ThisWorkbook.Worksheets(1).Cells(R,
Colseg).Value)) & "," 'dURATION
Cad = Cad & Format(ThisWorkbook.Worksheets(1).Cells(R, ColIn).Value,
"hh:mm") & ","
Cad = Cad & Format(ThisWorkbook.Worksheets(1).Cells(R, ColDur).Value,
"hh:mm") & ","
CreaLATE = Cad
End Function


Regards.....

elMedex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top