Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default loop through workbooks

Try this example
http://www.rondebruin.nl/copy3.htm

Post back if you need help



--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Rhonda" wrote in message ...
I need a macro to loop through a directory
(C:\MyDocuments) and identify files with the .bak
extension. Then open them one at a time because I need to
copy columns to a master. I found this macro, can it be
changed to my needs?

Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer

' Set WS_Count equal to the number of worksheets in
the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.
For I = 1 To WS_Count

' Insert your code here.
' The following line shows how to reference a sheet
within
' the loop by displaying the worksheet name in a
dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name

Next I

End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default loop through workbooks

Use this Rhonda

Set sourceRange = mybook.Worksheets(1).Columns("G:W")

There are only 256 columns??? in Excel


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Rhonda" wrote in message ...
Yes, the bottom macro would be great, how do I change it
to copy cols G To W from each workbook :

Copy a column or columns from each workbook

Remember Excel have only 256 columns.

Sub TestFile2()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim Colnum As Long
Dim a 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
Colnum = 1
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)
Set sourceRange = mybook.Worksheets(1).Columns(1)
a = sourceRange.Columns.Count
Set destrange = basebook.Worksheets(1).Columns
(Colnum)

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).Columns(Colnum). _
' Resize(, .Columns.Count)
'End With
'destrange.Value = sourceRange.Value

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














-----Original Message-----
Try this example
http://www.rondebruin.nl/copy3.htm

Post back if you need help



--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Rhonda" wrote in message

...
I need a macro to loop through a directory
(C:\MyDocuments) and identify files with the .bak
extension. Then open them one at a time because I need

to
copy columns to a master. I found this macro, can it be
changed to my needs?

Sub WorksheetLoop()

Dim WS_Count As Integer
Dim I As Integer

' Set WS_Count equal to the number of worksheets in
the active
' workbook.
WS_Count = ActiveWorkbook.Worksheets.Count

' Begin the loop.
For I = 1 To WS_Count

' Insert your code here.
' The following line shows how to reference a

sheet
within
' the loop by displaying the worksheet name in a
dialog box.
MsgBox ActiveWorkbook.Worksheets(I).Name

Next I

End Sub



.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find loop doesn't loop JSnow Excel Discussion (Misc queries) 2 June 24th 09 08:28 PM
Copy/ move selected data from workbooks to seperate worksheets or workbooks Positive Excel Worksheet Functions 1 August 30th 07 04:54 PM
Loop through folder of workbooks and add rows FIRSTROUNDKO via OfficeKB.com Excel Worksheet Functions 0 August 10th 06 07:50 PM
Display 2 formulas from source workbooks to destination workbooks Excel_seek_help Excel Discussion (Misc queries) 4 April 27th 06 08:13 PM
Can I have a loop to open a set of workbooks get some data, close it one a time. wellie Excel Programming 2 July 9th 03 04:58 AM


All times are GMT +1. The time now is 06:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"