Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Filename Variable?

I have 52 files that I want to open and copy 10 cells from each file and
paste them into a new spreadsheet. I plan to use the code below but I want
to add a For Next loop. The only part of the filename that changes is the
number after Marty. Example Marty23, Marty24, Marty25..... What syntax do I
use to make the number after Marty a variable?


Workbooks.Open(Filename:= _
"D:\Downloads\Tomahawk\2003\Tomahawk Invoice
(Marty23).xls").RunAutoMacros _
Which:=xlAutoOpen
Range("E18:J27").Select
Selection.Copy
Windows("Summary.xls").Activate
ActiveSheet.Paste
Range("A21").Select
Windows("Tomahawk Invoice (Marty23).xls").Activate
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
ActiveWorkbook.Close


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Filename Variable?

Hi,


Try this:

Dim FileNo as integer

For FileNo = 23 to 74

Workbooks.Open(Filename:= _
"D:\Downloads\Tomahawk\2003\Tomahawk Invoice
(Marty" & FileNo & ").xls").RunAutoMacros _
Which:=xlAutoOpen
Range("E18:J27").Select
Selection.Copy
Windows("Summary.xls").Activate
ActiveSheet.Paste
Range("A21").Select
Windows("Tomahawk Invoice (Marty" & FileNo & ").xls").Activate
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
ActiveWorkbook.Close

Next Fileno


HTH

"Penflex" wrote:

I have 52 files that I want to open and copy 10 cells from each file and
paste them into a new spreadsheet. I plan to use the code below but I want
to add a For Next loop. The only part of the filename that changes is the
number after Marty. Example Marty23, Marty24, Marty25..... What syntax do I
use to make the number after Marty a variable?


Workbooks.Open(Filename:= _
"D:\Downloads\Tomahawk\2003\Tomahawk Invoice
(Marty23).xls").RunAutoMacros _
Which:=xlAutoOpen
Range("E18:J27").Select
Selection.Copy
Windows("Summary.xls").Activate
ActiveSheet.Paste
Range("A21").Select
Windows("Tomahawk Invoice (Marty23).xls").Activate
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
ActiveWorkbook.Close



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Filename Variable?

Const sFile as string = _
"D:\Downloads\Tomahawk\2003\Tomahawk Invoice (Marty"
Dim oWb as workbook

For i 1 To 52
Workbooks.Open(Filename:= sFile & i & ").xls").RunAutoMacros _
Which:=xlAutoOpen
Set oWb = Activeworkbook
Range("E18:J27").Copy
Windows("Summary.xls").Activate
ActiveSheet.Paste
Range("A21").Select
oWb.Activate
oWb.RunAutoMacros Which:=xlAutoClose
oWb.Close
Next i



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Penflex" wrote in message
...
I have 52 files that I want to open and copy 10 cells from each file and
paste them into a new spreadsheet. I plan to use the code below but I want
to add a For Next loop. The only part of the filename that changes is the
number after Marty. Example Marty23, Marty24, Marty25..... What syntax do

I
use to make the number after Marty a variable?


Workbooks.Open(Filename:= _
"D:\Downloads\Tomahawk\2003\Tomahawk Invoice
(Marty23).xls").RunAutoMacros _
Which:=xlAutoOpen
Range("E18:J27").Select
Selection.Copy
Windows("Summary.xls").Activate
ActiveSheet.Paste
Range("A21").Select
Windows("Tomahawk Invoice (Marty23).xls").Activate
ActiveWorkbook.RunAutoMacros Which:=xlAutoClose
ActiveWorkbook.Close




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
Nested String Filename Variable ed9213 Excel Worksheet Functions 9 February 12th 07 02:27 AM
Put Filename Variable in Macro David Excel Discussion (Misc queries) 4 December 27th 05 06:27 PM
Store the Spreadsheets filename in a variable? Mike Hanby Excel Programming 2 March 17th 05 11:29 PM
counting cells and using a variable filename?!?! solo_razor[_10_] Excel Programming 1 September 22nd 03 12:55 PM
Variable Filename/Worksheet/Workbook bw Excel Programming 2 July 19th 03 07:41 PM


All times are GMT +1. The time now is 04:27 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"