Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping thru Sheets without activating


I have a code that loops through 56 Worksheets and on each sheet loops
through the used cells in Column A. Does anyone have a way to execute
the two-phase looping without activating the Worksheets? Following the
line of least resistance, and at the expense of speed, I have:

For each sh in WorkSheets
sh.activate
For each c in Range("a:a")
If Not IsEmpty(c) Then

<Do something

End if
Next
Next


I reckon that the code could be vastly speeded up if it didn't have to
select each sheet.

TIA.

Myles.


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Looping thru Sheets without activating

Hi Myles,

Try:

Public Sub Tester03()
Dim SH As Worksheet
Dim c As Range
Dim CalcMode As Long

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each SH In Worksheets
For Each c In SH.Range("a:a")
If Not IsEmpty(c) Then
'<Do something
End If
Next c
Next SH

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

End Sub
'<<=============


---
Regards,
Norman


"Myles" wrote in
message ...

I have a code that loops through 56 Worksheets and on each sheet loops
through the used cells in Column A. Does anyone have a way to execute
the two-phase looping without activating the Worksheets? Following the
line of least resistance, and at the expense of speed, I have:

For each sh in WorkSheets
sh.activate
For each c in Range("a:a")
If Not IsEmpty(c) Then

<Do something

End if
Next
Next


I reckon that the code could be vastly speeded up if it didn't have to
select each sheet.

TIA.

Myles.


--
Myles
------------------------------------------------------------------------
Myles's Profile:
http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping thru Sheets without activating


Hi,

I hope, it would help you out. Instead of using collection, you could
use a simple loop. Code could be used as follows:

For x = 1 To Sheets.Count
If Sheets(x).Range("___") = "Validation" Then

your code here...

End If
Next


--
gajendra_vba
------------------------------------------------------------------------
gajendra_vba's Profile: http://www.excelforum.com/member.php...o&userid=29935
View this thread: http://www.excelforum.com/showthread...hreadid=496613

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping thru Sheets without activating


Hi Norman,

Many hanks. Your code does the trick!

I had tried something similar but yours has exposed my mistake. For the
<do something segment of the code, I had a Call statement which
invoked another subroutine . And since this subroutine didn't have
range references qualified by the sheet reference, SH in this case, I
ran into strife as only the activesheet was worked upon. To get round
the problem, I have now abandoned the Call idea and embedded the
subroutine in the main code with all range references duly tacked. This
makes the end-product code very unwieldy but I suppose that is the price
to pay for enhanced speed.

In any event, if you know of a way I can use the Call idea -without-
-activating -the individual sheets, I would welcome it. I flirted with
declaring SH global-to no avail, apparently because SH is an object not
a variable.


Myles


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping thru Sheets without activating


Another one that failed ...
Passing the current sheet as an argument, as in:

Sub LoopThruShtsIn2Phases()
Dim sh as Worksheet

For each sh in worksheets
Call DoSomething(sh)
Next

End Sub

Sub DoSomething(obSht) *'returns Object doesn't support this property
or method *error
Dim c as range

For each c in obSht.Range("a:a")
Is Not Isempty(c) Then

<CarryOutSomeActions

End if
Next
End Sub


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Looping thru Sheets without activating

Hi Myles,

'=============
Public Sub Tester03()
Dim SH As Worksheet
Dim c As Range
Dim CalcMode As Long

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each SH In Worksheets
For Each c In SH.Range("A1:A20") '<<==== CHANGE
If Not IsEmpty(c) Then
Call MyMacro(c) '<<==== CHANGE
End If
Next c
Next SH

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

End Sub
'<<=============

'=============
Sub MyMacro(rng As Range)
rng.Interior.ColorIndex = 6
End Sub
'<<=============

--
---
Regards,
Norman



"Myles" wrote in
message ...

Another one that failed ...
Passing the current sheet as an argument, as in:

Sub LoopThruShtsIn2Phases()
Dim sh as Worksheet

For each sh in worksheets
Call DoSomething(sh)
Next

End Sub

Sub DoSomething(obSht) *'returns Object doesn't support this property
or method *error
Dim c as range

For each c in obSht.Range("a:a")
Is Not Isempty(c) Then

<CarryOutSomeActions

End if
Next
End Sub


--
Myles
------------------------------------------------------------------------
Myles's Profile:
http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping thru Sheets without activating


Hi Norman,

Many Thanks again. Great solution!

I found it a bit perplexing though why passing the cell range (c in
the example) as an argument works hitch-free but passing a sheet object
(sh as in my example) throws up error message.

If there is an underlying reason why this is so it would be interesting
to know.

Myles


--
Myles
------------------------------------------------------------------------
Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746
View this thread: http://www.excelforum.com/showthread...hreadid=496613

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
Activating sheets with same name in multiple workbooks Barb Reinhardt Excel Programming 4 July 26th 05 05:19 PM
Looping through Sheets Jim Thomlinson[_3_] Excel Programming 3 September 25th 04 03:48 AM
Looping through Sheets dalejrstwin Excel Programming 0 September 24th 04 06:43 PM
Looping through sheets Eric[_7_] Excel Programming 1 July 24th 03 04:45 PM


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