LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 60
Default simplifying repetitive routines in VBA

Hi guys,

I have a code to copy a (varying) range from one file to 25 other files. For
the time being I use this (all the necessary files are already opened):

-----------------------
Sub Repetitive_macro()

'1st loop
Windows("Source File.xls").Activate = this is always the same
Sheets("SourceSheet").Select = this is always the same
Range("B4").Select = this part is varying, starting with
B4, then at next loop C4, then D4,... until Z4
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Windows("1st Destination.xls").Activate = this part is also
varying, taking 25 different open files one after another and pasting what
was copied before... (file names follow no special pattern)
Range("C19").Select = this is always the same
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Save
ActiveWorkbook.Close

'2nd loop
Windows("Source File.xls").Activate
Sheets("SourceSheet").Select
Range("C4").Select

.... etc, following 25 times the scheme explained above

End Sub()
------------------------

So, my question is: Can you specify all the varying parts somewhere at the
beginning of the code (in array or whatsoever) and then put it as a variable
in the code, changing at every loop?

I would imagine something like this (pseudo-code)
--------------
Sub More_efficient_macro()

x = (B4,D4,E4,F4,G4,H4,...,Z4)
y = ("1st Destination.xls","Other Name.xls","Another Different
Name.xls",...,"Name of the last file.xls")

For every x and y

Loop
Windows("Source File.xls").Activate
Sheets("SourceSheet").Select
Range("x").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

Windows("y").Activate
Range("C19").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
ActiveWorkbook.Save
ActiveWorkbook.Close
End Loop

End Sub()
---------------

Thanks for any help on this,
Cheers,

Mark


 
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
VBA - sub routines Help please Richard Wrigley New Users to Excel 3 November 23rd 06 03:06 AM
VBA routines - help please Richard Wrigley Excel Discussion (Misc queries) 1 November 22nd 06 07:15 PM
Sorting Routines ADG Excel Programming 3 May 30th 06 10:30 AM
Removing VBA routines Robin Clay[_2_] Excel Programming 2 October 14th 03 05:44 PM
api call routines anthony Excel Programming 2 July 21st 03 02:00 PM


All times are GMT +1. The time now is 12:15 PM.

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

About Us

"It's about Microsoft Excel"