PC Review


Reply
Thread Tools Rate Thread

copy content from Multiple file to one file

 
 
Pman
Guest
Posts: n/a
 
      30th Dec 2008
Hi,

I currently have a folder which contains close to 800+ excel files. I need
to copy a range of cells from each of these files to another excel file. I'm
writing a Macro to do this, however It's not working.

I was wondering if someone could go through my code and let me know where I
made a mistake.

Thanks,
Prash

***************************************************
The Code
***************************************************
Sub Macro1()

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set Folder = oFSO.GetFolder("C:\OCFs")

Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"

For Each file In Folder.Files

If file.Type Like "*Excel*" Then
Workbooks.Open Filename:=file.Path

' Unmerging and copying
'
Columns("D").Select
Selection.UnMerge
Range("D7").Select
ActiveWindow.SmallScroll Down:=-6
Range("D1").Select
ActiveCell.FormulaR1C1 = "=CELL(""filename"")"
Range("D1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("D1").Select
ActiveWindow.SmallScroll Down:=42
Range("D166").Select
Selection.Copy
Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"
Range("A65536").End(xlUp).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=True

ActiveWorkbook.Close SaveChanges:=True

End If

Next file
Set oFSO = Nothing


End Sub

***************************************************
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      30th Dec 2008
Hi,

The problem with recorded macros is they generate an awful lot of
unnecessary code and this has happened in your case. If I understand
correctly you are trying to paste D1 to D66 of each workbook into another
after first unmerging cells and putting a formula in d1. Try this instead.

It goes in the receiving workbook (Book1 in your code). Note there are 2
subs. A general one to open every excel file in a folder and the second to do
the copying.
The copied data is then paste into Book 1



Dim File As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("c:\") ' Change to suit
For Each File In Folder.Files

If File.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=Folder.Path & "\" & File.Name

'Call your macro
dothings
ActiveWorkbook.Close True
lastrow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet1").Range("A" & lastrow + 1).PasteSpecial
End If
Next
End Sub
Sub dothings()
With ActiveWorkbook.ActiveSheet
..Range("D").UnMerge
..Range("D1").Formula = "=CELL(""filename"")"
..Range("D166").Copy
End With
End Sub

Mike


"Pman" wrote:

> Hi,
>
> I currently have a folder which contains close to 800+ excel files. I need
> to copy a range of cells from each of these files to another excel file. I'm
> writing a Macro to do this, however It's not working.
>
> I was wondering if someone could go through my code and let me know where I
> made a mistake.
>
> Thanks,
> Prash
>
> ***************************************************
> The Code
> ***************************************************
> Sub Macro1()
>
> Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> Set Folder = oFSO.GetFolder("C:\OCFs")
>
> Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"
>
> For Each file In Folder.Files
>
> If file.Type Like "*Excel*" Then
> Workbooks.Open Filename:=file.Path
>
> ' Unmerging and copying
> '
> Columns("D").Select
> Selection.UnMerge
> Range("D7").Select
> ActiveWindow.SmallScroll Down:=-6
> Range("D1").Select
> ActiveCell.FormulaR1C1 = "=CELL(""filename"")"
> Range("D1").Select
> Selection.Copy
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=False
> Application.CutCopyMode = False
> Range("D1").Select
> ActiveWindow.SmallScroll Down:=42
> Range("D166").Select
> Selection.Copy
> Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"
> Range("A65536").End(xlUp).Offset(1, 0).Select
> Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> SkipBlanks _
> :=False, Transpose:=True
>
> ActiveWorkbook.Close SaveChanges:=True
>
> End If
>
> Next file
> Set oFSO = Nothing
>
>
> End Sub
>
> ***************************************************

 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      30th Dec 2008
missed some code when pasting

Sub OpenFiles()
Dim FSO As Object
Dim Folder As Object
Dim File As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("c:\") ' Change to suit
For Each File In Folder.Files

If File.Type = "Microsoft Excel Worksheet" Then
Workbooks.Open Filename:=Folder.Path & "\" & File.Name

'Call your macro
dothings
ActiveWorkbook.Close True
lastrow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Sheet1").Range("A" & lastrow + 1).PasteSpecial
End If
Next
End Sub

Sub dothings()
With ActiveWorkbook.ActiveSheet
..Range("D").UnMerge
..Range("D1").Formula = "=CELL(""filename"")"
..Range("D166").Copy
End With
End Sub


"Mike H" wrote:

> Hi,
>
> The problem with recorded macros is they generate an awful lot of
> unnecessary code and this has happened in your case. If I understand
> correctly you are trying to paste D1 to D66 of each workbook into another
> after first unmerging cells and putting a formula in d1. Try this instead.
>
> It goes in the receiving workbook (Book1 in your code). Note there are 2
> subs. A general one to open every excel file in a folder and the second to do
> the copying.
> The copied data is then paste into Book 1
>
>
>
> Dim File As Object
> Set FSO = CreateObject("Scripting.FileSystemObject")
> Set Folder = FSO.GetFolder("c:\") ' Change to suit
> For Each File In Folder.Files
>
> If File.Type = "Microsoft Excel Worksheet" Then
> Workbooks.Open Filename:=Folder.Path & "\" & File.Name
>
> 'Call your macro
> dothings
> ActiveWorkbook.Close True
> lastrow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
> Sheets("Sheet1").Range("A" & lastrow + 1).PasteSpecial
> End If
> Next
> End Sub
> Sub dothings()
> With ActiveWorkbook.ActiveSheet
> .Range("D").UnMerge
> .Range("D1").Formula = "=CELL(""filename"")"
> .Range("D166").Copy
> End With
> End Sub
>
> Mike
>
>
> "Pman" wrote:
>
> > Hi,
> >
> > I currently have a folder which contains close to 800+ excel files. I need
> > to copy a range of cells from each of these files to another excel file. I'm
> > writing a Macro to do this, however It's not working.
> >
> > I was wondering if someone could go through my code and let me know where I
> > made a mistake.
> >
> > Thanks,
> > Prash
> >
> > ***************************************************
> > The Code
> > ***************************************************
> > Sub Macro1()
> >
> > Set oFSO = CreateObject("Scripting.FileSystemObject")
> >
> > Set Folder = oFSO.GetFolder("C:\OCFs")
> >
> > Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"
> >
> > For Each file In Folder.Files
> >
> > If file.Type Like "*Excel*" Then
> > Workbooks.Open Filename:=file.Path
> >
> > ' Unmerging and copying
> > '
> > Columns("D").Select
> > Selection.UnMerge
> > Range("D7").Select
> > ActiveWindow.SmallScroll Down:=-6
> > Range("D1").Select
> > ActiveCell.FormulaR1C1 = "=CELL(""filename"")"
> > Range("D1").Select
> > Selection.Copy
> > Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> > SkipBlanks _
> > :=False, Transpose:=False
> > Application.CutCopyMode = False
> > Range("D1").Select
> > ActiveWindow.SmallScroll Down:=42
> > Range("D166").Select
> > Selection.Copy
> > Workbooks.Open Filename:="C:\OCF Destination\Book1.xls"
> > Range("A65536").End(xlUp).Offset(1, 0).Select
> > Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
> > SkipBlanks _
> > :=False, Transpose:=True
> >
> > ActiveWorkbook.Close SaveChanges:=True
> >
> > End If
> >
> > Next file
> > Set oFSO = Nothing
> >
> >
> > End Sub
> >
> > ***************************************************

 
Reply With Quote
 
Pman
Guest
Posts: n/a
 
      5th Jan 2009


"Mike H" wrote:

> Hi,
>
> The problem with recorded macros is they generate an awful lot of
> unnecessary code and this has happened in your case. If I understand
> correctly you are trying to paste D1 to D66 of each workbook into another
> after first unmerging cells and putting a formula in d1. Try this instead.
>
> It goes in the receiving workbook (Book1 in your code). Note there are 2
> subs. A general one to open every excel file in a folder and the second to do
> the copying.
> The copied data is then paste into Book 1
>
>


Thanks a lot a lot a lot Mike. What you said is true, I've unmerged cells,
put a formula in D1, and copied a range to a new file. But I knew I was
missing something since I compiled this macro using a couple of recorded
Macros.

Thanks so much again.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Cell Content to CSV file =?Utf-8?B?Sm9lIEsu?= Microsoft Excel Programming 4 9th Oct 2007 09:04 PM
Copy Content Text File in Project to a Folder or a String vjunkl@hotpop.com Microsoft Dot NET Framework 1 22nd Feb 2007 11:06 PM
Copy Content Text File in Project to a Folder or a String vjunkl@hotpop.com Microsoft C# .NET 1 22nd Feb 2007 11:06 PM
Multiple attachments Different file names, Same Content =?Utf-8?B?YzFmZXI=?= Microsoft Outlook Interoperability 1 31st Jul 2006 11:05 PM
Copy Content file to Output Directory In Vs2003 =?Utf-8?B?QmlsbHlMaXUwMDc=?= Microsoft Dot NET Framework Forms 0 23rd Nov 2005 06:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:01 AM.