Generate Txt File VBA Question

Ruppert23

New Member
Joined
Mar 30, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello All! I was curious if anyone was able to assist with the VBA process for the below desired outcome.

I am looking to have a text file generated after clicking the "Generate Txt File" button in the below Excel snippet. The text file would then format like the second snippet below and take the current date & total daily settlement amount and plug it into the text file. (I would even be okay with it generating a blank txt file where I can type the info in)

Cheers!

Snippet #1 Generate.jpg

Snippet #2 Txt File Example.jpg
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
So your desired (csv) text file would only contain one line as shown?
 
Upvote 0
1680536470326.png

VBA Code:
Public Sub GenerateTextFile()
    Dim DateStr As String, Outfile As String, TemplateStr As String, TextLine As String
    Dim DailySettlement As Variant
    Dim WS As Worksheet

    Set WS = ActiveSheet

    Outfile = "C:\Temp\Daily.csv"
    DailySettlement = Format(WS.Range("B1").Value, "0.00")
    DateStr = Format(Date, "yyyy-mm-dd")
    TemplateStr = "ABC COLLATERAL,5000000.00,2023-03-30,,,,0,John Doe,000-00-0000"

    TextLine = Replace(TemplateStr, "2023-03-30", DateStr)
    TextLine = Replace(TextLine, "5000000.00", DailySettlement)

    Open Outfile For Output Access Write As #1        ' Open text file for write
    Print #1, TextLine                                ' Write to output file
    Close #1                                          ' Close file.
End Sub
 
Upvote 0
Where is the rest of the text coming from? Like John Doe?

This could be done with a one liner
 
Upvote 0
View attachment 88943
VBA Code:
Public Sub GenerateTextFile()
    Dim DateStr As String, Outfile As String, TemplateStr As String, TextLine As String
    Dim DailySettlement As Variant
    Dim WS As Worksheet

    Set WS = ActiveSheet

    Outfile = "C:\Temp\Daily.csv"
    DailySettlement = Format(WS.Range("B1").Value, "0.00")
    DateStr = Format(Date, "yyyy-mm-dd")
    TemplateStr = "ABC COLLATERAL,5000000.00,2023-03-30,,,,0,John Doe,000-00-0000"

    TextLine = Replace(TemplateStr, "2023-03-30", DateStr)
    TextLine = Replace(TextLine, "5000000.00", DailySettlement)

    Open Outfile For Output Access Write As #1        ' Open text file for write
    Print #1, TextLine                                ' Write to output file
    Close #1                                          ' Close file.
End Sub
This helps a lot, thank you for assisting! The only issue I am running into is the file path error. For reference, this is where I need the text file saved below after generating it via the macro button.

1680538329069.png

1680538260799.png
1680538414884.png
 
Upvote 0
That defines a folder path. You need variable Outfile to define a file path.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top