PC Review


Reply
Thread Tools Rate Thread

code to mark coloured cells in excel

 
 
David Kennedy
Guest
Posts: n/a
 
      11th Nov 2008
Hi,

I have an excel file that has 17000 rows and 33 cols.
Some of the cells in the file are highlighted yellow.
I am trying to mark (with an X) in col 34 at the end what rows contains the
highlighted cell

My code below:

Dim rngCells As Range
Dim intRows As Long
Dim intFields As Long
Dim iRow As Long
Dim iField As Long

Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange

intRows = rngCells.Rows.Count
intFields = rngCells.Columns.Count

For iRow = 1 To intRows Step 2
For iField = 1 To intFields
If Cells(iRow, iField).ColorIndex.Value = 6 Then
With Cells(iRow, 34)
.Value = "X"
End With
End If
Next iField
Next iRow

Set rngCells = Nothing

MsgBox "Yellow colors flagged"

The code gives me an error on the first If saying "Object doesnt support
this property or method"
Can anyone help? or is my code completely wrong?

Thanks in advance
David


 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      11th Nov 2008
David,

The correct syntax for this line

If Cells(iRow, iField).ColorIndex.Value = 6 Then

is

If Cells(iRow, iField).Interior.ColorIndex = 6 Then

Mike

"David Kennedy" wrote:

> Hi,
>
> I have an excel file that has 17000 rows and 33 cols.
> Some of the cells in the file are highlighted yellow.
> I am trying to mark (with an X) in col 34 at the end what rows contains the
> highlighted cell
>
> My code below:
>
> Dim rngCells As Range
> Dim intRows As Long
> Dim intFields As Long
> Dim iRow As Long
> Dim iField As Long
>
> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>
> intRows = rngCells.Rows.Count
> intFields = rngCells.Columns.Count
>
> For iRow = 1 To intRows Step 2
> For iField = 1 To intFields
> If Cells(iRow, iField).ColorIndex.Value = 6 Then
> With Cells(iRow, 34)
> .Value = "X"
> End With
> End If
> Next iField
> Next iRow
>
> Set rngCells = Nothing
>
> MsgBox "Yellow colors flagged"
>
> The code gives me an error on the first If saying "Object doesnt support
> this property or method"
> Can anyone help? or is my code completely wrong?
>
> Thanks in advance
> David
>
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Nov 2008
If Cells(iRow, iField).Font.ColorIndex = 6 Then
or
If Cells(iRow, iField).Interior.ColorIndex = 6 Then

(Font color or fill color??)

David Kennedy wrote:
>
> Hi,
>
> I have an excel file that has 17000 rows and 33 cols.
> Some of the cells in the file are highlighted yellow.
> I am trying to mark (with an X) in col 34 at the end what rows contains the
> highlighted cell
>
> My code below:
>
> Dim rngCells As Range
> Dim intRows As Long
> Dim intFields As Long
> Dim iRow As Long
> Dim iField As Long
>
> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>
> intRows = rngCells.Rows.Count
> intFields = rngCells.Columns.Count
>
> For iRow = 1 To intRows Step 2
> For iField = 1 To intFields
> If Cells(iRow, iField).ColorIndex.Value = 6 Then
> With Cells(iRow, 34)
> .Value = "X"
> End With
> End If
> Next iField
> Next iRow
>
> Set rngCells = Nothing
>
> MsgBox "Yellow colors flagged"
>
> The code gives me an error on the first If saying "Object doesnt support
> this property or method"
> Can anyone help? or is my code completely wrong?
>
> Thanks in advance
> David


--

Dave Peterson
 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      11th Nov 2008
Try filling an odd numbered row because that's what your testing

"David Kennedy" wrote:

