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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

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