Forum

Run-time error 1004
 
Notifications
Clear all

Run-time error 1004

5 Posts
5 Users
0 Reactions
74 Views
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
Topic starter
 

I now have a new error. Sometimes this error pops up, sometimes it runs OK

The line in red is where it stops with an error message, "Run-time error '1004': Method 'Range' of object'_Global' failed

The range "UP84Male" is a single column of 121 rows.

 

My code is as follows:

Public Sub Annuity()

Dim ArrayMortality As Variant
Dim ArrayL(121) As Variant
Dim ArrayInt_Discount(121) As Variant

Dim Seg_1 As Variant
Dim Seg_2 As Variant
Dim Seg_3 As Variant
Dim Age As Integer

Seg_1 = 0.03
Seg_2 = 0.04
Seg_3 = 0.05
Age = 15

ArrayMortality = Range("UP84Male").Value

ArrayL(1) = 1000000
ArrayInt_Discount(1) = 1
For i = 1 To 120

'Debug.Print ArrayMortality(i, 1)
'Debug.Print ArrayL(i)
ArrayL(i + 1) = (ArrayL(i) * (1 - ArrayMortality(i, 1)))
If i <= Age Then
ArrayInt_Discount(i) = 1
ElseIf i < (Age + 5) Then
ArrayInt_Discount(i) = 1 / ((1 + Seg_1) ^ (i - Age))
ElseIf i < (Age + 20) Then
ArrayInt_Discount(i) = 1 / ((1 + Seg_2) ^ (i - Age))
Else: ArrayInt_Discount(i) = 1 / ((1 + Seg_3) ^ (i - Age))
End If

Debug.Print ArrayL(i)
'Debug.Print ArrayInt_Discount(i)
Next i

 

End Sub

 
Posted : 23/12/2020 4:08 am
(@purfleet)
Posts: 412
Reputable Member
 

I dont know if your macro is doing anything before this but is it on a different worksheet when it errors? the code is not fully qualified, so it will act on the active worksheet

try Worksheets("Sheet1").Range("UP84Male").Value as a starting point (where Sheet1 is the actual sheet name with the named range)

 
Posted : 24/12/2020 2:09 am
(@debaser)
Posts: 836
Member Moderator
 

If the code is in a worksheet code module, then Range("UP84Male").Value would always refer to a range on that worksheet (this seems unlikely as you'd either always get the error, or never get it).

If the code is in a normal module, then that code is the equivalent of Application.Range("UP84Male").Value and should work as long as the correct workbook is active.

 
Posted : 24/12/2020 6:20 am
(@dmdavala)
Posts: 28
Eminent Member
 

What do you mean by, "the code is not fully qualified?"

I want this code to work on any worksheet in the workbook.

Ideally, I would like it to load whenever I opened Excel. (In my personal.xlsm file?)  I need to get the calculations working first.

 
Posted : 24/12/2020 9:01 am
(@doc-aelstein)
Posts: 21
Eminent Member
 

Hello David

Range referencing in VBA is actually quite complicated. To understand fully needs  quite a bit of reading of a good Blog, or good tuition. But mostly you are lucky and never notice its complexity,  since a simple code line like yours usually works. When it doesn’t work  is when you usually have to learn the hard way.

What is usually meant  by “unqualified” in such cases is that Excel is left to “guess” where Range("UP84Male") is.

Purfleet suggested something which would tell VBA that your range is in a worksheet which has the name "Sheet1" in the workbook where the macro is.

" Velouria" explained the typical default places that Excel “guesses” your range is at,  in certain situations

 

It’s not too clearly defined this “unqualified” thing. If your macro is in a normal code module, then with no worksheet “qualifier” reference before the Range, you can loosely call it “unqualified”. If your macro is in a worksheet object code module then you are effectively “in” that worksheet so you are referencing that worksheet.

 

To fully “qualify” your range, you would need to do something of this sort of form

Workbooks("MyBook.xls").Worksheets("MySheet").Range("UP84Male").Value

or this:

Application.Range("='[MyBook.xls]MySheet'!UP84Male").Value

 

_.____

 

Having said all this, there is a rarely  reported bug in Excel that does , seemingly randomly and inconsistently , chuck up that error you are having  even when you are “qualifying” everything fully.

That bug usually goes away if you do some activating or selecting just before the code line that typically errors, something like

Worksheet("y").Activate: Worksheet("y").Range("A1").Select

or, if dealing with multiple open workbooks,

Workbooks("x“).Activate: Worksheet("y").Activate: Worksheet("y").Range("A1").Select

 

Just to avoid confusing you, here is the same again just written a bit differently:

Worksheet("y").Activate

 Worksheet("y").Range("A1").Select

or, if dealing with multiple open workbooks,

Workbooks("x“).Activate

 Worksheet("y").Activate

 Worksheet("y").Range("A1").Select

 

In your case, if using this workaround,  it would be best if in place of x you had your workbook name, y your worksheet name, ( both being those containing your range "UP84Male" ), and finally best to be Selecting Range("UP84Male")

Note that Activating and Selecting is generally not a good idea in VBA as it is rarely needed and can slow things down. I am only suggesting it here as something to try if the error type that you are experiencing cannot be cured by correctly “qualifying” your range

_._____

Another shot in the dark, but worth a quick try: Just before the code line that errors, try a double DoEvents , like

DoEvents: DoEvents

or

DoEvents

 DoEvents

I have never known this double DoEvents to cure the particular bug I am referring to, but it does cure some similar ones

 But once again, only use this if you have to , since it can slow things down slightly.

Alan

 
Posted : 30/12/2020 9:52 am
Share: