PC Review


Reply
Thread Tools Rate Thread

Copy Multiple Workbooks to Worksheet

 
 
Darrell Lankford
Guest
Posts: n/a
 
      9th Mar 2007
I have used the following code thanks to Ron DeBruin and it works
great to copy the text in multiple workbooks to one sheet. The code
puts the workbook name in the cell at the header row of each sheet
range copied. How can I modify the code to add the workbook name in a
cell on every row? I tried to add a line with filldown, but that only
does the first set, and not the remaining. Any ideas?


Option Explicit

'***Copy a Range from each workbook***
'
'This two examples will copy Range("A1:C1") from the first sheet of
each workbook.
'Change the folder "C:\Data" 0r "\\ComputerName\YourFolder" to your
folder.

'Note: The second macro is also working if your files are in a network
folder.

Sub Example1()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim rnum As Long
Dim SourceRcount As Long
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String

SaveDriveDir = CurDir
MyPath = "C:\Data"
ChDrive MyPath
ChDir MyPath

FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
basebook.Worksheets(1).Cells.Clear
'clear all cells on the first sheet
rnum = 1

Do While FNames <> ""
Set mybook = Workbooks.Open(FNames)
Set sourceRange = mybook.Worksheets(1).Range("A1:C10")'
CHANGED Range("A1:C1")
SourceRcount = sourceRange.Rows.Count
Set destrange = basebook.Worksheets(1).Cells(rnum, "A")

basebook.Worksheets(1).Cells(rnum, "D").Value = mybook.Name
' This will add the workbook name in column D if you want

sourceRange.Copy destrange
' Instead of this line you can use the code below to copy only
the values

' With sourceRange
' Set destrange =
basebook.Worksheets(1).Cells(rnum, "A"). _
'
Resize(.Rows.Count, .Columns.Count)
' End With
' destrange.Value = sourceRange.Value

mybook.Close False
rnum = rnum + SourceRcount
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      9th Mar 2007
You can use this Darrell

basebook.Worksheets(1).Cells(rnum, "D").Resize(SourceRcount).Value = mybook.Name


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Darrell Lankford" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>I have used the following code thanks to Ron DeBruin and it works
> great to copy the text in multiple workbooks to one sheet. The code
> puts the workbook name in the cell at the header row of each sheet
> range copied. How can I modify the code to add the workbook name in a
> cell on every row? I tried to add a line with filldown, but that only
> does the first set, and not the remaining. Any ideas?
>
>
> Option Explicit
>
> '***Copy a Range from each workbook***
> '
> 'This two examples will copy Range("A1:C1") from the first sheet of
> each workbook.
> 'Change the folder "C:\Data" 0r "\\ComputerName\YourFolder" to your
> folder.
>
> 'Note: The second macro is also working if your files are in a network
> folder.
>
> Sub Example1()
> Dim basebook As Workbook
> Dim mybook As Workbook
> Dim sourceRange As Range
> Dim destrange As Range
> Dim rnum As Long
> Dim SourceRcount As Long
> Dim FNames As String
> Dim MyPath As String
> Dim SaveDriveDir As String
>
> SaveDriveDir = CurDir
> MyPath = "C:\Data"
> ChDrive MyPath
> ChDir MyPath
>
> FNames = Dir("*.xls")
> If Len(FNames) = 0 Then
> MsgBox "No files in the Directory"
> ChDrive SaveDriveDir
> ChDir SaveDriveDir
> Exit Sub
> End If
>
> Application.ScreenUpdating = False
> Set basebook = ThisWorkbook
> basebook.Worksheets(1).Cells.Clear
> 'clear all cells on the first sheet
> rnum = 1
>
> Do While FNames <> ""
> Set mybook = Workbooks.Open(FNames)
> Set sourceRange = mybook.Worksheets(1).Range("A1:C10")'
> CHANGED Range("A1:C1")
> SourceRcount = sourceRange.Rows.Count
> Set destrange = basebook.Worksheets(1).Cells(rnum, "A")
>
> basebook.Worksheets(1).Cells(rnum, "D").Value = mybook.Name
> ' This will add the workbook name in column D if you want
>
> sourceRange.Copy destrange
> ' Instead of this line you can use the code below to copy only
> the values
>
> ' With sourceRange
> ' Set destrange =
> basebook.Worksheets(1).Cells(rnum, "A"). _
> '
> Resize(.Rows.Count, .Columns.Count)
> ' End With
> ' destrange.Value = sourceRange.Value
>
> mybook.Close False
> rnum = rnum + SourceRcount
> FNames = Dir()
> Loop
> ChDrive SaveDriveDir
> ChDir SaveDriveDir
> Application.ScreenUpdating = True
> End Sub
>

 
Reply With Quote
 
Darrell Lankford
Guest
Posts: n/a
 
      9th Mar 2007
On Mar 9, 1:22 pm, "Ron de Bruin" <rondebr...@kabelfoon.nl> wrote:
> You can use this Darrell
>
> basebook.Worksheets(1).Cells(rnum, "D").Resize(SourceRcount).Value = mybook.Name
>
> --
>
> Regards Ron de Bruinhttp://www.rondebruin.nl/tips.htm
>
>
>
> "Darrell Lankford" <darre...@earl-ind.com> wrote in messagenews:(E-Mail Removed)...
> >I have used the following code thanks to Ron DeBruin and it works
> > great to copy the text in multiple workbooks to one sheet. The code
> > puts the workbook name in the cell at the header row of each sheet
> > range copied. How can I modify the code to add the workbook name in a
> > cell on every row? I tried to add a line with filldown, but that only
> > does the first set, and not the remaining. Any ideas?

>
> > Option Explicit

>
> > '***Copy a Range from each workbook***
> > '
> > 'This two examples will copy Range("A1:C1") from the first sheet of
> > each workbook.
> > 'Change the folder "C:\Data" 0r "\\ComputerName\YourFolder" to your
> > folder.

>
> > 'Note: The second macro is also working if your files are in a network
> > folder.

>
> > Sub Example1()
> > Dim basebook As Workbook
> > Dim mybook As Workbook
> > Dim sourceRange As Range
> > Dim destrange As Range
> > Dim rnum As Long
> > Dim SourceRcount As Long
> > Dim FNames As String
> > Dim MyPath As String
> > Dim SaveDriveDir As String

>
> > SaveDriveDir = CurDir
> > MyPath = "C:\Data"
> > ChDrive MyPath
> > ChDir MyPath

>
> > FNames = Dir("*.xls")
> > If Len(FNames) = 0 Then
> > MsgBox "No files in the Directory"
> > ChDrive SaveDriveDir
> > ChDir SaveDriveDir
> > Exit Sub
> > End If

>
> > Application.ScreenUpdating = False
> > Set basebook = ThisWorkbook
> > basebook.Worksheets(1).Cells.Clear
> > 'clear all cells on the first sheet
> > rnum = 1

>
> > Do While FNames <> ""
> > Set mybook = Workbooks.Open(FNames)
> > Set sourceRange = mybook.Worksheets(1).Range("A1:C10")'
> > CHANGED Range("A1:C1")
> > SourceRcount = sourceRange.Rows.Count
> > Set destrange = basebook.Worksheets(1).Cells(rnum, "A")

>
> > basebook.Worksheets(1).Cells(rnum, "D").Value = mybook.Name
> > ' This will add the workbook name in column D if you want

>
> > sourceRange.Copy destrange
> > ' Instead of this line you can use the code below to copy only
> > the values

>
> > ' With sourceRange
> > ' Set destrange =
> > basebook.Worksheets(1).Cells(rnum, "A"). _
> > '
> > Resize(.Rows.Count, .Columns.Count)
> > ' End With
> > ' destrange.Value = sourceRange.Value

>
> > mybook.Close False
> > rnum = rnum + SourceRcount
> > FNames = Dir()
> > Loop
> > ChDrive SaveDriveDir
> > ChDir SaveDriveDir
> > Application.ScreenUpdating = True
> > End Sub- Hide quoted text -

>
> - Show quoted text -


Ron,

That worked great!!

Thanks,
Darrell

 
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
Create Workbooks and Copy Template Worksheet to those Workbooks K Microsoft Excel Programming 13 26th Jul 2009 09:29 AM
Copy data from large worksheet into multiple workbooks richzip Microsoft Excel Programming 15 27th Feb 2008 12:33 AM
copy data from many workbooks to one worksheet =?Utf-8?B?bWF0dHkgcmF0YWZhaXJ5?= Microsoft Access 1 30th Jun 2007 08:40 PM
macro: copy multiple workbooks to multiple tabs in single book =?Utf-8?B?TWljaGFlbA==?= Microsoft Excel Programming 0 14th Jul 2006 04:53 PM
Copy from multiple workbooks and display the original worksheet na =?Utf-8?B?QmFyYiBSZWluaGFyZHQ=?= Microsoft Excel Misc 1 5th Oct 2005 03:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:29 AM.