Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi
I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As ALWAYS,post YOUR code for comments and suggestions.
for each ws in thisworkbook.worksheets if ws. name="firstone" or ws.name="secondone" then ws.do your thing end if next ws. -- Don Guillett Microsoft MVP Excel SalesAid Software "Kevin" wrote in message ... Hi I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Maybe something like this
Sub eachSheet() Dim ws As Worksheet For Each ws In Worksheets ws.Range("A1").Value = "Do Your Code Here" Next End Sub "Kevin" wrote: Hi I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Kevin;127070 Wrote: Hi I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin Sub eachSheet() Dim ws As Worksheet For Each ws In Worksheets ws.Range("A1").Value = "Do Your Code Here" Next End Sub -- mikeaj72 ------------------------------------------------------------------------ mikeaj72's Profile: http://www.thecodecage.com/forumz/member.php?userid=46 View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=35248 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Something like this maybe...
SheetNames = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4") For Each SN in SheetNames With Worksheets(SN) ' ' Do whatever you need to here. Remember to put a dot in front of ' each property/method call, such as .Range("A1"), so that it will ' refer to the worksheet that is the object of the With statement ' End With Next where you would substitute your actual sheet names for the example ones I used in the Array function call (remember to encase them in quote marks). -- Rick (MVP - Excel) "Kevin" wrote in message ... Hi I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I should point out, my code would be most useful if there were more than 4
worksheets in the workbook or if there could be more than 4 worksheets at some point in the future. Otherwise, just iterate all the sheet as others have posted. -- Rick (MVP - Excel) "Rick Rothstein" wrote in message ... Something like this maybe... SheetNames = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4") For Each SN in SheetNames With Worksheets(SN) ' ' Do whatever you need to here. Remember to put a dot in front of ' each property/method call, such as .Range("A1"), so that it will ' refer to the worksheet that is the object of the With statement ' End With Next where you would substitute your actual sheet names for the example ones I used in the Array function call (remember to encase them in quote marks). -- Rick (MVP - Excel) "Kevin" wrote in message ... Hi I have written some code that works ok. I need to do the same function on 4 different worksheets (the macro updates the 4 sheets in one go). What is a simple way to write some code so it replicates the same function in the 4 sheets without entering the code 4 times (referring to a each sheet) -- Kevin |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Creating new worksheets and appending data from multiple worksheets. | Excel Programming | |||
Compare Rows on different Worksheets and Output Difference's to other Worksheets. | Excel Programming | |||
How use info in Excel shared worksheets to create new worksheets | Excel Worksheet Functions | |||
VBA / Macro for creating new worksheets and new columns from existing worksheets | Excel Programming | |||
Need code to protect worksheets - amount of worksheets varies | Excel Programming |