Run-time 1004 error

Krull_WarriorKing

New Member
Joined
Nov 15, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to set a range variable so that it grabs all the columns in a given row. When setting the variable by calling out the cells specifically, the code runs just fine. However, when using the .End(...) extension, the code throws a 1004 error. What's even stranger is that I can copy this exact code (from Sheet1) into a different sheet (Sheet11, for example) and it works just fine. No errors! The lines that throw an error are below. Has anyone else encountered something like this before?

VBA Code:
Sub CapacityCompare()

Dim blkI As Range
Dim blkIA As Range
Dim cell As Range
Dim k As Long


Set blkI = Sheets("Capacity").Range(Range("E8"), Range("E8").End(xlToRight))
Set blkIA = Sheets("Capacity").Range(Range("E9"), Range("E9").End(xlToRight))

...
End Sub

I should also say that I have used several variations of this code, such as using With Sheets("Capacity") or using Range(Cells(x,x),Cells(x,Columns.Count).End(...)) with the same results: the code works perfectly in another Sheet, but not in Sheet1.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You need to specify the sheets for your second 'Range's. Range by itself assumes the active sheet.

VBA Code:
Set blkI = Sheets("Capacity").Range(Range("E8"), Range("E8").End(xlToRight))
Set blkIA = Sheets("Capacity").Range(Range("E9"), Range("E9").End(xlToRight))

The 'Range' at the end of those two lines need to have the sheet set like 'Range' at the beginning of those two lines
 
Upvote 0
Solution
You need to specify the sheets for your second 'Range's. Range by itself assumes the active sheet.

VBA Code:
Set blkI = Sheets("Capacity").Range(Range("E8"), Range("E8").End(xlToRight))
Set blkIA = Sheets("Capacity").Range(Range("E9"), Range("E9").End(xlToRight))

The 'Range' at the end of those two lines need to have the sheet set like 'Range' at the beginning of those two lines
Yup, calling out the correct sheet for the other Range callouts did it. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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