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: 2
Default Help on writing code more efficiently (Loops)

Below is an example of code I wrote to copy data input in
one file (the active file) to other files. I only show
one file being updated, but I have other routines that
update additional files. Could someone show me how to
use variables & loops to condense the code and make it
more efficient e.g., method of defining and looping
through specific sheets in a workbook and performing a
series of actions (not all sheets in the workbooked
should be updated & some are updated in different
columns)?

Any input on this or any other type of suggestions would
be appreciated. Kathryn



//////////////////

Option Explicit

' Set string to ID path and name of support files
' (note: "AnotherFile" is not used in the example
' - I just put it in to show that the routine
' will be updating several other files).
Global path_Psn As String, file_Psn As String
Global file_AnotherFile As String

' Set string to ID main file; file provides data&date
Global Myfile As String

' Set string/date to ID amounts$ copied to other files
Global pr_item1 As String, pr_item2 As String
Global pr_item3 As String, pr_item4 As String
Global pd_item1 As String, pd_item2 As String
Global pd_item3 As String, pd_item4 As String
Global pr_item5 As String, pd_item5 As String
Global MyDate As Date

Sub MyRoutine()
Call GetVariableInfo
Call UpdateFile1
' Call UpdateFile2 ' Not used in example
End Sub

Sub GetVariableInfo()

' Define variables
path_Psn = "c:\Documents and Settings\"
file_Psn = "Position.xls"
file_AnotherFile = "My Second File.xls"
Myfile = ActiveWorkbook.name

' Defined by range names in worksheet
Windows(Myfile).Activate
pr_item1 = -Range("pr_item1").Value
pr_item2 = -Range("pr_item2").Value
pr_item3 = -Range("pr_item3").Value
pr_item4 = -Range("pr_item4").Value
pd_item1 = Range("pd_item1").Value
pd_item2 = Range("pd_item2").Value
pd_item3 = Range("pd_item3").Value
pd_item4 = Range("pd_item4").Value
pr_item5 = Range("pr_item5").Value
pd_item5 = -Range("pd_item5").Value
MyDate = Range("b2").Value
End Sub

Sub UpdateFile1()
Workbooks.Open Filename:=path_Psn & file_Psn, _
UpdateLinks:=False
Sheets(2).Select
Cells.Find(What:=MyDate, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(0, 2).Value = pd_item1
ActiveCell.Offset(0, 3).Value = pr_item1
Sheets(3).Select
Cells.Find(What:=MyDate, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(0, 2).Value = pd_item2
ActiveCell.Offset(0, 3).Value = pr_item2
Sheets(4).Select
Cells.Find(What:=MyDate, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(0, 2).Value = pd_item3
ActiveCell.Offset(0, 3).Value = pr_item3
Sheets(5).Select
Cells.Find(What:=MyDate, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(0, 2).Value = pd_item4
ActiveCell.Offset(0, 3).Value = pr_item4
Sheets(1).Select
Cells.Find(What:=MyDate, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Offset(0, 7).Value = pr_item5
ActiveCell.Offset(0, 8).Value = pd_item5
End Sub

///////////////
 
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
IS IT POSSIBLE IN EXCEL WITHOUT WRITING CODE. HARSHAWARDHAN. S .SHASTRI[_2_] Excel Worksheet Functions 14 February 4th 09 04:48 PM
Need help writing Code LeAnne Excel Discussion (Misc queries) 1 February 15th 08 03:27 PM
Need some help writing code. Erik[_6_] Excel Programming 5 February 29th 04 06:51 PM
Code writing:Where to start ? MIke Excel Programming 2 February 19th 04 08:46 PM
Writing a formula in code Tom Ogilvy Excel Programming 0 September 5th 03 02:32 PM


All times are GMT +1. The time now is 10:40 AM.

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"