Problem with Set Rng

JMcD14

New Member
Joined
Aug 6, 2014
Messages
2
Hi there,
I’ve been using the below code for some time to delete unwanted (grand total) rows from an often updated document:
Sub DeleteRows()
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
Dim rng As Range, cell As Range, del As Range
Set rng = Range("RowRemoval")
For Each cell In rng
If (cell.Value) = "Grand Total" _
Then
If del Is Nothing Then
Set del = cell
Else: Set del = Union(del, cell)
End If
End If
Next cell
On Error Resume Next
del.EntireRow.Delete
End Sub
It has been working fine for a number of weeks, however, when attempting to utilise it in another workbook (copied the module over exactly) I get the error message:
““Run-time error ‘1004’:
Method ‘Range’ of object ‘ _ Global’ failed”
And the: Set rng = Range("RowRemoval") is highlighted yellow.
It seems I am not designating my ranges correctly, but I cannot work out why it works in my previous workbook but not this one. Any recommended code or advice would be very much appreciated.
John
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi JMcD14 and welcome :)

In the workbook that you copied this into, is there a range named RowRemoval?
 
Upvote 0
Hi Craig,

Thank you very much :)

Yes I set the named range. I've managed a work around where I set the ranges via .values, rather than .select

Not ideal, but it works. I would still be interested in learning why though, as I will admit to not understanding...!
 
Upvote 0
So when you go to the named range dropdown box just to the left of the formula bar on the worksheet, it lists RowRemoval? And you try running this code with that workbook active?

If yes then I'm stumped.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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