Extract Data from Text File into Excel - vba

I am new to VBA so getting my task done is quite a struggle. Been reading and trying codes from different threads for a few days now to no success. So I am hoping someone could assist me.
I have multiple text files that I need to extract data from. But I only need certain data such as DATE-TIME to be placed in the 1st column and CARD NUMBER in the 2nd column. Got codes from this thread >> Extract a single line of data from numerous text files and import into Excel but my output only shows the first data from the file. Please see the attached files below.
sample text
Output
Desired Output
Here's what I have:
Sub ExtractData()
Dim filename As String, nextrow As Long, MyFolder As String
Dim MyFile As String, text As String, textline As String, filedate As String
Dim filenum As Integer
MyFolder = "C:\directory\"
MyFile = Dir(MyFolder & "*.txt")
Do While MyFile <> ""
Open (MyFolder & MyFile) For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
MyFile = Dir()
Debug.Print text
filedate = InStr(text, "DATE-TIME")
nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
ActiveSheet.Cells(nextrow, "A").value = Mid(text, filedate + 16, 17)
filenum = InStr(text, "CARD NUMBER")
nextrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1
ActiveSheet.Cells(nextrow, "B").value = Mid(text, filenum + 16, 10)
text = ""
Loop
End Sub

I modify the code for you, it can work:
Sub ExtractData()
Dim filename As String, nextrow As Long, MyFolder As String
Dim MyFile As String, text As String, textline As String, filedate As String
Dim filenum As Integer
dim idx%
MyFolder = "C:\directory\"
MyFile = Dir(MyFolder & "*.txt")
nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do While MyFile <> ""
Open (MyFolder & MyFile) For Input As #1
'nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do Until EOF(1)
Line Input #1, textline 'read a line
idx = InStr(textline, "DATE-TIME") ' if has date, set it but not move to the next ROW
if idx > 0 then
ActiveSheet.Cells(nextrow, "A").value = Mid(textline, idx + 16)
end if
idx = InStr(textline, "CARD NUMBER")
if idx > 0 then
ActiveSheet.Cells(nextrow, "B").value = Mid(textline, filenum + 16)
nextrow = nextrow + 1 'now move to next row
end if
Loop
Close #1
MyFile = Dir()
Loop
End Sub

Related

How to write to a file using comma delimiter and no double quote in string?

I would like to print a range of date into .txt file with some special header so that a special software called Prophet could recognize.
Sub Export_TEST()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim prophetheader As String
Dim prophetlineheader As String
myFile = Application.DefaultFilePath & "\" & "test" & ".txt"
Set rng = Selection
prophetheader = "!1"
Open myFile For Output As #1
Print #1, rng.Columns.Count - 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
ElseIf i = 1 And j = 1 Then
Print #1, prophetheader, cellValue,
ElseIf j = 1 Then
Print #1, "*", cellValue,
Else
Print #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
The code is quite straight forward, just add * in the front of each line and add !1 in the column header. But I encounter the issue that the output file has been delimited using comma and no double quotation in the string. And I am in the dilemma that if I use write function, it will use comma as delimiter, but has double quotation on string and if I use Print, space as delimiter and no double quote on string.
Is there a way to print the file using comma delimiter and no double quote on string?
Below is the dummy data selection.
SPCODE NAME
1 JS
And the output should be
21
!1,SPCODE,NAME
*,1,JS
Maybe instead of writing each single row to the text file you just write each line in total like that
Option Explicit
Sub Export_TEST()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
Dim prophetheader As String
Dim prophetlineheader As String
myFile = ThisWorkbook.Path & "\" & "test" & ".txt"
Set rng = Selection
prophetheader = "!1"
Open myFile For Output As #1
Print #1, rng.Columns.Count - 1
Dim line As String
For i = 1 To rng.Rows.Count
line = ""
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
line = line & cellValue & ","
ElseIf i = 1 And j = 1 Then
line = line & prophetheader & cellValue & ","
ElseIf j = 1 Then
line = line & "*" & cellValue & ","
Else
line = line & cellValue & ","
End If
Next j
line = Left(line, Len(line) - 1)
Print #1, line
Next i
Close #1
End Sub

Create and Write to a text file using an excel macro and VBA

