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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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