PC Review


Reply
Thread Tools Rate Thread

Comparing cells, coloring differences

 
 
steve.breslin@gmail.com
Guest
Posts: n/a
 
      4th Mar 2008
I would like to compare adjoining cells (B1 and C1, B2 and C2, etc.),
and change the color of the C cells which are not identical to their B
counterparts.

It would save me an enormous amount of work, but unfortunately I'm not
quite at the point where I can work this sort of thing out by myself.
Any help would be greatly appreciated indeed!

Here's the sort of thing I'm trying, in a Word-based macro:

Sub ColorDifferences()
'compare adjoining cells (B1 and C1, B2 and C2, etc.),
'and change the color of the C cells which are not
'identical to their B counterparts

Dim oTbl As Table
Dim oRow As Row
Dim numRow As Long

If Not Selection.Information(wdWithInTable) Then
MsgBox "Please put the cursor in a table first."
Exit Sub
End If

Set oTbl = Selection.Tables(1)

If Not oTbl.Uniform Then
MsgBox "The macro can't deal with merged or split cells."
Exit Sub
End If

For numRow = 1 To oTbl.Rows.Count
Set oRow = oTbl.Rows(numRow)
With oRow
If Not .HeadingFormat Then
If .Cells(2).Range.Text <> .Cells(3).Range.Text Then
.Cells(3).Shading.ForegroundPatternColor =
wdColorRed
End If
End If
End With
Next
End Sub
 
Reply With Quote
 
 
 
 
Beverly
Guest
Posts: n/a
 
      4th Mar 2008
Hi Steve,

Have you tried using conditional formatting? You don't have to write any
code for this. Just go to cell C1, select Format-Conditional Formatting,
select cell value is not equal to B1, then click the "Format" button and
change it to something like a red background with bold font.

Beverly

"(E-Mail Removed)" wrote:

> I would like to compare adjoining cells (B1 and C1, B2 and C2, etc.),
> and change the color of the C cells which are not identical to their B
> counterparts.
>
> It would save me an enormous amount of work, but unfortunately I'm not
> quite at the point where I can work this sort of thing out by myself.
> Any help would be greatly appreciated indeed!
>
> Here's the sort of thing I'm trying, in a Word-based macro:
>
> Sub ColorDifferences()
> 'compare adjoining cells (B1 and C1, B2 and C2, etc.),
> 'and change the color of the C cells which are not
> 'identical to their B counterparts
>
> Dim oTbl As Table
> Dim oRow As Row
> Dim numRow As Long
>
> If Not Selection.Information(wdWithInTable) Then
> MsgBox "Please put the cursor in a table first."
> Exit Sub
> End If
>
> Set oTbl = Selection.Tables(1)
>
> If Not oTbl.Uniform Then
> MsgBox "The macro can't deal with merged or split cells."
> Exit Sub
> End If
>
> For numRow = 1 To oTbl.Rows.Count
> Set oRow = oTbl.Rows(numRow)
> With oRow
> If Not .HeadingFormat Then
> If .Cells(2).Range.Text <> .Cells(3).Range.Text Then
> .Cells(3).Shading.ForegroundPatternColor =
> wdColorRed
> End If
> End If
> End With
> Next
> End Sub
>

 
Reply With Quote
 
steve.breslin@gmail.com
Guest
Posts: n/a
 
      4th Mar 2008
Hi Beverly,

> Have you tried using conditional formatting? You don't have to write any
> code for this. Just go to cell C1, select Format-Conditional Formatting,
> select cell value is not equal to B1, then click the "Format" button and
> change it to something like a red background with bold font.


That works, but I need to do this for every single row in a very long
table. Is there a way to generalize the rule so it covers all the
rows, not just one-at-a-time?

Thanks!
 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      4th Mar 2008
see if something like this would work

Option Explicit

Sub compaer_cells()
Dim i As Long
Dim lastrow As Long
Dim ws As Worksheet

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "B").End(xlUp).Row

For i = 1 To lastrow
If ws.Range("B" & i).Value <> ws.Range("C" & i).Value Then
ws.Range("C" & i).Interior.ColorIndex = 35
End If
Next
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

--


Gary


<(E-Mail Removed)> wrote in message
news:5770faa2-ee8f-46d0-8077-(E-Mail Removed)...
>I would like to compare adjoining cells (B1 and C1, B2 and C2, etc.),
> and change the color of the C cells which are not identical to their B
> counterparts.
>
> It would save me an enormous amount of work, but unfortunately I'm not
> quite at the point where I can work this sort of thing out by myself.
> Any help would be greatly appreciated indeed!
>
> Here's the sort of thing I'm trying, in a Word-based macro:
>
> Sub ColorDifferences()
> 'compare adjoining cells (B1 and C1, B2 and C2, etc.),
> 'and change the color of the C cells which are not
> 'identical to their B counterparts
>
> Dim oTbl As Table
> Dim oRow As Row
> Dim numRow As Long
>
> If Not Selection.Information(wdWithInTable) Then
> MsgBox "Please put the cursor in a table first."
> Exit Sub
> End If
>
> Set oTbl = Selection.Tables(1)
>
> If Not oTbl.Uniform Then
> MsgBox "The macro can't deal with merged or split cells."
> Exit Sub
> End If
>
> For numRow = 1 To oTbl.Rows.Count
> Set oRow = oTbl.Rows(numRow)
> With oRow
> If Not .HeadingFormat Then
> If .Cells(2).Range.Text <> .Cells(3).Range.Text Then
> .Cells(3).Shading.ForegroundPatternColor =
> wdColorRed
> End If
> End If
> End With
> Next
> End Sub



 
Reply With Quote
 
Gord Dibben
Guest
Posts: n/a
 
      4th Mar 2008
steve

Select column C and Format>CF>Formula is:

=C1<>B1

Format to a color and OK your way out.

The C1 and B1 references, being relative, will increment down the column.


Gord Dibben MS Excel MVP

On Mon, 3 Mar 2008 19:07:24 -0800 (PST), (E-Mail Removed) wrote:

>Hi Beverly,
>
>> Have you tried using conditional formatting? You don't have to write any
>> code for this. Just go to cell C1, select Format-Conditional Formatting,
>> select cell value is not equal to B1, then click the "Format" button and
>> change it to something like a red background with bold font.

>
>That works, but I need to do this for every single row in a very long
>table. Is there a way to generalize the rule so it covers all the
>rows, not just one-at-a-time?
>
>Thanks!


 
Reply With Quote
 
steve.breslin@gmail.com
Guest
Posts: n/a
 
      4th Mar 2008
That works wonderfully, Gary, thanks very kindly. Thanks also to
Beverly and Gord -- I got this solution to work, but Excel is a bit
clumsy with this, e.g., coloring only bullets rather than the whole
contents of a cell, when it finds bullets.

By the way, what's the best way to become competent with this kind of
stuff for Word and Excel? Is one of the books I've seen considered
better than the rest? Or is there a particularly good in-depth online
tutorial?

 
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
Comparing text in cells for differences big john Microsoft Excel Misc 4 27th Aug 2008 12:00 PM
comparing 2 workbooks and highlighting differences =?Utf-8?B?U1lCUw==?= Microsoft Excel Programming 9 17th Apr 2008 08:31 PM
Comparing subdocuments for differences =?Utf-8?B?R2FqZGFy?= Microsoft Word Document Management 0 9th Nov 2007 12:27 AM
Comparing two columns and listing differences gb Microsoft Excel Programming 3 19th May 2004 10:18 PM
Comparing two strings and returning the differences. Rob Panosh Microsoft VB .NET 9 14th Nov 2003 09:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:08 AM.