View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Larry Larry is offline
external usenet poster
 
Posts: 159
Default Merging Files in ListView

Running the module it is debugging to the line "If Dir(SrcName) < "" Then"
then it exits. and the only thing created is an empty file.
--
Larry E. Brueshaber


"Tom Ogilvy" wrote:

Sub AppendFiles1()

Dim SourceNum As Long
Dim DestNum As Long
Dim Temp As String
Dim SrcName As String
Dim lbox As MSForms.ListBox
Dim i As Long

' If an error occurs, close the files and end the macro.
On Error GoTo ErrHandler

' Open the destination text file.
DestNum = FreeFile()
Open "E:\AA_Text\AAA_DEST.TXT" For Append As DestNum

Set lbox = Userform1.ListBox1 '<=== sample correction

For i = 0 To lbox.ListCount - 1
SrcName = lbox.List(i)

If Dir(SrcName) < "" Then
' Open the source text file.
SourceNum = FreeFile()
Open SrcName For Input As SourceNum


' Read each line of the source file and append it to the
' destination file.
Do While Not EOF(SourceNum)
Line Input #SourceNum, Temp
Print #DestNum, Temp
Loop

CloseFile:

' Close the source file.
Close #SourceNum

End If ' Dir(srcName) < ""
Next i

Close #DestNum
Exit Sub

ErrHandler:
MsgBox "Error # " & Err & ": " & Error(Err) & " " & SrcName
Resume CloseFile

End Sub


--
Regards,
Tom Ogilvy


"Larry" wrote in message
...
I understand the problem now. The problem is I am on a Userform instead of
an
active worksheet. Sorry about the mis-understanding.
--
Larry E. Brueshaber


"Tom Ogilvy" wrote:

Here is an example from the immediate window demonstrating it working
perfectly

set lbox = Activesheet.Listbox1
? lbox.name
ListBox1
? typename(lbox)
ListBox


There was an issue with two variables being reversed - but you haven't
reached that line yet, so no bearing on the above.

Anyway here is a tested revision. It of course assume you a have a
listbox
from the control toolbox toolbar on the activesheet when you run the
macro
and the listbox has a list of fully qualified text file names.

Sub AppendFiles1()

Dim SourceNum As Long
Dim DestNum As Long
Dim Temp As String
Dim SrcName As String
Dim lbox As MSForms.ListBox
Dim i As Long

' If an error occurs, close the files and end the macro.
On Error GoTo ErrHandler

' Open the destination text file.
DestNum = FreeFile()
Open "E:\AA_Text\AAA_DEST.TXT" For Append As DestNum

Set lbox = ActiveSheet.ListBox1

For i = 0 To lbox.ListCount - 1
SrcName = lbox.List(i)

If Dir(SrcName) < "" Then
' Open the source text file.
SourceNum = FreeFile()
Open SrcName For Input As SourceNum


' Read each line of the source file and append it to the
' destination file.
Do While Not EOF(SourceNum)
Line Input #SourceNum, Temp
Print #DestNum, Temp
Loop

CloseFile:

' Close the source file.
Close #SourceNum

End If ' Dir(srcName) < ""
Next i

Close #DestNum
Exit Sub

ErrHandler:
MsgBox "Error # " & Err & ": " & Error(Err) & " " & SrcName
Resume CloseFile

End Sub


--
Regards,
Tom Ogilvy


"Larry" wrote in message
...
Entering in the Code, when running the module the following error
"Object
doesn't support this property or method" appears in line "set lbox =
activesheet.listbox1". Thank you for your continues support.
--
Larry E. Brueshaber


"Tom Ogilvy" wrote:

Assumes the Listbox is from the control toolbox toolbar and is on the
activesheet.

Sub AppendFiles1()

Dim SourceNum As Integer
Dim DestNum As Integer
Dim Temp As String
Dim SrcName as String
Dim lbox as MSForms.Listbox

' If an error occurs, close the files and end the macro.
On Error GoTo ErrHandler

' Open the destination text file.
DestNum = FreeFile()
Open "C:\DEST.TXT" For Append As DestNum

set lbox = ActiveSheet.Listbox1

for i = 0 to lbox.Listcount - 1
SrcName = lbox.List(i)

if Dir(srcName) < "" then
' Open the source text file.
SourceNum = FreeFile()
Open srcName For Input As SourceNum


' Read each line of the source file and append it to the
' destination file.
Do While Not EOF(SourceNum)
Line Input #SourceNum, Temp
Print #DestNum, Temp
Loop

CloseFile:

' Close the source file.
Close #DestNum

End if ' Dir(srcName) < ""
Next i

Close #SourceNum
Exit Sub

ErrHandler:
MsgBox "Error # " & Err & ": " & Error(Err) & " " & srcName
Resume CloseFile

End Sub

--
Regards,
Tom Ogilvy


"Larry" wrote in message
...
the listbox has a list of fully qualified filenames for text files
which I want to combine into one large file.
--
Larry E. Brueshaber


"Tom Ogilvy" wrote:

You mean the listbox has a list of fully qualified filenames for
text
files
which you want to combine into one large file? Or have you read
each
file
into the listbox sequentially (so the data from all files is in the
listbox)
and you want to write the list back out to a textfile?

--
Regards,
Tom Ogilvy

"Larry" wrote in message
...
This site is and will be very helpful in the near future in
another
segment
of The program I am writing; right now I have a populated listbox
with
txt
files and I am trying to merge them into another text file. I
appreciate
all
the help so far.
--
Larry E. Brueshaber


"Tom Ogilvy" wrote:

http://www.rondebruin.nl/copy3.htm

--
Regards,
Tom Ogilvy


"Larry" wrote in message
...
On my Userform, I am using the listbox to populate it with
files
from a
directory. What I am trying to do is open the files and merge
them
into a
single file. I appreciate any help anyone can give me.
--
Larry E. Brueshaber