Use cell reference instead of text in VBA

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all!

I have this portion of code that seems to work, however I need instead of a fixed item like "California", I need it to reference a sheet and a cell instead. I feel like everything I've tried isn't working, and its probably something super simple that I'm doing wrong.
VBA Code:
    For Each c In Range("C2:C50").Cells
        If c.Value <> "California" Then
            c.EntireRow.Hidden = True

        End If
    Next c
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
you could simply replace "California" with something like range("Z1").value (assuming Z1 was your reference cell.
 
Upvote 0
you could simply replace "California" with something like range("Z1").value (assuming Z1 was your reference cell.
See thats what I thought. I put it in and it works on my test worksheet. Then I transfer it to my bigger worksheet and it doesn't work. It's confusing...
 
Upvote 0
It will depend if, when you use it, if you are on that worksheet or on another. If your on another then it needs Worksheets("nameofsheet").Range("Z1").Value
 
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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