Intersect - Variable Range - Error

meledward23

New Member
Joined
Aug 2, 2016
Messages
19
So I am using intersect to clearcontents, as shown below. I am getting an "Object variable or with block variable not set" and I am stuck.
If I manually enter the resulting Range name (option2x) instead of the variable range name (rngselect), then it works.

Dim rngselect As Range
Dim lrow As Long
Dim rng As Range

Set rngselect = Range("assemselect") 'user selected range result on demo is "OPTION2X"

'show only user selected range
lrow = Sheets("HS Template").Range("ag" & Rows.Count).End(xlUp).Row
Range("ag1:ag" & lrow).EntireRow.Hidden = True
Range("a1:a5").EntireRow.Hidden = False
Range(rngselect).EntireRow.Hidden = False

'clear manual data in range
msgbox rngselect 'results in "option2x"
Application.Intersect(rngselect, Range("c:c")).clearcontents 'if I use application.intersect("Option2x",range("c:c")).clearcontents this works

Application.GoTo ActiveSheet.Range("A1"), True
 
I am not cemented on this layout. If you can think of a better way to achieve this. I just have 40 ranges, each with over 50 columns. I want to be able to filter down to the one the user selects. and make sure all user enter data is cleared, but it leaves the standardized text in certain columns.
This is the only solution I can think of.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I lied... $K$2 - the address of assemselect


1596652123368.png
 
Upvote 0
Well K2 doesn't intersect with Column C which is why you get the error, you need
VBA Code:
Application.Intersect(rngselect.EntireRow, Range("c:c")).ClearContents
which will clear C2
 
Upvote 0
Right, so...
1596654177384.png

this unhides the Range, not $k$2, but the range Identified/named by K2 (Option2x = rows 13:21)
Not sure why it works here, but not there.


Oh well, I can clear visible cells if they are user ranges instead of headers so,


*******
lrow = Sheets("HS Template").Range("ag" & Rows.Count).End(xlUp).Row

On Error Resume Next
For Each rngrow In Range("c5:c" & lrow).SpecialCells(xlCellTypeVisible)
If Range("ag") = "ASSEM" Then
rngrow.Cells(3).clearcontents
******

And it works.
Thank you for your time , you helped me solve this.
 
Upvote 0
this unhides the Range, not $k$2, but the range Identified/named by K2 (Option2x = rows 13:21)

So what happens with
VBA Code:
Application.Intersect(Range(rngselect).EntireRow, Range("c:c")).ClearContents
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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