1004 Error Setting Variable Equal to Value of a Range

stinkingcedar

New Member
Joined
May 2, 2016
Messages
23
Hey guys,

So I keep getting a 1004 on this particular line of code

Code:
newVal = sht.Range(Cells(4 + (30 * i), j)).Value

Sht is a worksheet variable set to one specific sheet in the workbook, newVal has been declared as double, i and j have both been declared as integers. Keeps telling me the range method failed.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hey guys,

So I keep getting a 1004 on this particular line of code

Code:
newVal = sht.Range(Cells(4 + (30 * i), j)).Value

Sht is a worksheet variable set to one specific sheet in the workbook, newVal has been declared as double, i and j have both been declared as integers. Keeps telling me the range method failed.

error 1004 is a catch-all type error, usually related to a user created condition such as typos, or misuse of variables. You will need to post more of your code than the one line for analysis.
 
Upvote 0
error 1004 is a catch-all type error, usually related to a user created condition such as typos, or misuse of variables. You will need to post more of your code than the one line for analysis.

Sorry about that, here's a longer piece of the code:

Code:
Public Sub Generate()

Dim StartCellRow As Integer, StartCellColumn As Integer
Dim i As Integer, newVal As Double, curVal As Double
Dim c As Integer, r As Integer, j As Integer
Dim Start As Range
Dim shtS As Worksheet
Dim shtDWorksheet
Dim shtV Worksheet
Dim Region As String, ProjectName As String


Set shtS= Sheets("Sheet1")
Set shtD= Sheets("Sheet 2")
Set shtV= Sheets("Sheet 3")


Application.ScreenUpdating = False


Region = shtV.Range("C1").Text


Select Case Region


        Case Is = "Americas"
            
            For c = 4 To 13
               
                j = 5
                    
                    For r = 4 To 24
        
                        For i = 0 To 29
                
                            newVal = shtD.Range(Cells(4 + (30 * i), j)).Value
                            curVal = newVal + curVal
                    
                        Next i
                        
                        shtS.Cells(r, c) = curVal
                        j = j + 1
                        
                    Next r
                
            Next c

The error is this line of code:

Code:
newVal = shtD.Range(Cells(4 + (30 * i), j)).Value
 
Upvote 0
Is this inconsistency really correct?
Code:
Set shtS= Sheets("Sheet1")
Set shtD= Sheets("Sheet 2")
Set shtV= Sheets("Sheet 3")
So, some of your sheet names have spaces in them and others do not?
 
Last edited:
Upvote 0
Is this inconsistency really correct?
Code:
Set shtS= Sheets("Sheet1")
Set shtD= Sheets("Sheet 2")
Set shtV= Sheets("Sheet 3")
So, some of your sheet names have spaces in them and others do not?

No, I have renamed several variables and sheet names for the purpose of posting in this thread. Must have been a mistake when typing up the forum post, but in my code that is not the case.
 
Upvote 0
Often times, when I see that error, it is because it is unable to access range you are referencing. This can happen for a myriad of reasons, including:
- You have a typo in a sheet name (so you are trying to reference a sheet which does not exist)
- You are trying to reference an invalid cell reference (i.e. row 2,000,000)
- You are trying to reference a hidden cell
- You are trying to update a protected cell

Sometimes, stepping through your code and seeing what you variables are set to at certain points makes it evident what the error is. Have you tried that?
 
Upvote 0
The error is this line of code:

Code:
newVal = shtD.Range([B][COLOR="#FF0000"]shtD[/COLOR][/B].Cells(4 + (30 * i), j)).Value
I think the problem is the Cells property is referencing the active sheet which I am assuming is not what shtD is referencing. Try making the change I show in red and see if that solves the problem.
 
Upvote 0
Joe, just attempted to step through the code and could not seem to glean any important information from doing so. All the other variables seem to be declared, set and passed correctly, so I can't seem to figure out why that line isn't working.

Rick, I just attempted your suggested change and still no luck :/
 
Upvote 0
Any chance you could post a sample of your data so we can try to recreate your scenario and try it out for ourselves?

There are instructions on how you can post images found in the "Posting Aids" section of this link here: http://www.mrexcel.com/forum/board-announcements/127080-guidelines-forum-use.html. There is also a "Test Here" forum on this site that you can use to test those tools before using them in your threads.
 
Upvote 0
Does this work?
Code:
newVal = sht.Cells(4 + (30 * i), j).Value
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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