VBA to export dat files from each row of a workbook

zinizaga

New Member
Joined
May 8, 2017
Messages
3
Hi all, i need help writing a macro that copies specific data from each row of a workbook and puts it into a separate dat file for each row. So far i was able to write something up that only works for the first row(seconf beneath header). How can i modify this to export each row on a separate file - again, having some custom ordering applied same as i do below

Sample data:
Project nameClientFirst nameLast nameuserid
JHGHNikeJohnDoejohnd
VBNMCoca ColaJaneDoejaned2

<tbody>
</tbody>

Code:
Option Explicit

Sub TEST()


Dim fso As Object, opStr As String
Set fso = CreateObject("Scripting.FileSystemObject")


Dim Fileout As Object, fPath As String


fPath = ThisWorkbook.Path & "\" & Data.Range("A2") & "-" & Format(Now, "MM.DD.YYYY") & ".dat"
Set Fileout = fso.CreateTextFile(fPath, True, True)




opStr = "SOME HARDCODED TEXT &" _
& Data.Range("A2") & "|" & Data.Range("B2") & "|" & Data.Range("C2") & "|" & Data.Range("D2")


Fileout.Write opStr


Fileout.Close


MsgBox "Done. File: " & fPath


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this:
Code:
Public Sub Create_Dat_Files()

    Dim r As Long, lastRow As Long
    Dim filePath As String
    
    With ActiveSheet
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).row
        For r = 2 To lastRow
            filePath = ThisWorkbook.Path & "\" & .Cells(r, "A").Value & "-" & Format(Now, "MM.DD.YYYY") & ".dat"
            Open filePath For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
            Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , "SOME HARDCODED TEXT &" & .Cells(r, "A").Value & "|" & .Cells(r, "B").Value & "|" & .Cells(r, "C").Value & "|" & .Cells(r, "D").Value
            Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Next
    End With
    
    MsgBox "Done"
    
End Sub
 
Upvote 0
Based on the original code.
Code:
Sub TEST()
Dim rng As Range
Dim FSO As Object
Dim Fileout As Object
Dim fPath As String
Dim opStr As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Set rng = Data.Range("A2")
    Do
        fPath = ThisWorkbook.Path & "c:\Test\" & rng.Value & "-" & Format(Now, "MM.DD.YYYY") & ".dat"
    
        Set Fileout = FSO.CreateTextFile(fPath, True, True)

        opStr = "SOME HARDCODED TEXT &" _
            & rng.Value & "|" & rng.Offset(, 1).Value & "|" & rng.Offset(, 2).Value & "|" & rng.Offset(, 3).Value

        Fileout.Write opStr
        
        Fileout.Close

        Set rng = rng.Offset(1)
        
    Loop Until rng.Value = ""

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,182
Members
448,948
Latest member
spamiki

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