ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need to perform the same macro to multiple xls files in VB2005 Exp (https://www.excelbanter.com/excel-programming/349154-need-perform-same-macro-multiple-xls-files-vb2005-exp.html)

Jhcorsair

Need to perform the same macro to multiple xls files in VB2005 Exp
 
How do I "loop" through multiple files in a folder to perform the same task
to each file?

voodooJoe

Need to perform the same macro to multiple xls files in VB2005 Exp
 
try something like

Sub x()

strFolder = "c:\"

If Right(strFolder, 1) < "\" Then strFolder = strFolder & "\"
f = Dir(strFolder & "*.xls")

Do While f < ""
Set wb = Workbooks.Open(strFolder & f)
Debug.Print wb.Worksheets(1).Name 'do stuff - wb will be the
workbook object
wb.Close
f = Dir()
Loop

Set wb = Nothing
End Sub

don't forget to turn off screen updating

rgds - voodooJoe


"Jhcorsair" wrote in message
...
How do I "loop" through multiple files in a folder to perform the same
task
to each file?




voodooJoe

Need to perform the same macro to multiple xls files in VB2005 Exp
 
try something like

Sub x()
strFolder = "c:\"
strFileType = "xls"

If Right(strFolder, 1) < "\" Then strFolder = strFolder & "\"
f = Dir(strFolder & "*." & strFileType)

Do While f < ""
Set wb = Workbooks.Open(strFolder & f)
Debug.Print wb.Worksheets(1).Name 'do stuff - wb will be the
workbook object
wb.Close
f = Dir()
Loop

Set wb = Nothing
End Sub

remember to turn off screen updating

rgds- voodooJoe


"Jhcorsair" wrote in message
...
How do I "loop" through multiple files in a folder to perform the same
task
to each file?





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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com