I am trying to import special characters from a txt file into excel.
I've tried so many things but the characters BREAK in excel.
example of my string:
in txt: Changjíhuízúzìzhìzhou
converts in excel to: ChangjÃhuÃzúzìzhìzhou
so I tried moving values over bit by bit but no luck..
Sub ImportTXTFile()
Dim file As Variant
Dim EXT As String
Dim Direct As String ' directory...
Direct = "C:\FilePath\Here\"
EXT = ".txt"
Dim COL As Long
Dim row As Long
COL = 1
row = 1
file = Dir(Direct)
Do While (file <> "") ' Cycle through files until no more files
If InStr(file, "Data.txt") > 0 Then
'
Open Direct & "Data.txt" For Input As #1
'
While Not EOF(1)
Line Input #1, DataLine ' Read in line
Do While DataLine <> ""
If InStr(DataLine, ",") = 0 Then ' Drop value into excel upto the first ,
Sheets("test").Cells(row, COL).Value = DataLine
DataLine = ""
Else
Sheets("test").Cells(row, COL).Value = Left(DataLine, InStr(DataLine, ",") - 1)
DataLine = Right(DataLine, Len(DataLine) - InStr(DataLine, ",")) ' rebuild array without data upto first ,
End If
COL = COL + 1 ' next column
Loop
COL = 1 ' reset column
row = row + 1 ' write to next row
Wend
'
Close #1 ' Close files straight away
End If
file = Dir
Loop
MsgBox "Data Updated"
End Sub
So I want to cry because all this converting of UTF-8 to ASCII can be avoid simply by:
opening the txt file in Notepad++
going to the encoding tab
clicking convert to ASCII
ran my original code.
BLAM
everything is perfect.
Thank you danieltakeshi for all your help!
Using the first link i gave you, here is a test code, i tested with success. Using the charset: CdoISO_8859_1
Dim objStream As Object
Dim strData As String
Set objStream = CreateObject("ADODB.Stream")
objStream.Charset = "iso-8859-1"
objStream.Open
objStream.LoadFromFile ("C:\Users\user_name\Desktop\test.txt")
strData = objStream.ReadText()
Debug.Print strData & " Compare to: Changjíhuízúzìzhìzhou"
The output was:
EDIT:
Check the encoding type of your .txt file and import to Excel with the same encoding charset, for example, i changed the test.txt to UTF-8 and imported successfully with the .Charset as "utf-8"
You can Save As your .txt file and choose the encoding.
Related
Steps I need to do:
Open all necessary files(which are from the same folder) in Excel
For the first file, copy from row 6 to bottom of table. For second and subsequent files, copy from row 7 to bottom of table (Note that each file has different number of table rows). (Reasoning is that rows 1-5 are irrelevant, row 6 has heading, and I only want the heading to appear once in the table)
Paste into main excelsheet, but without overlapping previous rows
Separate main excelsheet by commas (text to column)
Close all files other than main excelsheet
Tried to google the various steps, but each step's code does not work well with one another, resulting in numerous errors, so I gave up and tried to record macro, but I did not get a "for" loop.
I've just tested the code below
Sub Read_Texts()
'Variable Declaration
Dim sFilePath As String
Dim sFileName As String
'Specify File Path
sFilePath = "C:\Users\use\Desktop\New folder"
'Check for back slash
If Right(sFilePath, 1) <> "\" Then
sFilePath = sFilePath & "\"
End If
sFileName = Dir(sFilePath & "*.txt")
Do While Len(sFileName) > 0
If Right(sFileName, 3) = "txt" Then
'Display file name in immediate window
Dim hf As Integer: hf = FreeFile
Dim lines() As String, i As Long
Open sFileName For Input As #hf
lines = Split(Input$(LOF(hf), #hf), vbNewLine)
Close #hf
If sFileName = "file1.txt" Then
For i = 5 To UBound(lines)
Debug.Print "File 1 Line"; i; "="; lines(i)
Next
Else
For i = 6 To UBound(lines)
Debug.Print "File 1 Line"; i; "="; lines(i)
Next
End If
End If
'Set the fileName to the next available file
sFileName = Dir
Loop
End Sub
Change C:\Users\use\Desktop\New folder according to your folder path, and here you can do whatever with the returned lines Debug.Print "File 1 Line"; i; "="; lines(i)
I need one value from multiple text files. Those text files are stored with a 5-digit filename in a folder(Around 1000 files) and I would like to create a macro, which scans this folder for a subset of files and then extract an individual Euro value.
I got the extraction part going, but I'm not able to loop this process through different file names yet as I'm fairly new to VBA.
Sub ExtractData()
Dim myFile As String, text As String, textline As String, Data As Integer, filename As String
Dim myFolder As String
myFolder = "C:\Folder\"
filename = Range("A1").Value & ".txt"
myFile = "C:\Folder\" & filename & ""
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
Data = InStr(text, "Euro")
Range("B1").Value = Mid(text, Data + 6, 4)
End Sub
I would highly appreciate it if someone would point me in the right direction.
Greetings
You may use Scripting.FileSystemObject to iterate the files in the target folder, use the Like operator to validate the file name and then get the value from each file as usual.
This should work:
Sub ExtractData()
Dim folderPath As String, filePath As String
Dim textline As String, data As Integer
folderPath = "C:\Folder\"
Dim oFso As Object: Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFolder As Object: Set oFolder = oFso.GetFolder(folderPath)
Dim oFiles As Object: Set oFiles = oFolder.Files
Dim oFile As Object
Dim counter As Integer
For Each oFile In oFiles
If Not oFile.Name Like "#####.txt" Then GoTo ContinueFor
data = 0
counter = counter + 1
Range("A" & counter).Value = oFile.Name
filePath = folderPath & oFile.Name
Open filePath For Input As #1
Do Until EOF(1) Or data > 0
Line Input #1, textline
data = InStr(textline, "Euro")
Loop
Close #1
If data > 0 Then Range("B" & counter).Value = Mid(textline, data + 6, 4)
ContinueFor:
Next
End Sub
This will extract the target value from the first line that contains the word "Euro". If the value that you're trying to extract is not in the same line, you can read the whole text (similar to what you did originally) and then extract the value you want:
Dim allText As String
' ...
' ...
Open filePath For Input As #1
allText = Input(LOF(1), 1)
Close #1
data = InStr(allText, "Euro")
If data > 0 Then Range("B" & counter).Value = Mid(allText, data + 6, 4)
There are probably better ways but it all depends on the structure of your file (which you haven't shown). For example, if the target value is in the next line and you know its position in that line, you could use the original code above to read the line that contains the word "Euro", read the next line, and then extract the value.
I'm very new to VBA in Excel. I'm using this code I cobbled together from example snippets online to convert a column of cells in Excel to a text file:
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim FName As String
Dim FPath As String
Set fsT = CreateObject("ADODB.Stream"): 'Create Stream object
fsT.Type = 2: 'Specify stream type – we want To save text/string data.
fsT.Charset = "utf-8": 'Specify charset For the source text data.
FPath = "C:\WHIT\ParamGen"
FName = Sheets("Sheet1").Range("b49").Text
myFile = FPath & "\" & FName
Set rng = Range("B2: B42 ")
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
The problem is that the first cell in my Excel file contains this text:
#!=1
...and it shows up in the generated text file like this:
?#!=1
Everything else in the excel file gets written to the text file without issue, but that question mark messes up the import function in the software this file is being generated for.
Any ideas on getting this question mark to disappear?
Have you tried removing the "?" with code in the file, such as:
If left(cellvalue,1)="?" then
application.substitute(cellvalue,"?","")
end if
I ran this code with the first cell containing #!=1 and it wrote correctly to the text file as #!=1 (no ? added). Did you check to see if cell B2 contains any non-printable characters?
I found a solution. Excel was treating the #!=1 in the first cell as a function, but it wasn't a functional function. My best guess is that it was throwing an invisible character in there as it parsed it into a text file. Overwriting the offending cell with '#!=1 did the trick.
I need to select a text file to import into Excel where the name of the text file contains a string of text that matches a cell in the Excel spreadsheet.
Eg.
A cell with a value "D12345"
I need to import a text file into the sheet where the same string (i.e. "D12345") is contained in the name of the text file.
The selection needs to be made from a collection of text files. Only 1 file in the collection will contain the matching string.
Hope that makes sense.
Give this a try:
Sub SimpleFileListre()
Dim s As String, FileName As String
Dim mesage As String
Range("A:A").Clear
s = "C:\TestFolder\*.txt"
sFolder = "C:\TestFolder\"
FileName = Dir(s)
Do Until FileName = ""
If InStr(1, FileName, "D12345") > 0 Then
Call GetStuff(sFolder & FileName)
End If
FileName = Dir()
Loop
End Sub
Sub GetStuff(s)
Close #2
Open s For Input As #2
j = 1
Do While Not EOF(2)
Line Input #2, TextLine
Cells(j, 1) = TextLine
j = j + 1
Loop
Close #2
End Sub
I've found an answer to import lines of data from numerous text files into an Excel sheet (https://stackoverflow.com/a/4941605/1892030 answered by Chris Neilsen). However I would like to also do the following:
There is garbage data before and after the useful data I want to import. The lines of data I want to import all start with an asterix (*).
The data is comma delimited and must be parsed that way when imported into Excel. This I could change by editing the parse code in the above answer.
At the end of each line that is imported, I want to add an additional item of data which is the name of the text file where the data was imported from (name of file only, without file extension).
The answer from Chris refered to above works real well so I would like to edit the code to allow for my additional requirements under points 1 and 3 above - but don't know how. For completeness I copy the code from the earlier answer below. Many thanks.
Sub ReadFilesIntoActiveSheet()
Dim fso As FileSystemObject
Dim folder As folder
Dim file As file
Dim FileText As TextStream
Dim TextLine As String
Dim Items() As String
Dim i As Long
Dim cl As Range
' Get a FileSystem object
Set fso = New FileSystemObject
' get the directory you want
Set folder = fso.GetFolder("C:\#test")
' set the starting point to write the data to
Set cl = ActiveSheet.Cells(1, 1)
' Loop thru all files in the folder
For Each file In folder.Files
' Open the file
Set FileText = file.OpenAsTextStream(ForReading)
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Parse the line into comma delimited pieces
Items = Split(TextLine, ",")
' Put data on one row in active sheet
For i = 0 To UBound(Items)
cl.Offset(0, i).Value = Items(i)
Next
' Move to next row
Set cl = cl.Offset(1, 0)
Loop
' Clean up
FileText.Close
Next file
Set FileText = Nothing
Set file = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
I haven't done it all for you (I expect the file name will need tidying up to fit the format you want) but drop this code in and it will get you started...
' Read the file one line at a time
Do While Not FileText.AtEndOfStream
TextLine = FileText.ReadLine
' Process lines which don't begin with Asterisk (*)
If Left(TextLine,1)<>"*" Then
' This crudely appends the filename as if it were a column in the source file
TextLine = TextLine + "," + file.Name
' Parse the line into comma delimited pieces
Items = Split(TextLine, ",")
' Put data on one row in active sheet
For i = 0 To UBound(Items)
cl.Offset(0, i).Value = Items(i)
Next
' Move to next row
Set cl = cl.Offset(1, 0)
End If
Loop