ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   For each, Next - 4 worksheets (https://www.excelbanter.com/excel-programming/420669-each-next-4-worksheets.html)

Kevin

For each, Next - 4 worksheets
 
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

Don Guillett

For each, Next - 4 worksheets
 
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



Mike

For each, Next - 4 worksheets
 
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


mikeaj72[_5_]

For each, Next - 4 worksheets
 

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


Rick Rothstein

For each, Next - 4 worksheets
 
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



Rick Rothstein

For each, Next - 4 worksheets
 
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





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

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