In this post we are going to see how to export data from excel to text file using VBA.
Below is the VBA Code.
Sub ExceltoText()
'Declaring the variables
Dim FileName, sLine, Deliminator As String
Dim LastCol, LastRow, FileNumber As Integer
'Excel Location and File Name
FileName = Thisworkbook.path & "\ExceltoText.txt"
'Field Separator
Deliminator = "|"
'Identifying the Last Cell
LastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
FileNumber = FreeFile
'Creating or Overwrighting a text file
Open FileName For Output As FileNumber
'Reading the data from Excel using For Loop
For i = 1 To LastRow
For j = 1 To LastCol
'Removing Deliminator if it is wrighting the last column
If j = LastCol Then
sLine = sLine & Cells(i, j).Value
Else
sLine = sLine & Cells(i, j).Value & Deliminator
End If
Next j
'Wrighting data into text file
Print #FileNumber, sLine
sLine = ""
Next i
'Closing the Text File
Close #FileNumber
'Generating message to display
MsgBox "Text file has been generated"
End Sub
See it in action.
Below is the VBA Code.
Sub ExceltoText()
'Declaring the variables
Dim FileName, sLine, Deliminator As String
Dim LastCol, LastRow, FileNumber As Integer
'Excel Location and File Name
FileName = Thisworkbook.path & "\ExceltoText.txt"
'Field Separator
Deliminator = "|"
'Identifying the Last Cell
LastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
FileNumber = FreeFile
'Creating or Overwrighting a text file
Open FileName For Output As FileNumber
'Reading the data from Excel using For Loop
For i = 1 To LastRow
For j = 1 To LastCol
'Removing Deliminator if it is wrighting the last column
If j = LastCol Then
sLine = sLine & Cells(i, j).Value
Else
sLine = sLine & Cells(i, j).Value & Deliminator
End If
Next j
'Wrighting data into text file
Print #FileNumber, sLine
sLine = ""
Next i
'Closing the Text File
Close #FileNumber
'Generating message to display
MsgBox "Text file has been generated"
End Sub
See it in action.
Is it possible to keep the text file open after it has been generated? Thanks
ReplyDeleteplz do another one with comma delimited. Thanks
ReplyDeletePlz disregard my previous request as I have tried it with the comma and it worked perfectly.
ReplyDeleteThanks.
Thank you for the useful video, pl tell me how to insert "Tab" as the deliminator instead of the"|" used in this video
ReplyDeletecan i get the excel file for the same
ReplyDeletePLEASE MUKHILAJI@GMAIL.COM
DeletePerfecto :)
ReplyDeleteThank you for this working solution. Any idea how to do the same thing coding the text file un UTF-8 format (this is needed when I have accentuated characters, like é ö ü and so on..
ReplyDeletecan you help me, VBA code Import
ReplyDeleteThanks,
Hi, this code is exactly what I need. Just One question, if I have a lot of blank lines, is there a way to skip the blanks and only export the lines that aren't blank?
ReplyDeleteThx in advance!
Kenny
Thank you brilliant
ReplyDelete