VBA Change name of the Text file

I am using this code:-
Sub VBA_write_to_a_text_file_from_Excel_Range()
Dim iCntr
Dim strFile_Path As String
strFile_Path = "C:\temp\test.txt"
Open strFile_Path For Output As #1
For iCntr = 1 To 10
Print #1, Range("A" & iCntr)
Next iCntr
Close #1
End Sub
I need help in having a new name every time a file is generated. In this case the old file gets overrided.
Top Contributor Asked on January 11, 2017 in VBA: Programming.
Add Comment
1 Answer(s)

Hi,

Here is the solution for your query to create or change name of the text file.

Sub VBA_write_to_a_text_file_from_Excel_Range()
Dim iCntr
Dim strFile_Path As String
strFile_Path = "C:\temp\test " & Format(Now(), "dd-MMM-yyyy h-m-s") & ".txt"
Open strFile_Path For Output As #1
For iCntr = 1 To 10
Print #1, Range("A" & iCntr)
Next iCntr
Close #1
End Sub

Thanks!

Expert Answered on January 11, 2017.

Thank you so much you are a life saver. God bless you

on January 12, 2017.
Add Comment
  • Found this useful?

    Please share using the share button above.

    If you found the answer is best answer for your question, Please mark as 'best answer' by clicking on the right tick mark icon at the left side of the answer.

    Found the answer useful and wants to credit the user, then vote the answer (vote up).

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.