printing txt file from VBA for Matlab - excel

I am working parallel with Excel and VBA in order to create txt files I wish to use for MATLAB. However, I experience some format issues I can't resolve.
For instance, the following VBA
Open "example.txt" For Output As #1
For i = 1 To 5
Print #1, Sheets("Example").Cells(i + 3, 3)
Next i
Indeed prints numbers (reals) it is supposed to however MATLAB struggles with reading this example .txt file.
There are some characters VBA is printing. I don't know how to delete those within a VBA code.
Example.txt opened in matlab. Note the NaN read by MATLAB from a text file:
VBA text file - Note a line as the first element of a column

Perhaps there is a character that is invisible.
A possible solution is to remove those characters with regex.
Add reference to Microsoft VBScript Regular Expression 5.5
Then the following VBA code:
Set re = New RegExp
re.Pattern = "[^0-9]"
Open "example.txt" For Output As #1
For i = 1 To 5
Print #1, re.Replace(Sheets("Example").Cells(i + 3, 3).value, vbNullString)
Next i
This should remove anything that is not a digit from the cell before printing it to the text document.

Related

Split function in Excel VBA returning odd characters

I have a text file containing danish characters
"Næsby IF afdeling * Badminton * Sport *"
When splitting and placing them in an array the danish characters gets "messed up"
This is the complete Text string in a *.TXT file to be split up in Excel columns:
"Ulrich*wiingreen*BenPauWin05 Aps*Søballehøjen 12*5270*Odense N*+4530212215*ulrich#wiingreen.eu*Næsby IF afdeling*Badminton*Sport* *Hal 1*Hal 2*99*11/03/2022 13:00*11/03/2022 17:00*kkkk"
The code doing this is:
If InStr(FileName, "forespoerg_") <> 0 Then
OrderArr = Split(OrderDetails, "*")
OrderRow = OrdersDB.Range("A99999").End(xlUp).Row + 1
OrdersDB.Cells(OrderRow, 1).Value = Application.WorksheetFunction.Max(Range("A4:A9999")) + 1
OrdersDB.Cells(OrderRow, 2).Value = Date
For OrderCol = 3 To 20
OrdersDB.Cells(OrderRow, OrderCol).Value = OrderArr(OrderCol - 3)
Next OrderCol
End If
The splitting works just fine. Unfortiunately the characters gets messed up.
Example: "Søballehøjen 12" imports as: "Søballehøjen 12"
Can anyone give a hint to solve this character issue.
Not sure but I suspect encoding mismatch. You can try opening your text file with VS Code and watch at the bottom right what is its encoding.
You can then use StrConv(yourTextVar, someconversion) wher someconversion is a value like vbUnicode or vbFromUnicode (see options here)
How do you import the text file ? If your file is a unix or another non Windows flavour you could try reading the file using an ADODB.Stream, which offers fine control on the encoding. I quickly found a sample here.

read first 1000 characters of text file in VBA

Am using the following command to read the first 1000 characters of a huge txt file.
text = Input$(1000, 1)
problem is when I want to display a character in a textbox, it displays a newline character at the end (a reverseP) which I dont want.
The other option i have is to read the first 1000 characters using a Do-Until loop like the following. However, could not find a command to replace EOF(1).
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
can somebody provide any help with this ?

Removing unwanted data from text file

I have a large text file exported from an application that has three unwanted zeros in each row. The text file needs to be imported into another application and the zeros cause a problem.
Basically the unwanted three zeros per row need to be deleted. These zeros are always in the same location (same number of characters when counting from the left), but the location is somewhere in the middle. I have tried various things like importing the file into excel, removing the zeroes and then exporting as text file, but always have formatting problems with the exported text file.
Can someone suggest a solution or point me in the right direction?
something like this ? (quickly done)
Sub replaceInTx()
Dim inFile As String, outFile As String
Dim curLine As String
inFile = "x:\Documents\test.txt"
outFile = inFile & ".new.txt"
Open inFile For Input As #1
Open outFile For Output As #2
Do Until EOF(1)
Line Input #1, curLine
Print #2, Replace(curLine, "000", "", 6, 1, vbTextCompare)
Loop
Close #1
Close #2
End Sub
Alternatively, you can do that with any text editor that allows block selection (I like Notepad2, tiny, fast and portable)
I see you use excel a lot.
When you import the text file into excel do you use the import function and do you push the data into separate cells?
if the cell is numeric you could do the following:
=LEFT(TEXT(G5,"#"),LEN(TEXT(G5,"#"))-3)
if the cell is text:
=LEFT(G5,LEN(G5)-3)
G5 would the cell the data row/field is in.
curLine = Left(curLine, 104)
This will take the first 104 characters

Count commas in line of text file

I have a text file and I'm trying to count the number of commas in the first line of the file using excel VB and then do an action if there are 3 - but something is wrong. When I use the replace method (commented out in this example below) the macro fails, and when I use the Split method it ALWAYS does the action, no matter what value I add in place of 3.
'Load txt file into array
Open FilePath For Input As #1
dataArray = Split(Input$(LOF(1), #1), vbLf)
Close #1
'Test first line if it has three commas
If Len(dataArray(0).value) - Len(Replace(dataArray(0).value, ",", "")) = 3 Then
'If dataArray(0).Split(",").Length = 3 Then
'Add comma to start of strings
For i = LBound(dataArray) To UBound(dataArray)
dataArray(i) = "," & dataArray(i)
Next i
The .value appears to be the problem. VBA doesn't have properties the same way .NET does.
Replace your uncommented out IF statement with this:
If Len(dataArray(0)) - Len(Replace(dataArray(0), ",", "")) = 3 Then

Excel VbScript Quotes issue

I am using Vbscript to write some data into excel then i am saving this excel object as txt file. My problem here is after saving as text file some rows contains quotes ". Below is my code can some body help me recording this.
My Output text file is:
"Rules*V*ZBEA*892**0010*10*IBM-01**"
30,000.00*01/08/2012*21/08/2012****0000013556*01***2600
"Scale value* *********"
problem here is 1st and 3rd row starts and ends with quotes (" ").
code is
Dim objXL1,name
Set objXL1 = CreateObject("Excel.Application")
objXL1.Workbooks.Add
objXL1.Cells(1 ,1) = "Rules*V*ZBEA*892**0010*10*IBM-01** "
objXL1.Cells(2,1) = "30,000.00*01/08/2012*21/08/2012****0000013556*01***2600"
objXL1.Cells(3 ,1) = "Scale value* *********"
name = objXL1.GetSaveAsFilename(,"Text(MS-DOS)(*.txt),*.txt")
objXL1.ActiveWorkbook.SaveAs name ,21 ,,21
objXL1.ActiveWorkbook.Close 0
objXL1.quit
And here once more issue is I am using SaveAs method for getting file name. When execution comes to this line the file save dialog box hiding behind the main IE window is there any way to get this save dialog box in focus?
I assume this is because of the whitespace. In your code there is a trailing whitespace in the frist line.
"Rules*V*ZBEA*892**0010*10*IBM-01** "
-----------------------------------^-
If you would export multiple cells you would need to encapsualte those cells to recognize where a cell value begins and ends.

Resources