PrashanthNair
New Member
- Joined
- Jun 10, 2010
- Messages
- 21
Hi guys,
I am having trouble with my code. From the code shown, the "mypath" constant is embedded into the vba while reading for the file location. i tried representing the location in spreadsheet but the mypath constant could not retrieve the location. Every time i want 2 open a file,i have to manually change the file location and also the file name.
I am having trouble with my code. From the code shown, the "mypath" constant is embedded into the vba while reading for the file location. i tried representing the location in spreadsheet but the mypath constant could not retrieve the location. Every time i want 2 open a file,i have to manually change the file location and also the file name.
Code:
Option Explicit
'---------------------
'----- EDIT THIS -----
'---------------------
Const myPath = "U:\Prashanth\Stereoplot Tool\tool\"
Const delim = vbTab
Sub MergeMultipleTextFiles()
Dim myFile As String 'filename
Dim strRecord As String 'record string
Dim arrRecord() 'record array
Dim fNum As Integer 'free file number
Dim RowCounter As Long 'row counter
Dim i As Long 'loop variable
On Error GoTo errHandler
myFile = Dir(myPath & "Scotia-7_Dip_file.txt")
RowCounter = 0
Do While myFile <> ""
fNum = FreeFile
Open myPath & myFile For Input As #fNum
Do While Not EOF(fNum)
'read in the row into the record variable
Line Input #fNum, strRecord
'add the filename to the record variable
strRecord = myFile & delim & strRecord
RowCounter = RowCounter + 1
'resize the record array
ReDim Preserve arrRecord(1 To RowCounter)
arrRecord(RowCounter) = Split(strRecord, delim)
Loop
Close #fNum
'get the next file in the directory
myFile = Dir()
Loop
'
'------------------
'----- OUTPUT -----
'------------------
With ThisWorkbook.Sheets("Input_Data").Range("A1")
For i = 1 To RowCounter
.Offset(i - 1).Resize(, UBound(arrRecord(i)) + 1).Value = arrRecord(i)
Next i
End With
errExit:
'close all open text files
Reset
Exit Sub
errHandler:
'error message goes here
Resume errExit
End Sub