
Originally Posted by
anilsolipuram
Read through the comments
Sub Macro1()
Dim ar As Variant
Dim file_path As Variant
file_path = "c:\test\" 'this is the path where the text files exist
ar = Array("Diag000.txt", "Diag001.txt") ' this are the files from which we need to copy
Dim file_name As Variant
file_name = ActiveWorkbook.Name
Cells.Select
Cells.Delete
For i = 0 To UBound(ar)
'below code will open the text demlimit by space
Workbooks.OpenText Filename:=file_path & ar(i), Origin:=xlWindows, StartRow _
:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, Comma:=False _
, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array _
(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1))
Range(ActiveSheet.UsedRange.Address).Select
Selection.Copy 'copy the text file format
While ActiveWorkbook.Name <> file_name
ActiveWindow.ActivateNext
Wend
Dim r As Range
Set r = ActiveSheet.UsedRange
temp = Split(r.Address, ":")
If UBound(temp) > 0 Then
Range("a" & Range(temp(1)).Offset(1, 0).Row).Select
Else
Range("a" & Range(r.Address).Row).Select
End If
ActiveSheet.Paste 'paste into the workbook
Application.CutCopyMode = False
While ActiveWorkbook.Name <> ar(i)
ActiveWindow.ActivateNext
Wend
ActiveWindow.Close 'close the text file
Next
End Sub
Bookmarks