Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default For each cell method, is there a much faster way?

Hi, I have a lot of records to search thru', everything works fine,
its just this section of code below is far too slow.

Is there any way to make this section of code work at sub light speed
or even light speed?

thanks for any help

Ste

Do While Not (Sheets("Meetingstodate").Cells(xlrow, 2).Value = "")

Set StartCell = Sheets("Meetingstodate").Cells(xlrow, 2)
Set EndCell = Sheets("Meetingstodate").Cells(xlrow, 7)

For Each cell In Range(StartCell, EndCell)

If cell.Value = A Then
intcounter1 = 1
End If
If cell.Value = B Then
intcounter2 = 1
End If
If cell.Value = C Then
intcounter3 = 1
End If
If cell.Value = D Then
intcounter4 = 1
End If
If cell.Value = E Then
intcounter5 = 1
End If
If cell.Value = F Then
intcounter6 = 1
End If

Next cell

If intcounter1 + intcounter2 + intcounter3 + intcounter4 + intcounter5
+ intcounter6 = 5 Then
totcounter = totcounter + 1
End If

xlrow = xlrow + 1

Loop
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default For each cell method, is there a much faster way?

Light speed is tough!

But rather than using cell.Value repeatedly, use a correctly Dimm'ed variable:
v=cell.Value

Also use a Case in place of the bunch of Ifs. That way the extra statements
don't need to be executed.
--
Gary''s Student - gsnu2007k


"ste mac" wrote:

Hi, I have a lot of records to search thru', everything works fine,
its just this section of code below is far too slow.

Is there any way to make this section of code work at sub light speed
or even light speed?

thanks for any help

Ste

Do While Not (Sheets("Meetingstodate").Cells(xlrow, 2).Value = "")

Set StartCell = Sheets("Meetingstodate").Cells(xlrow, 2)
Set EndCell = Sheets("Meetingstodate").Cells(xlrow, 7)

For Each cell In Range(StartCell, EndCell)

If cell.Value = A Then
intcounter1 = 1
End If
If cell.Value = B Then
intcounter2 = 1
End If
If cell.Value = C Then
intcounter3 = 1
End If
If cell.Value = D Then
intcounter4 = 1
End If
If cell.Value = E Then
intcounter5 = 1
End If
If cell.Value = F Then
intcounter6 = 1
End If

Next cell

If intcounter1 + intcounter2 + intcounter3 + intcounter4 + intcounter5
+ intcounter6 = 5 Then
totcounter = totcounter + 1
End If

xlrow = xlrow + 1

Loop

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default For each cell method, is there a much faster way?

Thanks Gary''s Student, can you please post an example.

Ste
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default For each cell method, is there a much faster way?

Something like:

'Dim v as whatever A,B,C,... are
v = cell.Value
Select Case v
Case A
intcounter1 = 1
Case B
intcounter2 = 1
Case C
intcounter3 = 1
Case D
intcounter4 = 1
Case E
intcounter5 = 1
Case F
intcounter6 = 1
End Select

The speed will improve a little. Like I said; light speed is tough.
--
Gary''s Student - gsnu2007k


"ste mac" wrote:

Thanks Gary''s Student, can you please post an example.

Ste

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 117
Default For each cell method, is there a much faster way?

Thanks Gary''s Student, I just tried your improvement and it it
definately quicker no doubt about it.

A welcome improvement.

I will have another trawl through this ng and see if it cab be further
improved.

Thanks alot...

Ste



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default For each cell method, is there a much faster way?

Do
set WhatFound=Range(StartCell, EndCell).Find(A, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=False)
If Not (WhatFound Is Nothing) then
'Action A
End If
Loop Until WhatFound Is Nothing
Do
set WhatFound=Range(StartCell, EndCell).Find(B, LookIn:=xlValues,
LookAt:=xlWhole, MatchCase:=False)
If Not (WhatFound Is Nothing) then
'Action B
End If
Loop Until WhatFound Is Nothing
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default For each cell method, is there a much faster way?

I don't quite understand why your totcounter counter gets incremented that
way, but in general if you want to loop values of a large number of cells,
assign the entire range.Value to an array and loop the array

