Hi yourskarthik, and welcome to the board.
I have made the following presumptions: by "Notepads" you mean text files and the fields in the text files are seperated (deliminated) by commas.
In the code below you will have to edit these variables to meet your needs.
Code:
[COLOR=green]'---------------------[/COLOR]
[COLOR=green]'----- EDIT THIS -----[/COLOR]
[COLOR=green]'---------------------[/COLOR]
[COLOR=darkblue]Const[/COLOR] myPath = "C:\Temp\"
[COLOR=darkblue]Const[/COLOR] delim = ","
The code reads
all text files in the directory and stores their values in an array. Once all the text files have been read the array is output to "Sheet1" on the workbook containing this code.
Press
ALt + F11 to launch the VBE Editor
Click
Insert => Module
Copy and Paste the code into the module
Run the code (F5)
Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=green]'---------------------[/COLOR]
[COLOR=green]'----- EDIT THIS -----[/COLOR]
[COLOR=green]'---------------------[/COLOR]
[COLOR=darkblue]Const[/COLOR] myPath = "C:\Temp\"
[COLOR=darkblue]Const[/COLOR] delim = ","
[COLOR=darkblue]Sub[/COLOR] MergeMultipleTextFiles()
[COLOR=darkblue]Dim[/COLOR] myFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] [COLOR=green]'filename[/COLOR]
[COLOR=darkblue]Dim[/COLOR] strRecord [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] 'record string
[COLOR=darkblue]Dim[/COLOR] arrRecord() [COLOR=green]'record array[/COLOR]
[COLOR=darkblue]Dim[/COLOR] fNum [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR] 'free file number
[COLOR=darkblue]Dim[/COLOR] RowCounter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR] [COLOR=green]'row counter[/COLOR]
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR] 'loop variable
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] errHandler
myFile = Dir(myPath & "*.txt")
RowCounter = 0
[COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] myFile <> ""
fNum = FreeFile
[COLOR=darkblue]Open[/COLOR] myPath & myFile [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Input[/COLOR] [COLOR=darkblue]As[/COLOR] #fNum
[COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]While[/COLOR] [COLOR=darkblue]Not[/COLOR] EOF(fNum)
[COLOR=green]'read in the row into the record variable[/COLOR]
Line [COLOR=darkblue]Input[/COLOR] #fNum, strRecord
[COLOR=green]'add the filename to the record variable[/COLOR]
strRecord = myFile & delim & strRecord
RowCounter = RowCounter + 1
[COLOR=green]'resize the record array[/COLOR]
[COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] arrRecord(1 [COLOR=darkblue]To[/COLOR] RowCounter)
arrRecord(RowCounter) = Split(strRecord, delim)
[COLOR=darkblue]Loop[/COLOR]
[COLOR=darkblue]Close[/COLOR] #fNum
[COLOR=green]'get the next file in the directory[/COLOR]
myFile = Dir()
[COLOR=darkblue]Loop[/COLOR]
[COLOR=green]'[/COLOR]
'------------------
[COLOR=green]'----- OUTPUT -----[/COLOR]
[COLOR=green]'------------------[/COLOR]
[COLOR=darkblue]With[/COLOR] ThisWorkbook.Sheets("Sheet1").Range("A1")
[COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] RowCounter
.Offset(i - 1).Resize(, [COLOR=darkblue]UBound[/COLOR](arrRecord(i)) + 1).Value = arrRecord(i)
[COLOR=darkblue]Next[/COLOR] i
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
errExit:
[COLOR=green]'close all open text files[/COLOR]
Reset
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
errHandler:
[COLOR=green]'error message goes here[/COLOR]
[COLOR=darkblue]Resume[/COLOR] errExit
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]