How to hide row if cell in one column equals another

ecsinfo

New Member
Joined
May 1, 2011
Messages
4
Hello,

I am new to excel and am wondering if there is a way to create a macro that can hide a row if the cell value in one column equals the cell value in another. Here is an example:

A B C D
4485 Main 4485 Main
4486 Main 123 Anytown
4487 Main 4487 Main
4488 Main 124 Anytown

Is there a way to hide rows 1 and 3 if D1 equals B1 and D3 equals B3? There are thousands of rows I am working with, so if there is a way to do this it would be most helpful!

Thanks.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:

Code:
Sub HideRows()
 
lr = Range("A65536").End(xlUp).Row

For a = lr To 1
If Cells(a, 2).Value = Cells(a, 4).Value Then
    Rows("a:a").EntireRow.Hidden = True
Next a
 
End Sub
 
Last edited:
Upvote 0
My apologies. I was in a rush and missed a vital part out.

This should hopefully work:

Code:
Sub HideRows()
 
lr = Range("A65536").End(xlUp).Row
 
For a = lr To 1
If Cells(a, 2).Value = Cells(a, 4).Value Then
    Rows("a:a").EntireRow.Hidden = True
End if
Next a
 
End Sub
 
Upvote 0
Sorry, I got it wrong again. I've test the following code and it works for me, so it should for you too.

Code:
Sub HideRows()
 
Application.ScreenUpdating = False
 
lr = Range("A65536").End(xlUp).Row
For a = lr To 1 Step -1
If Cells(a, 2).Value = Cells(a, 4).Value Then
    Rows(a).EntireRow.Hidden = True
End If
Next a
 
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for the reply, BungleNZ.

I think I'm having trouble with this. Does it matter if the specific columns are C and N? It doesn't appear to be doing anything. (Or I may just be doing it all wrong. Like I said, I'm just starting to get into this)

Thanks again.
 
Upvote 0
Hi

It definitely matters what the 2 columns you are comparing are - I based the macro on columns B & D being the 2 compared.

If the 2 columns are C & N, then use:

Code:
Sub HideRows()
 
Application.ScreenUpdating = False
 
lr = Range("A65536").End(xlUp).Row
For a = lr To 1 Step -1
If Cells(a, 3).Value = Cells(a, 14).Value Then
    Rows(a).EntireRow.Hidden = True
End If
Next a
 
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks for the update.

I'm now getting a Compile Error: Next without for.

Do I need to add anything?
 
Upvote 0
Ok, maybe you don't have data in column a?

Try this:

Code:
Sub HideRows()
 
Application.ScreenUpdating = False
 
lr = Range("C65536").End(xlUp).Row
For a = lr To 1 Step -1
If Cells(a, 3).Value = Cells(a, 14).Value Then
    Rows(a).EntireRow.Hidden = True
End If
Next a
 
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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