> Thanks for the swift reply,
>
> Fill color,
>
> for testing I filled in yellow on row2 col1
> I applied your fix but the code doesnt seem to recognise the yellow filled
> field
>
>
>
>
>
> "Dave Peterson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > If Cells(iRow, iField).Font.ColorIndex = 6 Then
> > or
> > If Cells(iRow, iField).Interior.ColorIndex = 6 Then
> >
> > (Font color or fill color??)
> >
> > David Kennedy wrote:
> >>
> >> Hi,
> >>
> >> I have an excel file that has 17000 rows and 33 cols.
> >> Some of the cells in the file are highlighted yellow.
> >> I am trying to mark (with an X) in col 34 at the end what rows contains
> >> the
> >> highlighted cell
> >>
> >> My code below:
> >>
> >> Dim rngCells As Range
> >> Dim intRows As Long
> >> Dim intFields As Long
> >> Dim iRow As Long
> >> Dim iField As Long
> >>
> >> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
> >>
> >> intRows = rngCells.Rows.Count
> >> intFields = rngCells.Columns.Count
> >>
> >> For iRow = 1 To intRows Step 2
> >> For iField = 1 To intFields
> >> If Cells(iRow, iField).ColorIndex.Value = 6 Then
> >> With Cells(iRow, 34)
> >> .Value = "X"
> >> End With
> >> End If
> >> Next iField
> >> Next iRow
> >>
> >> Set rngCells = Nothing
> >>
> >> MsgBox "Yellow colors flagged"
> >>
> >> The code gives me an error on the first If saying "Object doesnt support
> >> this property or method"
> >> Can anyone help? or is my code completely wrong?
> >>
> >> Thanks in advance
> >> David

> >
> > --
> >
> > Dave Peterson

>
>
>

 
Reply With Quote
 
David Kennedy
Guest
Posts: n/a
 
      11th Nov 2008
Thanks for the swift reply,

Fill color,

for testing I filled in yellow on row2 col1
I applied your fix but the code doesnt seem to recognise the yellow filled
field





"Dave Peterson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If Cells(iRow, iField).Font.ColorIndex = 6 Then
> or
> If Cells(iRow, iField).Interior.ColorIndex = 6 Then
>
> (Font color or fill color??)
>
> David Kennedy wrote:
>>
>> Hi,
>>
>> I have an excel file that has 17000 rows and 33 cols.
>> Some of the cells in the file are highlighted yellow.
>> I am trying to mark (with an X) in col 34 at the end what rows contains
>> the
>> highlighted cell
>>
>> My code below:
>>
>> Dim rngCells As Range
>> Dim intRows As Long
>> Dim intFields As Long
>> Dim iRow As Long
>> Dim iField As Long
>>
>> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>>
>> intRows = rngCells.Rows.Count
>> intFields = rngCells.Columns.Count
>>
>> For iRow = 1 To intRows Step 2
>> For iField = 1 To intFields
>> If Cells(iRow, iField).ColorIndex.Value = 6 Then
>> With Cells(iRow, 34)
>> .Value = "X"
>> End With
>> End If
>> Next iField
>> Next iRow
>>
>> Set rngCells = Nothing
>>
>> MsgBox "Yellow colors flagged"
>>
>> The code gives me an error on the first If saying "Object doesnt support
>> this property or method"
>> Can anyone help? or is my code completely wrong?
>>
>> Thanks in advance
>> David

>
> --
>
> Dave Peterson



 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      11th Nov 2008
A highly simplified version you can modify

