Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coloured Cells

Status
Not open for further replies.

Ca1icoJack

IS-IT--Management
Nov 27, 2008
36
GB
I have a loop which looks down thousands of lines and across 20 cells on each row. The code takes a huge amount of time to run, since it is looped on a lot of files. Is there a way to make the code only look at a line if the line contains colour since many are blank?

Thank you
 




Hi,

Wouldn't each cell in the line need to be interogated to determine as such?

Perhaps you ought to post your code containing the loops, in order to get some advice.

So you have ScreenUpdating turned off?

Are you referencing cells rather than SELECTING cells?

Are you using redundant referencing, that can often be mitigated using the With...End With construct?

Some of these questions can be answered by observing your code.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've missed out all of the detal in the code except for the way the loop works, any idea on how to make this faster? (I never know where the coloured cells will begin or end)

Thanks for your help
____________________________________________________________
For R = 1 To LastRow(0)

For C = 1 To 15

If Selection.Interior.ColorIndex = 35 Then Type = ActiveCell.Value 'There are a lot of these.

ActiveCell.Offset(0, 1).Select

If Type <> "" And C = 15 Then

Sheets("Output").Select
Range("A2").Select

For T = 1 To Range("IV1").End(xlToLeft).Column

If ActiveCell.Interior.ColorIndex = 35 Then ActiveCell.Value = Type

ActiveCell.Offset(0, 1).Select

Next T

End If

Next C

ActiveCell.Offset(1, -15).Select

Next R
____________________________________________________________
 


You are selecting Sheet Output, but you never select any other sheet. WHY?

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



What does it mean, there are alot of these? A lot of colors you are testing for?

What color indexes? What action is taken?

BTW, I see MUCH that could shave significant time from your process.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 



This is only an example, since the code that you posted is woefully undescriptive of your process. Do NOT use Type...

faq707-4105
Code:
Dim Typ     'Type is a reserve word
For r = 1 To LastRow(0)
                        
    For c = 1 To 15

        With Cells(r, c)
            Select Case .Interior.ColorIndex
                Case 32
                    
                Case 33
                
                Case 34
                    Typ = .Value
                Case Else
                
            End Select
                
            If Typ <> "" And c = 15 Then
                   
                For t = 1 To Sheets("Output").Range("IV1").End(xlToLeft).Column
                    With .Offset(0, t)
                        Select Case .Interior.ColorIndex
                            Case 32
                                
                            Case 33
                            
                            Case 34
                                Typ = .Value
                            Case Else
                            
                        End Select
                    End With
                Next t
        
            End If
        End With
    Next c
Next r


Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
There are ~30 different colours the code goes through to test for.

I don't actually use Type in the code... I cut down all of the detail just to show the loop as the rest is just a repetition of the two if statements and a lot of calculations which cannot be changed. If you want to see the entire piece of code I can post that. And yes, I do reselect the original sheet, I just forgot to type that bit of code in.

Will referencing cells, as SkipVought described, make the code run a lot more quickly than moving and selecting cells then?

Thanks for the help.
 



Yes. And ScreenUpdating.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for the help. ScreenUpdating has always been off.

I'm trying it out with your code now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top