To output the specified columns from an Excel worksheet into a text file using VBA, you would need to iterate through each row and format the output according to your specifications. Here's a VBA macro that performs this action:
VBA Code to Output Specific Columns to a Text File
This code assumes your data is on a worksheet named "Sheet1" and outputs a text file with structured data, formatted as per your requirements described earlier. Adjust the sheet name and column mappings in the code if needed.
Sub ExportDataToTextFile()
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim outputFile As String
Dim lastRow As Long
Dim line As String
Dim totalTransactionValue As Double
Dim totalStampDutyValue As Double
' Set the worksheet and define the range
Set ws = ThisWorkbook.Worksheets("Sheet1") ' Adjust the sheet name if necessary
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Assumes timestamp is in column A
' Define output file path (modify as needed)
outputFile = ThisWorkbook.Path & "\Output_" & Format(Now, "YYYYMMDD") & ".txt"
' Open the text file for writing
Open outputFile For Output As #1
' Loop through each row in the defined range
For i = 2 To lastRow ' Assuming data headers are in the first row
line = "" ' Start with empty line
' Columns are adjusted according to indexes as per the question prompt
With ws
' Construct the line by appending formatted values from each column
line = line & Format(.Cells(i, 1).Value, "YYYYMMDD") ' Timestamp -> Trading Date
line = line & Format(.Cells(i, 1).Value, "HHMMSS") ' Timestamp -> Trade Time
line = line & Right(" " & .Cells(i, 6).Value, 5) ' Broker Firm ID
line = line & Right("0000" & .Cells(i, 7).Value, 4) ' Broker Number
line = line & Left(.Cells(i, 8).Value & String(30, " "), 30) ' Broker Firm Name
line = line & Right(" " & .Cells(i, 9).Value, 5) ' Stock code
line = line & .Cells(i, 3).Value ' Buy/Sell -> 'way'
line = line & Right("0000" & .Cells(i, 10).Value, 4) ' Corr Broker Number
line = line & .Cells(i, 11).Value ' Trade Type
line = line & .Cells(i, 12).Value ' Settlement Type
line = line & Right(" " & Format(.Cells(i, 5).Value, "0.000"), 10) ' Price
line = line & Right(" " & .Cells(i, 4).Value, 12) ' Quantity
line = line & Right(" " & Format(.Cells(i, 13).Value, "0.00"), 15) ' Transaction Value
totalTransactionValue = totalTransactionValue + .Cells(i, 13).Value
line = line & Right(" " & Format(.Cells(i, 14).Value, "0.00"), 15) ' Stamp Duty Adj Amount
totalStampDutyValue = totalStampDutyValue + .Cells(i, 14).Value
line = line & .Cells(i, 15).Value ' Amendment Applied
line = line & Right(String(18, " ") & .Cells(i, 16).Value, 18) ' Trade Reference Group
line = line & String(24, " ") ' Reserved
End With
' Write the line to the text file
Print #1, line
Next i
' Add the checksum line
Dim checksumLine As String
checksumLine = Right(" " & Format(totalTransactionValue, "0.00"), 15) & _
Right(" " & Format(totalStampDutyValue, "0.00"), 15)
Print #1, checksumLine
' Close the text file
Close #1
MsgBox "Data exported to " & outputFile, vbInformation
End Sub
Explanation of the Code
-
Worksheet and Range Setup:
- The worksheet "Sheet1" is set as the source of data, and the
lastRow is determined based on the timestamp column to ensure all data is covered.
-
Output File Path:
- Generate an output file in the same directory as the workbook, naming it with the current date for clarity.
-
Data Processing:
- A loop runs from the second row to the last, gathering data from specific columns. Adjustments are made through string manipulation and formatting to meet the format requirements.
-
Checksum Calculation:
- During data processing, totals for transaction values and stamp duty adjustment amounts are computed. This is used to generate the checksum line appended at the end of the file.
-
Output:
- Data is written to the text file line by line, followed by the inclusive checksum line, and the file is closed to complete the write process.
-
Notification:
- A message box confirms the completion of the export process, showing the file path to the user.
Adjust the column indices in the script as necessary to match your worksheet's exact layout. Make sure to save your workbook and back up any important data before running this script to avoid accidental data loss.