Sub colorif()
lr = ActiveSheet.UsedRange.Rows.Count
'MsgBox lr
For i = 1 To lr
lc = Cells(i, Columns.Count).End(xlToLeft).Column
'MsgBox lc
For j = 1 To lc
If Cells(i, j).Interior.ColorIndex = 6 Then Cells(i, 34) = "x"
'MsgBox i
Next j
Next i
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"David Kennedy" <(E-Mail Removed)> wrote in message
news:%23RZt49$(E-Mail Removed)...
> Hi,
>
> I have an excel file that has 17000 rows and 33 cols.
> Some of the cells in the file are highlighted yellow.
> I am trying to mark (with an X) in col 34 at the end what rows contains
> the highlighted cell
>
> My code below:
>
> Dim rngCells As Range
> Dim intRows As Long
> Dim intFields As Long
> Dim iRow As Long
> Dim iField As Long
>
> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>
> intRows = rngCells.Rows.Count
> intFields = rngCells.Columns.Count
>
> For iRow = 1 To intRows Step 2
> For iField = 1 To intFields
> If Cells(iRow, iField).ColorIndex.Value = 6 Then
> With Cells(iRow, 34)
> .Value = "X"
> End With
> End If
> Next iField
> Next iRow
>
> Set rngCells = Nothing
>
> MsgBox "Yellow colors flagged"
>
> The code gives me an error on the first If saying "Object doesnt support
> this property or method"
> Can anyone help? or is my code completely wrong?
>
> Thanks in advance
> David
>
>


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      11th Nov 2008
Even simpler but may take awhile
Sub colorif2()
For Each c In ActiveSheet.UsedRange
If c.Interior.ColorIndex = 6 Then Cells(c.Row, 34) = "X"
Next c
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Don Guillett" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>A highly simplified version you can modify
>
> Sub colorif()
> lr = ActiveSheet.UsedRange.Rows.Count
> 'MsgBox lr
> For i = 1 To lr
> lc = Cells(i, Columns.Count).End(xlToLeft).Column
> 'MsgBox lc
> For j = 1 To lc
> If Cells(i, j).Interior.ColorIndex = 6 Then Cells(i, 34) = "x"
> 'MsgBox i
> Next j
> Next i
> End Sub
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "David Kennedy" <(E-Mail Removed)> wrote in message
> news:%23RZt49$(E-Mail Removed)...
>> Hi,
>>
>> I have an excel file that has 17000 rows and 33 cols.
>> Some of the cells in the file are highlighted yellow.
>> I am trying to mark (with an X) in col 34 at the end what rows contains
>> the highlighted cell
>>
>> My code below:
>>
>> Dim rngCells As Range
>> Dim intRows As Long
>> Dim intFields As Long
>> Dim iRow As Long
>> Dim iField As Long
>>
>> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>>
>> intRows = rngCells.Rows.Count
>> intFields = rngCells.Columns.Count
>>
>> For iRow = 1 To intRows Step 2
>> For iField = 1 To intFields
>> If Cells(iRow, iField).ColorIndex.Value = 6 Then
>> With Cells(iRow, 34)
>> .Value = "X"
>> End With
>> End If
>> Next iField
>> Next iRow
>>
>> Set rngCells = Nothing
>>
>> MsgBox "Yellow colors flagged"
>>
>> The code gives me an error on the first If saying "Object doesnt support
>> this property or method"
>> Can anyone help? or is my code completely wrong?
>>
>> Thanks in advance
>> David
>>
>>

>


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      11th Nov 2008
Don,

The OP is only checking odd rows so this makes your code go nearly twice as
fast

For Each c In ActiveSheet.UsedRange
If c.Interior.ColorIndex = 6 And c.Row Mod 2 = 1 _
Then Cells(c.Row, 34) = "X"
Next c


Mike

"Don Guillett" wrote:

> Even simpler but may take awhile
> Sub colorif2()
> For Each c In ActiveSheet.UsedRange
> If c.Interior.ColorIndex = 6 Then Cells(c.Row, 34) = "X"
> Next c
> End Sub
>
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "Don Guillett" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >A highly simplified version you can modify
> >
> > Sub colorif()
> > lr = ActiveSheet.UsedRange.Rows.Count
> > 'MsgBox lr
> > For i = 1 To lr
> > lc = Cells(i, Columns.Count).End(xlToLeft).Column
> > 'MsgBox lc
> > For j = 1 To lc
> > If Cells(i, j).Interior.ColorIndex = 6 Then Cells(i, 34) = "x"
> > 'MsgBox i
> > Next j
> > Next i
> > End Sub
> > --
> > Don Guillett
> > Microsoft MVP Excel
> > SalesAid Software
> > (E-Mail Removed)
> > "David Kennedy" <(E-Mail Removed)> wrote in message
> > news:%23RZt49$(E-Mail Removed)...
> >> Hi,
> >>
> >> I have an excel file that has 17000 rows and 33 cols.
> >> Some of the cells in the file are highlighted yellow.
> >> I am trying to mark (with an X) in col 34 at the end what rows contains
> >> the highlighted cell
> >>
> >> My code below:
> >>
> >> Dim rngCells As Range
> >> Dim intRows As Long
> >> Dim intFields As Long
> >> Dim iRow As Long
> >> Dim iField As Long
> >>
> >> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
> >>
> >> intRows = rngCells.Rows.Count
> >> intFields = rngCells.Columns.Count
> >>
> >> For iRow = 1 To intRows Step 2
> >> For iField = 1 To intFields
> >> If Cells(iRow, iField).ColorIndex.Value = 6 Then
> >> With Cells(iRow, 34)
> >> .Value = "X"
> >> End With
> >> End If
> >> Next iField
> >> Next iRow
> >>
> >> Set rngCells = Nothing
> >>
> >> MsgBox "Yellow colors flagged"
> >>
> >> The code gives me an error on the first If saying "Object doesnt support
> >> this property or method"
> >> Can anyone help? or is my code completely wrong?
> >>
> >> Thanks in advance
> >> David
> >>
> >>

