RE: Open file, Copy a column and paste this into a new Excel doc - Pl

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,


This should get you started. It all assumes ALL files in your directory
are to be processed. Code is assumed to be in in your output file. Change wb
to refer to output workbook if this is required.

Sub CopyB4()

Dim wb As Workbook, outrng As Range, Storeb4()

Set wb = ThisWorkbook ' <=== Change if output workbook is not this one


Set fs = Application.FileSearch

Inpath = "C:\Documents and Settings\John Topley\My Documents\Test Files\" '
<=== Change

With fs
Filescount = 0

.LookIn = Inpath
.Filename = "*.xls"
.Execute

If .FoundFiles.Count > 0 Then
nf = .FoundFiles.Count
ReDim Storeb4(1 To 1, 1 To .FoundFiles.Count) ' assumes all files
to be processed
For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i)
Storeb4(1, i) = Range("B4") ' Store B$ data
Workbooks(FileNameOnly(.FoundFiles(i))).Close SaveChanges:=False
Next i

End If
End With
' Write date to ouput file
wb.Worksheets("Sheet1").Range("a1:a" & nf) =
Application.Transpose(Storeb4)

End Sub
Function FileNameOnly(pname) As String
' Returns the filename from a path/filename string
Dim i As Integer, length As Integer, temp As String
length = Len(pname)
temp = ""
For i = length To 1 Step -1
If Mid(pname, i, 1) = Application.PathSeparator Then
FileNameOnly = temp
Exit Function
End If
temp = Mid(pname, i, 1) & temp
Next i
FileNameOnly = pname
End Function


HTH

"Harish Mohanbabu" wrote:

> Hi,
>
> There are around 700 files in a particular directory. This directory is
> mapped to my drive (F:\)
>
> What I want to do is -
>
> 1) Open the files one by one in the above directory
> 2) Copy just a particular column (B4) alone in all those files
> 3) And paste them all into a single Excel file
>
> These files are in XML format, but could be opened with Excel.
>
> Can some one help me please.
>
> Thanks,
>
> Harish Mohanbabu
> MBS Axapta - MVP
> http://www.harishm.com/
.



Relevant Pages

  • Re: glob in specific directory
    ... > I am writing a DTS ... I have to use full path names because I don't run the perl ... It's a user supplied string run by the SQL server...did you remember ... > The output file is created in $mydirectory with no problem, ...
    (comp.lang.perl.misc)
  • Re: search and select from a textfile
    ... Create an output file. ... Create a stringlist to use as a buffer. ... Check the current line for the trigger search string, ... We are at the end of a section, now check if the foundit flag is set. ...
    (comp.lang.pascal.delphi.misc)
  • Perl script to search and replace a string in a file
    ... replaces the string in the input file with some other string and writes out ... the output file. ... I want to change the script to traverse through a directory of files ...
    (perl.beginners)
  • Re: Evaluate string expression in if statement?
    ... Richard Heathfield wrote: ... > to write a program containing the string and any necessary program ... > read B's output file to find the result. ... you can't use the system command ...
    (comp.lang.c)