I am using a macro and VBA code to create a text file with a specific format. All the data needed to create the text file is gathered from the macro cells.
I have attached pictures of the macro data file and the output text file (please see below).
excel macro with data
Desired output txt format-example
Also, below is my VBA code I generated to get data from the macro and create/write into a text file. I still need to figure out how to write it in the specified format (Desired output txt format-example).
Sub ExcelToTxt()
'Declaring variables
Dim lCounter As Long
Dim lLastRow As Long
Dim destgroup As String
Dim parmlabel as Variant
Dim FName As Variant
'Activate Sheet1
Sheet1.Activate
'Find the last row that contains data
With Sheet1
lLastRow = .Cells(.Rows.Count, "A").End(xlDown).Row
End With
'Create txt file
FName = Application.GetSaveAsFilename("", "txt file (*.txt), *.txt")
'Open FName For Output As #1
For lCounter = 2 To lLastRow
'Read specific data from the worksheet
With Sheet1 destgroup = .Cells(lCounter, 19)
parmlabel = .Cells(lCounter, 8)
If destgroup="trex_15hz" Or destgroup="trex_10hz" Or destgroup="trex_5hz" Then
'Write selected data to text file
'Write #1, parmlabel
End If
End With
'Continue looping until the last row
Next lCounter
'Close the text file
Close #1
End Sub
Any help with what I need to add in my VBA to create the formatted output txt file will be greatly appreciate it.
Thank you in advance.
You can combine the data into an array and then convert it back into text.
Sub ExcelToTxt()
'Declaring variables
Dim i As Long, j As Integer
Dim n As Long, k As Long
Dim destgroup As String
Dim FName As String
Dim vDB, vR(1 To 6), vJoin(), vResult()
Dim sJoin As String, sResult As String
Dim s As Long
'Activate Sheet1
Sheet1.Activate
'Find the last row that contains data
With Sheet1
vDB = .Range("a1").CurrentRegion '<~~ get data to array from your data range
n = UBound(vDB, 1) 'size of array (row of 2 dimension array)
End With
'Create txt file
FName = Application.GetSaveAsFilename("", "txt file (*.txt), *.txt")
For i = 2 To n '<~~loop
destgroup = vDB(i, 2) '<~~ second column
If destgroup = "trex_15hz" Or destgroup = "trex_10hz" Or destgroup = "trex_5hz" Then
vR(1) = "; ### LABEL DEFINITION ###" '<~~ text 1st line
s = Val(Replace(vDB(i, 3), "label", ""))
vR(2) = "EQ_LABEL_DEF,02," & Format(s, "000")
vR(3) = "UDB_LABEL," & Chr(34) & vDB(i, 4) & Chr(34) '<~~ 2nd line
ReDim vJoin(4 To 7)
vJoin(4) = Chr(34) & vDB(i, 4) & Chr(34)
For j = 5 To 7
vJoin(j) = vDB(i, j)
Next j
sJoin = Join(vJoin, ",")
vR(4) = "STD_SUB_LABE," & sJoin '<~~ 3th line
ReDim vJoin(8 To 12)
vJoin(8) = Chr(34) & UCase(vDB(i, 8)) & Chr(34)
vJoin(9) = Chr(34) & vDB(i, 9) & Chr(34)
vJoin(10) = Format(vDB(i, 10), "#.000000000")
For j = 11 To 12
vJoin(j) = vDB(i, j)
Next j
sJoin = Join(vJoin, ",")
vR(5) = "STD_SUB_LABE," & sJoin '<~~ 4the line
vR(6) = "END_EQ_LABEL_DEF" '<~~ 5th line
k = k + 1
ReDim Preserve vResult(1 To k)
vResult(k) = Join(vR, vbCrLf) '<~~ 5 line in array vR and get to array vResult with join method
End If
Next i
sResult = "EQUIPMENT_ID_DEF,02,0x1," & Chr(34) & "trex" & Chr(34) '<~~ text file first line
sResult = sResult & vbCrLf & Join(vResult, vbCrLf) '<~~ combine 1th and other line
ConvertText FName, sResult '<~~ sub presedure
End Sub
Sub ConvertText(myfile As String, strTxt As String)
Dim objStream
Set objStream = CreateObject("ADODB.Stream")
With objStream
'.Charset = "utf-8"
.Open
.WriteText strTxt
.SaveToFile myfile, 2
.Close
End With
Set objStream = Nothing
End Sub

Create multiple text files for selected data on excel using vba scripting in single click

