How do I have a file generated with a unique name instead of “Assignee” as in this code below? How can I link it to a cell ?

Option Explicit

Private Sub CommandButton1_Click()
Dim iCntr

Dim strFile_Path As String
strFile_Path = “C:\Users\pkc504787\Desktop\Assignee” & Format(Now(), “dd-MMM-yyyy h-m-s”) & “.xml”
Open strFile_Path For Output As #1
For iCntr = 2 To 2329

Print #1, Sheets(“CCH_Import_Template_2016”).Range(“D” & iCntr)

Next iCntr
Close #1

End Sub

Top Contributor Asked on January 26, 2017 in VBA: Programming.
Add Comment
2 Answer(s)

HI,

Here is the VBA code  to generate Excel file with unique name.

Please use either of the one below statement.

'Change cell reference accordingly
Filename = Sheets("Sheet1").Range("A1")
'or
'Filename = InputBox("Enter File Name", "File Name", 8)

 

Here is the complete VBA macro.

Option Explicit
Private Sub CommandButton1_Click()
Dim iCntr
Dim strFile_Path As String
Dim Filename As String
'Change cell reference accordingly
Filename = Sheets("Sheet1").Range("A1")
'or
'Filename = InputBox("Enter File Name", "File Name", 8)
 
strFile_Path = "C:\Users\pkc504787\Desktop\" & Filename & " " & Format(Now(), "dd-MMM-yyyy h-m-s") & ".xml"
Open strFile_Path For Output As #1
For iCntr = 2 To 2329
Print #1, Sheets("CCH_Import_Template_2016").Range("D" & iCntr)
Next iCntr
Close #1
End Sub

Hope it clarifies your query.

Regards!

Expert Answered on January 27, 2017.
Add Comment

Thank you, this works perfectly fine.

Top Contributor Answered on January 27, 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.