How to Convert .csv into .txt for Matlab Import - vba

I have a .csv file. I want to convert it into .txt file starting from line the A to the B (A, B are declared at the beginning). The .txt file supposed to have gaps " " instead of original semicolons. Moreover, the end of each copied row from the original .csv file should be indicated by a semicolon in the new .txt file (txt file is going to be used as an input matrix for Matlab).
That's what I have till now, but it's not working
Sub csv2mat()
Dim Filename As String
Dim Filenamenew As String
'Change the path to the Files and and create a txt file
Filename = "C:\Documents and Settings\user\Desktop\as.csv"
Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
Open Filenamenew For Output As #2
Open Filenamme For Input As #1
Do While Not EOF(1)
Line Input #1, str
str = Replace(str, ";", """")
Print #2, str
Loop
End Sub

You hade a few spelling mistakes like Filnemme with to m.
And you forgot to close the Files after Reading.
And you didn't add the ";" ad the and of the string if i understand you right
Try this:
Sub csv2mat()
Dim Filename As String
Dim Filenamenew As String
Dim str As String
'Change the path to the Files and and create a txt file
Filename = "C:\Documents and Settings\user\Desktop\as.csv"
Filenamenew = "C:\Documents and Settings\user\Desktop\new.txt"
Open Filenamenew For Output As #2
Open Filename For Input As #1
Do While Not EOF(1)
Line Input #1, str
str = Replace(str, ";", """") & ";"
Print #2, str
Loop
Close #1
Close #2
End Sub

Related

How to read all information from shell command in VBA and save it in .txt file

I try to use this code:
Shell(nppPath & " " & fileToOpen, vbNormalFocus)
which opens txt file nppPath = "C:\Windows\notepad.exe" but I cannot read from it and save it as .txt file using VBA.
Looking forward to any suggestions.
I'm presuming you copied the nppPath over wrong as it should be
nppPath = "C:\Windows\System32\notepad.exe"
but for reading from the text file you could use free file:
Dim IntFile As Integer, StrFileName As String, StrFileContent As String
StrFileName = fileToOpen 'Your file location here
IntFile = FreeFile
Open StrFileName For Input As #IntFile
StrFileContent = Input(LOF(IntFile), IntFile) 'This now contains the text
Close #IntFile
This will read the text file into a single variable that you can then use however you wish.
If you want to write to a text file you can use the following code:
Dim StrNewLocation As String, StrFileContent As String
StrNewLocation = "" 'Put the new files name and location here
StrFileContent = "Example" 'The text to go into the new file
Open StrNewLocation For Output As #1
Write #1, StrFileContent
Close #1
Hopefully this helps you solve your problem if not send me a message and I'll see what I can do.

VBA: Replace text from txt for each line and save as

Could somebody help me with the following? I tried making it on my own, but all I could do is open a txt and replace a static word with static word.
VBA script:
Open and Read first line of ThisVbaPath.WordsToUse.txt
Open and Find USER_INPUT in ThisVbaPath.BaseDoc.docx (or txt)
Replace all occurrence of USER_INPUT with first line from WordsToUse.txt
Save BaseDoc.docx as BaseDoc&First_Line.docx
Close all
Go next line and do same, but don't ask for user input, use previous one
If error go next
When done show if there were any errors (unlikely I guess)
I would use it about weekly for 150-ish lines.
Thanks!
I think something like this should work?
Sub test()
Dim text, textReplace, findMe As String
findMe = InputBox("What Text To Find?")
Open "C:\Excel Scripts\test.txt" For Input As #1
Open "C:\Excel Scripts\test2.txt" For Input As #2
Open "C:\Excel Scripts\output.txt" For Output As #3
While Not EOF(1)
Line Input #1, text
While Not EOF(2)
Line Input #2, textReplace
Write #3, Replace(text, findMe, textReplace)
Wend
Wend
Close #1
Close #2
Close #3
End Sub
Sub TextFile_FindReplace()
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
FilePath = Application.ActiveWorkbook.Path & "\NEWTEST.txt"
TextFile = FreeFile
Open FilePath For Input As TextFile
FileContent = Input(LOF(TextFile), TextFile)
FileContent = Replace(FileContent, "FOO", "BAR")
Print #TextFile, FileContent
Close TextFile
End Sub

How to remove the empty line that excel makes when creating a csv file using vba

As some of you probably know, excel creates an empty line at the end of CSV files. I'm looking for a solution that can remove/delete this line because I want to upload the CSV file to a different program, which can't handle this empty line.
First I thought it was the way I created the CSV file, but after spending hours searching for a solution, I found out that it's a bug.
Does anybody have a solution to this problem, removing the last line in a CSV file using VBA?
Try calling this Sub to kill the last line of the csv-file. You have to insert the path into the code:
Sub KillLastLine()
Dim fso As New FileSystemObject
Dim ts As TextStream
Dim filecontent As String
Dim myFile As File
Set myFile = fso.GetFile("YourCSVPathHere")
Set ts = myFile.OpenAsTextStream(ForReading)
While Not ts.AtEndOfStream
filecontent = filecontent & ts.ReadLine & vbCrLf
Wend
Set ts = myFile.OpenAsTextStream(ForWriting)
ts.Write Left(filecontent, Len(filecontent) - 1)
ts.Close
End Sub
Sub ZUtil_TextFile_FindReplace(FilePath As String, strOld As String, strNew As String)
'PURPOSE: Modify Contents of a text file using Find/Replace
'SOURCE: www.TheSpreadsheetGuru.com
Dim TextFile As Integer
Dim FileContent As String
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Read State
Open FilePath For Input As TextFile
'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)
'Clost Text File
Close TextFile
'Find/Replace
FileContent = Replace(FileContent, strOld, strNew)
FileContent = Replace(FileContent, "^(?:[\t ]*(?:\r?\n|\r))+", "")
If Right(FileContent, 2) = Chr(13) & Chr(10) Then
FileContent = Left(FileContent, Len(FileContent) - 2)
End If
'Determine the next file number available for use by the FileOpen function
TextFile = FreeFile
'Open the text file in a Write State
Open FilePath For Output As TextFile
'Write New Text data to file
Print #TextFile, FileContent
'Close Text File
Close TextFile
'MsgBox "ZUtil_TextFile_FindReplace TERMINADO"
End Sub

reading rep file in vba

I'm trying to read a ".rep" file in VBA, here's the code I'm using:
FileNum = FreeFile()
Open FileName For Input As #FileNum
Line Input #FileNum, DataLine
LineItems = Split(DataLine, vbTab)
except that after the last code line, I have all of the file in "LineItems" (it becomes an array with 15K cells) and not just the first row (when I open the same file through Excel - I see it in the correct lines)
does anyone have an idea how to read it line-by-line?
Thank you
Try vbNewline instead?
LineItems = Split(DataLine, vbNewLine)
or this:
LineItems = Split(DataLine, vbCrLf)
Edit1: Ok try this then:
Dim LineItems() As String
Open Filename For Input As #Filenum
LineItems() = Split(Input$(LOF(Filenum), #Filenum), vbNewLine)
Close #Filenum

How to read a big file list from a textfile to form a query

I have this query which I have to run multiple times in excel and i need to change the filelists in it.
select * from files
where
filename in ('filename1','filename2')
so I have a TEMP in my query filename in TEMP and I want to loop and get the result for all filelists. My onlyproblem is reading .txt into the TEMP and executing the query once for all filenames in the txt file. i know how to read files line by line so that didn't help.
my text files which I want to read the lists from are like
filename1
filename2
.
.
.
.
filename15000
yes some big numbers.
dim filelist as string
dim filelistpath as string
sqlstring = "select count(*) from files where filename in TEMP"
filelistpath = "c:\"
open filelistpath for input as #1
do while not EOF(1)
line input #1,text
'here i should construct my file list to replace the TEMP below, how ?
loop
close #1
sqlstring = replace(sqlstring,TEMP, filelist)
set rs = conn.execute(sqlstring)
'here i want to write the result to my excel sheet, how ?
thanks
I formatted the text and just read it like that as a whole
Function GetText(sFile As String) As String
Dim nSourceFile As Integer, sText As String
''Close any open text files
Close
''Get the number of the next free text file
nSourceFile = FreeFile
''Write the entire file to sText
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
GetText = sText
End Function