Compare Cells for VBA

katk

Board Regular
Joined
Jul 21, 2009
Messages
62
Hi all,

I'm trying to write a code that checks to see whether a cell is the same as the one above it, and if not, performs several actions. I've been trying to learn VBA from an Excel guide but it only has one relating chapter and after about 15 google searches, I decided to look for help here again.

Here's what I have right now:

Code:
Sub FormatRegions()
For Each cell In Worksheets("Rough Draft").Range("C:C")
If cell.Value <> Offset(-1, 0).Value Then
cell.Font.Bold = True
cell.FontSize 0.16 = True
EntireRow.Insert
EntireRow.Insert
End If
Next cell
End Sub

It gives me an error at the "If cell.Value" line -- that's the part I can't figure out. How do I get it to compare the value of each cell to the one above it?

Thanks so much.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Your close. Try:
Code:
Sub FormatRegions()
[COLOR=red]Dim cell as Range[/COLOR]
For Each cell In Worksheets("Rough Draft").Range("C:C")
If cell.Value <> [COLOR=red]cell[/COLOR].Offset(-1, 0).Value Then
cell.Font.Bold = True
[COLOR=red]cell.Font.Size = 16[/COLOR]
[COLOR=red]cell[/COLOR].EntireRow.Insert
[COLOR=red]cell[/COLOR].EntireRow.Insert
End If
Next cell
End Sub

Note: using "cell" as a variable may not be the best idea as "cells" is a predefined object. I usually use icell or mycell. Up to you.
Also you may run into problems with the loop since you are inserting rows.
 
Last edited:
Upvote 0
Testing every cell in column C will take you a while. I'd go with something like this:

Code:
Sub FormatRegions()
Dim Last_Row As Long
Dim i As Long
Application.ScreenUpdating = False
With Sheets("Rough Draft")
    Last_Row = Range("C" & Rows.Count).End(xlUp).Row
    
    For i = Last_Row To 2 Step -1
    
        If .Cells(i, 3) <> Cells(i - 1, 3) Then
            .Cells(i, 3).Font.Bold = True
            .Cells(i, 3).FontSize 0.16 = True
            Range(.Cells(i - 1, 3), .Cells(i, 3)).EntireRow.Insert
        End If
    Next i
End With
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try

Code:
Sub FormatRegions()
Dim LR As Long, i As Long
With Worksheets("Rough Draft")
    LR = .Range("C" & Rows.Count).End(xlUp).Row
    For i = 2 To LR Step -1
        If .Range("C" & i).Value <> .Range("C" & i - 1).Value Then
            .Range("C" & i).Font.Bold = True
            .Range("C" & i).FontSize 0.16 = True
            .Rows(i).Resize(2).Insert
        End If
    Next i
End With
End Sub
 
Upvote 0
Hi, thanks -- I implemented your changes. Unfortunately, I'm still getting an error at that line, although now it's a different one: "Method 'offset' of object 'Range' failed." Any ideas?
 
Upvote 0
You need to loop backwards and stop at row 2 as in the last two posts. Otherwise you will be attempting to reference C0.
 
Upvote 0
Hi, thanks -- I implemented your changes. Unfortunately, I'm still getting an error at that line, although now it's a different one: "Method 'offset' of object 'Range' failed." Any ideas?

If you're testing every cell in column C, then the first cell (C1) cannot have an offset of -1, since cell C0 doesn't exist.

A better approach would be the one I or VoG suggested, working backwards from the last cell to the first. And you don't need to use offset, since you can simply subtract 1 from the row in question.
 
Upvote 0
Sorry, I had posted that second response before refreshing my screen and seeing your and VoG's posts. Thanks everyone!
 
Upvote 0
Hi Neil, I tried your code out, my only problem is that it is inserting two rows between every row, rather than only the rows where c11 and c12 (for instance) differ. I think it's either because the cells I'm looking at contain formulas, or (since I tried just assessing the cell values by adding .value to the references) because the formula results are text. Do those seem like probable issues?

(The formula is a UDF that does a VLOOKUP from another sheet; the results are country names, like "ARGENTINA")

I also tried Peter's code but nothing seemed to happen -- I'm not sure why?
 
Last edited:
Upvote 0
Oops!

Rich (BB code):
Sub FormatRegions()
Dim LR As Long, i As Long
With Worksheets("Rough Draft")
    LR = .Range("C" & Rows.Count).End(xlUp).Row
    For i = LR To 2 Step -1
        If .Range("C" & i).Value <> Range("C" & i - 1).Value Then
            .Range("C" & i).Font.Bold = True
            .Range("C" & i).FontSize 0.16 = True
            .Rows(i).Resize(2).Insert
        End If
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,700
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top