Hi Ganesh

You can try that
Create a module and copy paste into it

Sub CopyMergeTxtFiles()

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

Dim ws As Worksheet

Dim i As Long, j As Long
Dim lastr As Long
Dim V() As Variant
Dim r As Range
Dim rEnd As Long
Dim folderPathFileName As String

'Affect worksheet object
Set ws = ThisWorkbook.Worksheets("Sheet1")

'Look for the last row in column A
lastr = ws.Cells(Rows.Count, 1).End(xlUp).Row


'''
'This loop will store values from column A
'i.e file names and path to a temporary table V
'from a range r that will call a sub local procedure
'to process files
'
'''

For i = 1 To lastr
    If ws.Cells(i, 2).Value <> vbNullString Then
        folderPathFileName = ws.Cells(i, 2).Value
        For j = i + 1 To lastr
            If ws.Cells(j, 2).Value <> vbNullString Then
                Set r = ws.Range(ws.Cells(i, 1), ws.Cells(j - 1, 1))
                'uncomment below to check if the range is made of correct 
data
                'r.Select
                V() = r
                CopyDataToTxtFile V(), folderPathFileName 'Local sub to 
process data to files
                Set r = Nothing
                Exit For
                
            ElseIf j >= lastr Then
                Set r = ws.Range(ws.Cells(i, 1), ws.Cells(lastr, 1))
                'uncomment below to check if the range is made of correct 
data
                'r.Select
                V() = r
                CopyDataToTxtFile V(), folderPathFileName 'Local sub to 
process data to files
                Set r = Nothing
                Exit For
            End If
            
        Next j
        If Not r Is Nothing Then Set r = Nothing
    End If
Next i

Set r = Nothing
Set ws = Nothing

With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

End Sub

Sub CopyDataToTxtFile(T() As Variant, fname As String)
'This sub will copy data from existing
'text files and create another text file
'folder named Merge using OpenAsTextStream VB method

Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim fs As Object, f As Object, ts As Object
Dim fil As Object, fts As Object
Dim s As String
Dim i As Long
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CreateTextFile fname

Set f = fs.GetFile(fname)
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)

For i = LBound(T, 1) To UBound(T, 1)
    Set fil = fs.GetFile(T(i, 1))
    Set fts = fil.OpenAsTextStream(ForReading, TristateUseDefault)
    s = s & fts.ReadAll & vbCrLf
    'MsgBox s
    fts.Close
    Set fts = Nothing
Next i

ts.write s
s = vbNullString
ts.Close
Set fs = Nothing
Set ts = Nothing
Set f = Nothing

End Sub


Pascal Baro
http://multiskillz.tekcities.com/


-- 
Join official Facebook page of this forum @ 
https://www.facebook.com/discussexcel

FORUM RULES

1) Use concise, accurate thread titles. Poor thread titles, like Please Help, 
Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will not get 
quick attention or may not be answered.
2) Don't post a question in the thread of another member.
3) Don't post questions regarding breaking or bypassing any security measure.
4) Acknowledge the responses you receive, good or bad.
5) Jobs posting is not allowed.
6) Sharing copyrighted material and their links is not allowed.

NOTE  : Don't ever post confidential data in a workbook. Forum owners and 
members are not responsible for any loss.
--- 
You received this message because you are subscribed to the Google Groups "MS 
EXCEL AND VBA MACROS" group.
To post to this group, send email to excel-macros@googlegroups.com.
To unsubscribe from this group, send email to 
excel-macros+unsubscr...@googlegroups.com.
Visit this group at http://groups.google.com/group/excel-macros?hl=en.


Reply via email to