create separate file for each row in Worksheet using VBA

Hi, I am using this code to export each line item as a separate file, however I want the column headers to be constant, unable to do it, for instance I would like each file to have the column names but contain only the specific assignee information

create separate file for each row in Worksheet using VBAsample file

Sub ExportTextFiles()
Dim i As Long
Dim LastDataRow As Long
Dim MyFile As String
Dim fnum
With ActiveSheet
LastDataRow = .Range(“A” & Rows.Count).End(xlUp).Row
lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
For i = 2 To LastDataRow
'The next line uses the contents of column B on the same row to name it
MyFile = "C:\Users\pkc504787\Desktop\Client Work\Trifecta\test\" & ActiveSheet.Range("A" & i).Value & ".pdf"
' fnum = FreeFile()
Open MyFile For Output As fnum
'It reads all values from respective row
For iCntr = 1 To lastColumn
Print #1, Cells(i, iCntr)
Next iCntr
Close fnum
Next i
End Sub

Top Contributor Asked 1 day ago in VBA: Macros.
Add Comment
0 Answer(s)
  • 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.