I am using the code below to generate the single file for selected range and considering the first cell in the selected range as file name. Please find the image below for more details[This image shows the selected range,Consider K column(Firstline) and N Column( Lastline) to be in one file and other set of 1st and last line in other file ]this image shows the print file for a single file this is the way m currently using for generating files.I need to create more 30k files so please help me to create more files in single click considering the first and last line as header and footer for the file
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer, path As String, filename, filename2 As String
path = "D:\Watchlist-Files\"
filename = Selection.Cells(1, 1).Value
filename2 = Left(Mid(filename, 32, 99), Len(Mid(filename, 32, 99)) - 2)
myFile = path & filename2
Set rng = Selection
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 code below is using a Loop that scans rows in a range that consists of Columns K:N (according to your attached screen-shots).
Assumptions made: your FirstLine is in Column K, and it's the marker of the start position of copying the first cell in the first row.
Your LastLine is in Column N, and it's the marker of the last cell to copy, this is why I am closing the file once it is found.
Edit 1: added a Msgbox to allow the user selection of exporting the entire range or not. In case the user selected NO, then a second InputBox appears that allows the user to enter manually the last row number to export.
Option Explicit
Public Sub CommandButton1_Click()
Dim myFile As String
Dim rng As Range
Dim cellValue As Variant
Dim i As Long
Dim j As Long
Dim LastRow As Long
Dim path As String
Dim filename As String
Dim response As Boolean
path = "D:\Watchlist-Files\"
response = MsgBox("Do you want to Export the entire Range ? ", vbYesNo)
' Export the entire Range
If response = vbYes Then
LastRow = Cells(Rows.Count, "N").End(xlUp).Row
Else ' enter in the inputbox the last row number you want to export
LastRow = InputBox("Enter Last Row Number you wsnt to Export")
End If
Set rng = Range("K2:N" & LastRow)
For i = 2 To LastRow
' Column K holds the file name
If Cells(i, 11) <> "" Then
filename = Left(Mid(Cells(i, 11).Value, 32, 99), Len(Mid(Cells(i, 11).Value, 32, 99)) - 2)
myFile = path & filename
Open myFile For Output As #1
End If
For j = 1 To rng.Columns.Count
cellValue = Cells(i, 10 + j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
' found LastLine >> close the file
If Not cellValue = "" Then
Close #1
End If
Else
Print #1, cellValue,
End If
Next j
Next i
End Sub
Edit 2: Added new code below (to keep the first option valid). The user needs to confirm that every selection he makes start and ends with FirstLine and LastLine , there is no error handling.
Option Explicit Section
Option Explicit
Dim filename As String
Dim path As String
Dim myFile As String
Dim rng As Range
Dim j As Long
Public Sub CommandButton1_Click
Public Sub CommandButton1_Click()
Dim lastRow As Long
Dim Sel_Range As Long
Dim response As Boolean
Dim rowStart() As Long
Dim rowFinish() As Long
path = "D:\Watchlist-Files\"
response = MsgBox("Do you want to Export only the Selected Range ? ", vbYesNo)
If response = True Then
Set rng = Selection
ReDim rowStart(1 To Selection.Areas.Count)
ReDim rowFinish(1 To Selection.Areas.Count)
For Sel_Range = 1 To Selection.Areas.Count
rowStart(Sel_Range) = Selection.Areas(Sel_Range).Row
rowFinish(Sel_Range) = Selection.Areas(Sel_Range).Row + Selection.Areas(Sel_Range).Rows.Count - 1
Call CreateTextFiles(rowStart(Sel_Range), rowFinish(Sel_Range))
Next Sel_Range
Else ' export the entire Range in Columns K:N
lastRow = Cells(Rows.Count, "N").End(xlUp).Row
Set rng = Range("K2:N" & lastRow)
Call CreateTextFiles(2, lastRow)
End If
Sub CreateTextFiles(Sel_StartRow As Long, Sel_FinishRow As Long) - new routine to allow handling of multiple Ranges selection
Sub CreateTextFiles(Sel_StartRow As Long, Sel_FinishRow As Long)
Dim i As Long
Dim cellValue As Variant
For i = Sel_StartRow To Sel_FinishRow
' Column K holds the file name
If Cells(i, 11) <> "" Then
filename = Left(Mid(Cells(i, 11).Value, 32, 99), Len(Mid(Cells(i, 11).Value, 32, 99)) - 2)
myFile = path & filename
Open myFile For Output As #1
End If
For j = 1 To rng.Columns.Count
cellValue = Cells(i, 10 + j).Value
If j = rng.Columns.Count Then
Print #1, cellValue
' found LastLine >> close the file
If Not cellValue = "" Then
Close #1
End If
Else
Print #1, cellValue,
End If
Next j
Next i
End Sub

VBA: Exit For Loop when row is empty

I'm using the following code to export rows to individual text files:
Sub export_Test()
Dim firstRow As Integer, lastRow As Integer, fileName As String
Dim myRow As Integer, myStr As String
firstRow = 10
lastRow = 29
For myRow = firstRow To lastRow
fileName = "C:\mallet\test\" & Cells(myRow, 1) & ".txt"
Open fileName For Append As #1
myStr = Cells(myRow, 2).Value
Print #1, myStr
Close #1
Next
End Sub
The problem is that this code is for a specific number of rows. I want to use this code for different data samples, so the number of rows in the excel file will vary and could number in the thousands. I need the lastRow variable to be set to an infinite number and exit the For Loop when it hits an empty row.
This code will start in row 10 and run until it finds a blank cell in the second column. Note that I also shortened your code a bit (though it still does the same writing to a file):
Sub export_Test()
Dim myRow As Long
myRow = 10
While Cells(myRow, 2).Value <> ""
Open "C:\mallet\test\" & Cells(myRow, 1) & ".txt" For Append As #1
Print #1, Cells(myRow, 2).Value
Close #1
myRow = myRow + 1
Wend
End Sub
This is code from a project of mine that does exactly what you want - end with a blank value
Sub export_Test()
Dim firstRow As Integer, lastRow As Integer, fileName As String
Dim myRow As Integer, myStr As String
firstRow = 10
myRow = firstRow
' Seed initial value
Cells(myRow, 1).Select
' Keep going until a blank cell is found
While Trim(ActiveCell.Value) <> ""
fileName = "C:\mallet\test\" & ActiveCell.Value & ".txt"
Open fileName For Append As #1
myStr = Cells(myRow, 2).Value
Print #1, myStr
Close #1
' Get the next value
myRow = myRow + 1
Cells(myRow, NameCol).Select
Wend
End Sub

Excel 2010 - Export single XSLM to multiple CSV Files

Alright, so, basically I have an XSLM file containing about ~40k rows. I need to export these rows to a customized CSV format - ^ delimited and ~ marking the boundaries of each cell. Once they've been exported, they are read in by a Joomla importer app and processed into the database. I found a good macro script which does just that and tweaked it to use the correct delimiters.
Sub CSVFile()
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
'ListSep = Application.International(xlListSeparator)
ListSep = "^" ' Use ^ as field separator.
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & "~" & CurrCell.Value & "~" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub
However, what've I've found is that the generated CSVs are simply too big to be handled with the available script execution time. I can split the files manually to about 5000 rows apiece and it does well enough. What I'd like to do is adjust the above script as follows:
Stores the header row to be inserted into each file.
Asks the user how many rows should be output per file.
Appends -pt# to the chosen save as file name.
Processes out the Excel file into as many 'chunk' csv files as required.
For example, if my file name was output, the file break number was 5000, and the excel file had 14000 rows, I'd end up with output-pt1.csv, output-pt2.csv, and output-pt3.csv.
If it were just me doing it, I'd just keep breaking the files manually, but when all is said and done I need to hand these files off to the client commissioning the project, so the easier the better.
Much appreciated for any ideas.
Something like this might work for you. Untested, but compiles...
Sub CSVFile()
Const MAX_ROWS As Long = 5000
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant, newFName As String
Dim TextHeader As String, lRow As Long, lFile As Long
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
'ListSep = Application.International(xlListSeparator)
ListSep = "^" ' Use ^ as field separator.
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
lRow = 0
lFile = 1
newFName = Replace(FName, ".csv", "_pt" & lFile & ".csv")
Open newFName For Output As #1
For Each CurrRow In SrcRg.Rows
lRow = lRow + 1
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & "~" & CurrCell.Value & "~" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
If lRow = 1 Then TextHeader = CurrTextStr
Print #1, CurrTextStr
If lRow > MAX_ROWS Then
Close #1
lFile = lFile + 1
newFName = Replace(FName, ".csv", "_pt" & lFile & ".csv")
Open newFName For Output As #1
Print #1, TextHeader
lRow = 0
End If
Next
Close #1
End Sub
So, with Tim's help, here's the final version that accepts an argument on the max number of rows per file, and outputs to as many sub files as needed.
Sub CSVFile()
Dim MaxRows As Long
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant, newFName As String
Dim TextHeader As String, lRow As Long, lFile As Long
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
MaxRows = Application.InputBox(Prompt:="Enter maximum number of rows per file.", _
Default:=5000, Type:=1)
'ListSep = Application.International(xlListSeparator)
ListSep = "^" ' Use ^ as field separator.
If Selection.Cells.Count > 1 Then
Set SrcRg = Selection
Else
Set SrcRg = ActiveSheet.UsedRange
End If
lRow = 0
lFile = 1
newFName = Replace(FName, ".csv", "-pt" & lFile & ".csv")
Open newFName For Output As #1
For Each CurrRow In SrcRg.Rows
lRow = lRow + 1
CurrTextStr = ""
For Each CurrCell In CurrRow.Cells
CurrTextStr = CurrTextStr & "~" & CurrCell.Value & "~" & ListSep
Next
While Right(CurrTextStr, 1) = ListSep
CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
If lRow = 1 And lFile = 1 Then TextHeader = CurrTextStr 'Capture the header row
Print #1, CurrTextStr
If lRow > MaxRows Then
Close #1
lFile = lFile + 1
newFName = Replace(FName, ".csv", "-pt" & lFile & ".csv")
Open newFName For Output As #1
Print #1, TextHeader
lRow = 0
End If
Next
Close #1
End Sub
I just added a request for user input to get the max rows, and also tweaked it so it didn't update the header row with each new file. Thanks again for the help.