Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Loop - for each sheet not working?

I want a value to be written to same cell of all the sheets. I have
following macro written.
===========
Sub All_Sheets_Loop()
Dim i As Integer
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
End Sub
===========
Above macro writes only in the active sheet and other sheets are not
written by the value.
If I add a line activate.sheet it gives error.
Any thoughts pl?
Shetty
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Loop - for each sheet not working?

ActiveSheet.Range("A1").Value = 10
Try
sh.Range("A1").Value = 10

"Shetty" wrote in message
om...
For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
Above macro writes only in the active sheet and other sheets are not
written by the value.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Loop - for each sheet not working?

Shetty

Try this

Sub All_Sheets_Loop()
Dim i As Integer
Dim sh
i = Worksheets.Count
For sh = 1 To i
Sheets(sh).Range("A1").Value = 10
Next
End Sub

Regards
Peter
-----Original Message-----
I want a value to be written to same cell of all the

sheets. I have
following macro written.
===========
Sub All_Sheets_Loop()
Dim i As Integer
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
End Sub
===========
Above macro writes only in the active sheet and other

sheets are not
written by the value.
If I add a line activate.sheet it gives error.
Any thoughts pl?
Shetty
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Loop - for each sheet not working?

Explanation

For Each sh in Worksheets
does not activate the sheet, that is why you cannot use ActiveSheet. It just
sets the sh object at the next sheet in the collection, thus you reference
the sheet through this object.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Sriram N A" wrote in message
...
ActiveSheet.Range("A1").Value = 10

Try
sh.Range("A1").Value = 10

"Shetty" wrote in message
om...
For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
Above macro writes only in the active sheet and other sheets are not
written by the value.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Loop - for each sheet not working?


Sub All_Sheets_Loop()
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
sh.Range("A1").Value = 10
Next

End Sub

'sh' is the worksheet that you want to work on. No need
for it to be the active sheet, and in your code,
activesheet refers to the er, active sheet only.

Patrick Molloy
Microsoft Excel MVP


-----Original Message-----
I want a value to be written to same cell of all the

sheets. I have
following macro written.
===========
Sub All_Sheets_Loop()
Dim i As Integer
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
End Sub
===========
Above macro writes only in the active sheet and other

sheets are not
written by the value.
If I add a line activate.sheet it gives error.
Any thoughts pl?
Shetty
.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Loop - for each sheet not working?

I just created this using the macro recorder to select all sheets.
Just change one line as shown by removinging the one with the comment
Sub Macro1()
' Macro recorded 1/13/2004 by Don Guillett

' Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
sheets.select ' selects all instead of array above

Sheets("Sheet1").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = "3"
Sheets("Sheet2").Select
End Sub

--
Don Guillett
SalesAid Software

"Shetty" wrote in message
om...
I want a value to be written to same cell of all the sheets. I have
following macro written.
===========
Sub All_Sheets_Loop()
Dim i As Integer
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
End Sub
===========
Above macro writes only in the active sheet and other sheets are not
written by the value.
If I add a line activate.sheet it gives error.
Any thoughts pl?
Shetty



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 78
Default Loop - for each sheet not working?

It worked great. Thanks to all of you for your help.

Regards,



"Don Guillett" wrote in message ...
I just created this using the macro recorder to select all sheets.
Just change one line as shown by removinging the one with the comment
Sub Macro1()
' Macro recorded 1/13/2004 by Don Guillett

' Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5")).Select
sheets.select ' selects all instead of array above

Sheets("Sheet1").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = "3"
Sheets("Sheet2").Select
End Sub

--
Don Guillett
SalesAid Software

"Shetty" wrote in message
om...
I want a value to be written to same cell of all the sheets. I have
following macro written.
===========
Sub All_Sheets_Loop()
Dim i As Integer
Dim sh As Worksheet

For Each sh In ThisWorkbook.Worksheets
ActiveSheet.Range("A1").Value = 10
Next
End Sub
===========
Above macro writes only in the active sheet and other sheets are not
written by the value.
If I add a line activate.sheet it gives error.
Any thoughts pl?
Shetty

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
Loop all Sheets not working. Pank New Users to Excel 12 February 27th 07 11:55 AM
On Error GoTo Label in a loop only working once. Ken Johnson Charts and Charting in Excel 4 July 5th 06 09:39 PM
Another loop & sheet question steve Excel Programming 4 November 25th 03 02:12 PM
Using UsedRange as limits in a For Each loop but for cells on another sheet ? tur13o Excel Programming 2 October 23rd 03 01:18 PM
Excluding sheet in loop david Excel Programming 2 July 31st 03 03:18 AM


All times are GMT +1. The time now is 08:30 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"