Solved

Export with VB/Macro from excel, unicode characters to text files

Posted on 2010-11-22
1
1,051 Views
Last Modified: 2012-05-10
Hi all,

I need to export the rows from the attached excel file in to multiple txt files. Each row will be exported to a different txt file with filename the last columns data.

I have written a VB code but it doesn't work with unicode data. I need to export Greek text in the file. Can anyone help by writing a new code or by tweaking mine?
Book1.xls

Sub TextFile()
Dim fs As Object, a As Object, i As Long, s As String, lngLastRow As Long
Set fs = CreateObject("Scripting.FileSystemObject")
lngLastRow = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To lngLastRow Step 1
  Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True)

  s = s & Cells(i, 1) & vbCrLf & Cells(i, 2) & vbCrLf & Cells(i, 3) & vbCrLf & Cells(i, 4) & vbCrLf & Cells(i, 5) & vbCrLf & Cells(i, 6) & vbCrLf & Cells(i, 7) & vbCrLf & Cells(i, 8) & vbCrLf & Cells(i, 9)


  a.WriteLine s
  s = ""
  a.Close
Next i
End Sub

Open in new window

0
Comment
Question by:gkarydis
1 Comment
 
LVL 14

Accepted Solution

by:
canali earned 125 total points
ID: 34192850

object.CreateTextFile(filename[, overwrite[, unicode]])
You need to tell that your file is unicode if the third param is omited the file is ASCII.

http://msdn.microsoft.com/en-us/library/5t9b5c0c(VS.85).aspx

Sub TextFile()
Const Unicode = -1
...
...
Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True, Unicode)
...
...
end sub
Sub TextFile()
Const Unicode = -1


Dim fs As Object, a As Object, i As Long, s As String, lngLastRow As Long
Set fs = CreateObject("Scripting.FileSystemObject")
lngLastRow = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To lngLastRow Step 1
  Set a = fs.CreateTextFile("C:\Temp\" & Range("G" & i).Value & ".txt", True, Unicode)

  s = s & Cells(i, 1) & vbCrLf & Cells(i, 2) & vbCrLf & Cells(i, 3) & vbCrLf & Cells(i, 4) & vbCrLf & Cells(i, 5) & vbCrLf & Cells(i, 6) & vbCrLf & Cells(i, 7) & vbCrLf & Cells(i, 8) & vbCrLf & Cells(i, 9)


  a.WriteLine s
  s = ""
  a.Close
Next i
End Sub

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question