Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 504
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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



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
Creating new worksheets and appending data from multiple worksheets. Dow Excel Programming 5 March 11th 08 07:04 PM
Compare Rows on different Worksheets and Output Difference's to other Worksheets. [email protected] Excel Programming 3 September 19th 07 04:48 PM
How use info in Excel shared worksheets to create new worksheets dkc Excel Worksheet Functions 0 June 28th 07 08:36 PM
VBA / Macro for creating new worksheets and new columns from existing worksheets webby2006 Excel Programming 3 July 25th 06 03:38 PM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


All times are GMT +1. The time now is 08:13 AM.

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"