arr = Range("A1:D100").Value

for r = 1 to ubound(arr)
for c = 1 to ubound(arr, 2)

select case arr(r, c)
case A: intcounter1 = 1 ' ?
etc
end select
' code
next
next

Perhaps in your case though you could use the CountIf worksheet function for
each of your A, B, C values

Regards,
Peter T


"ste mac" wrote in message
...
Hi, I have a lot of records to search thru', everything works fine,
its just this section of code below is far too slow.

Is there any way to make this section of code work at sub light speed
or even light speed?

thanks for any help

Ste

Do While Not (Sheets("Meetingstodate").Cells(xlrow, 2).Value = "")

Set StartCell = Sheets("Meetingstodate").Cells(xlrow, 2)
Set EndCell = Sheets("Meetingstodate").Cells(xlrow, 7)

For Each cell In Range(StartCell, EndCell)

If cell.Value = A Then
intcounter1 = 1
End If
If cell.Value = B Then
intcounter2 = 1
End If
If cell.Value = C Then
intcounter3 = 1
End If
If cell.Value = D Then
intcounter4 = 1
End If
If cell.Value = E Then
intcounter5 = 1
End If
If cell.Value = F Then
intcounter6 = 1
End If

Next cell

If intcounter1 + intcounter2 + intcounter3 + intcounter4 + intcounter5
+ intcounter6 = 5 Then
totcounter = totcounter + 1
End If

xlrow = xlrow + 1

Loop



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default For each cell method, is there a much faster way?

I don't quite understand your logic and I know this code is not the same
logic as yours.
but i think this one might be a little faster than yours.

Sub test()
Dim xlrow As Long, lastrow As Long
Dim totcounter As Long, s As Long
Dim Startcell As Range, Endcell As Range, rng As Range
Dim r

xlrow = 2
Set Startcell = Cells(xlrow, 2)
Set Endcell = Cells(xlrow, 7)
Set rng = Range(Startcell, Endcell)

lastrow = Cells(xlrow, 2).End(xlDown).Row

For xlrow = 2 To lastrow
For Each r In Array("A", "B", "C", "D", "E", "F")
If Application.CountIf(rng, r) = 1 Then
s = s + 1
End If
Next
If s = 5 Then
totcounter = totcounter + 1
End If
s = 0
Set rng = rng.Offset(1, 0)
Next
MsgBox totcounter
End Sub

keiji

"ste mac" wrote in message
...
Hi, I have a lot of records to search thru', everything works fine,
its just this section of code below is far too slow.

Is there any way to make this section of code work at sub light speed
or even light speed?

thanks for any help

Ste

Do While Not (Sheets("Meetingstodate").Cells(xlrow, 2).Value = "")

Set StartCell = Sheets("Meetingstodate").Cells(xlrow, 2)
Set EndCell = Sheets("Meetingstodate").Cells(xlrow, 7)

For Each cell In Range(StartCell, EndCell)

If cell.Value = A Then
intcounter1 = 1
End If
If cell.Value = B Then
intcounter2 = 1
End If
If cell.Value = C Then
intcounter3 = 1
End If
If cell.Value = D Then
intcounter4 = 1
End If
If cell.Value = E Then
intcounter5 = 1
End If
If cell.Value = F Then
intcounter6 = 1
End If

Next cell

If intcounter1 + intcounter2 + intcounter3 + intcounter4 + intcounter5
+ intcounter6 = 5 Then
totcounter = totcounter + 1
End If

xlrow = xlrow + 1

Loop


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
Which method is faster matpoh Excel Discussion (Misc queries) 2 October 21st 05 03:12 PM
Can faster CPU+larger/faster RAM significantly speed up recalulati jmk_li Excel Discussion (Misc queries) 2 September 28th 05 10:24 AM
Looking for faster method for Copy & Paste David Copp[_4_] Excel Programming 5 August 25th 04 04:32 AM
a faster method in changing cell value? Nick Excel Programming 7 August 17th 04 04:05 PM
Which method is faster? Layman Excel Programming 1 February 27th 04 05:22 PM


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