LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Problems Defining Object Variable in For Each Loop

Not sure why you want to minimize the number of For statements. If I
was in your shoes, my code would be structured along the lines of:

Option Explicit

Public Type UserDiagnosticChoices
doComments As Boolean
doCells As Boolean
'...
End Type
Sub CheckCOmments(aWS As Worksheet)
'...
End Sub
Sub checkCells(aWS As Worksheet)
'...
End Sub
Sub Main()
Dim UserChoices As UserDiagnosticChoices
UserForm1.Manager UserChoices
Dim aWS As Worksheet
For Each aWS In ActiveWorkbook.Worksheets
If UserChoices.doComments Then CheckCOmments aWS
If UserChoices.doCells Then checkCells aWS
'...
Next aWS
End Sub


and Userform1 would contain:
Option Explicit

Public Sub Manager(ByRef UserChoices As UserDiagnosticChoices)
UserForm1.Show
'if user clicked ok set doComments, doCells, etc.
End Sub


--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Well you see, this is how I orginally built it. But I am
running a diagnostic on a spreadsheet which reviews cells
commenets etc. I have a user form which where I click off
which type of diagnostic I want to run. The different
types of diagnostics (7 -10) are on comments or cells. I
was originally using your methodology but decided that
insted of using mulitple loops within my sheet loop

For each sh in Workbook
'Loop1
For each comment in sh.comments
Code
Next
'Loop2
For each cell in sh.Worksheet.UsedRange
code
Next

More loops etc
Next

I would only use 1 Loop. But in doing that, I need to
know ahead of time which collection I need prior to going
into the loop. Therefore, I passed the collection type to
the array at the beginning of the routine based on what
checkboxes I checked off.

For each sh in Workbook
For each CurObj in ObjCollArray(variable)
Code
Next
Next

As I am only using 1 For Each loop within the sheet loop,
I can only do what you are suggesting if I know ahead of
time what time of Object and Collection I need, and I also
need to somehow pass these to the two variables I created
CurObj and ObjCollArray.

Am I missing something here?

Thanks for your patience



-----Original Message-----
Inside the loop whereever you want to refer to the

comments (or
usedrange) for the sheet associated with the loop, use

sh.Comments (or
sh.UsedRange). Now, you can safely throw away

ObjCollArray.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I think my problem is that my array says:

Private Sub OKButton_Click()
Set ObjCollArray(0) = ActiveSheet.Comments
Set ObjCollArray(1) = ActiveSheet.UsedRange
Set ObjCollArray(2) = ActiveSheet.UsedRange
Set ObjCollArray(3) = ActiveSheet.UsedRange
Set ObjCollArray(4) = ActiveSheet.UsedRange
Set ObjCollArray(5) = ActiveSheet.UsedRange

I then reference these within a For Each sheet Loop

which
loops through specific sheets. I am not sure how to

set
this up differently do that it works.



{snip}
.


 
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
Defining Range of For Each Loop for Cells and Worksheets ExcelMonkey[_190_] Excel Programming 2 February 22nd 05 01:04 PM
Defining a variable within a sub... aking1987[_2_] Excel Programming 1 November 11th 04 12:32 PM
Defining a variable within a sub... aking1987 Excel Programming 1 November 11th 04 11:00 AM
Defining a discontiguous Range object Charley Kyd[_2_] Excel Programming 15 December 30th 03 12:33 AM
Defining a Variable Stephen[_3_] Excel Programming 1 September 8th 03 11:33 PM


All times are GMT +1. The time now is 01:50 PM.

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"