> >

>
>

 
Reply With Quote
 
David Kennedy
Guest
Posts: n/a
 
      11th Nov 2008
thanks for all your help lads
much appreciated

David


"Mike H" <(E-Mail Removed)> wrote in message
news:33CF02C6-47EA-4277-9868-(E-Mail Removed)...
> Try filling an odd numbered row because that's what your testing
>
> "David Kennedy" wrote:
>
>> Thanks for the swift reply,
>>
>> Fill color,
>>
>> for testing I filled in yellow on row2 col1
>> I applied your fix but the code doesnt seem to recognise the yellow
>> filled
>> field
>>
>>
>>
>>
>>
>> "Dave Peterson" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > If Cells(iRow, iField).Font.ColorIndex = 6 Then
>> > or
>> > If Cells(iRow, iField).Interior.ColorIndex = 6 Then
>> >
>> > (Font color or fill color??)
>> >
>> > David Kennedy wrote:
>> >>
>> >> Hi,
>> >>
>> >> I have an excel file that has 17000 rows and 33 cols.
>> >> Some of the cells in the file are highlighted yellow.
>> >> I am trying to mark (with an X) in col 34 at the end what rows
>> >> contains
>> >> the
>> >> highlighted cell
>> >>
>> >> My code below:
>> >>
>> >> Dim rngCells As Range
>> >> Dim intRows As Long
>> >> Dim intFields As Long
>> >> Dim iRow As Long
>> >> Dim iField As Long
>> >>
>> >> Set rngCells = Application.ActiveWorkbook.ActiveSheet.UsedRange
>> >>
>> >> intRows = rngCells.Rows.Count
>> >> intFields = rngCells.Columns.Count
>> >>
>> >> For iRow = 1 To intRows Step 2
>> >> For iField = 1 To intFields
>> >> If Cells(iRow, iField).ColorIndex.Value = 6 Then
>> >> With Cells(iRow, 34)
>> >> .Value = "X"
>> >> End With
>> >> End If
>> >> Next iField
>> >> Next iRow
>> >>
>> >> Set rngCells = Nothing
>> >>
>> >> MsgBox "Yellow colors flagged"
>> >>
>> >> The code gives me an error on the first If saying "Object doesnt
>> >> support
>> >> this property or method"
>> >> Can anyone help? or is my code completely wrong?
>> >>
>> >> Thanks in advance
>> >> David
>> >
>> > --
>> >
>> > Dave Peterson

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there an Excel forumla to count different coloured cells? dataprincess Microsoft Excel Misc 2 22nd Nov 2007 11:29 PM
Can Excel mark or identify duplicate cells in a range? =?Utf-8?B?Um9iIE1jRXdhbg==?= Microsoft Excel Misc 0 15th Feb 2007 03:54 PM
RE: Can Excel mark or identify duplicate cells in a range? =?Utf-8?B?Ymo=?= Microsoft Excel Misc 0 15th Feb 2007 03:38 PM
RE: Can Excel mark or identify duplicate cells in a range? =?Utf-8?B?RGF2ZSBG?= Microsoft Excel Misc 0 15th Feb 2007 03:35 PM
Is it possible to have a code that counts coloured cells? =?Utf-8?B?RmlvbmE=?= Microsoft Excel Programming 4 14th Feb 2005 01:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 AM.