Solved

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

Posted on 2010-11-22
1
1,031 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvieā€¦
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now