Update macro to save file with date/timestamp and into a certain folder?

NormChart55

New Member
Joined
Feb 22, 2022
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have this macro that saves data into a comma delimited .txt file that I can import into another system. The file has to be .txt as the system does not recognize .csv. I am trying to add to the file name a date and timestamp of when it was created and to save into a specific folder. Currently it saves into my documents folder but would like to keep them in another without having to move. Any thoughts on how to do that with this macro? Any help is certainly appreciated.

VBA Code:
Const MyDelimiter As String = ","
Dim iData As Range
Dim iRng As Range
Dim iFile As Long
Dim iResult As String
iFile = FreeFile

Open "ManualFix.txt" For Output As #iFile
    For Each iData In Range("AF7:AF" & Range("AF" & Rows.Count).End(xlUp).Row)
        With iData
            For Each iRng In Range(.Cells, Cells(.Row, Columns.Count).End(xlToLeft))
                iResult = iResult & MyDelimiter & iRng.Text
            Next iRng
            Print #iFile, Mid(iResult, 2)
            iResult = Empty
        End With
    Next iData
Close #iFile

End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Why don't you just use SaveAs to save it as a csv file to start with? Then you can do a rename after you save it.

If you stick with your existing code you can do this for save to a different folder:

VBA Code:
Open "C:\Users\NormChart55\Documents\Some Folder\ManualFix " & Format(Now, "yyyy-mm-dd hhmm") & ".txt" For Output As #iFile
 
Upvote 0
Solution
Why don't you just use SaveAs to save it as a csv file to start with? Then you can do a rename after you save it.

If you stick with your existing code you can do this for save to a different folder:

VBA Code:
Open "C:\Users\NormChart55\Documents\Some Folder\ManualFix " & Format(Now, "yyyy-mm-dd hhmm") & ".txt" For Output As #iFile

Thank you so much. The updated line of code works just fine. I appreciate the quick assist, you rock!
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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