VBA Error 1004 (Global Range Failed)

JaredMcCullough

Well-known Member
Joined
Aug 1, 2011
Messages
516
Hi,

It has been a few years since I have really wrote any substantial amount of macros. This one is a few years old and I am trying to identify the issue and feel like it should be coming to me easier. I am hoping someone else with a little better knowledge base might be able to identify it easily.

The VBA essentially runs a quick set of calculations upon workbook open. I adjusted the location that it is doing this by one column which changed the range reference but not sure why this would have impacted its operation.

Runtime 1004 is showing up on the following line items from the code below. Based on my understanding it is most likely due to the reference of the text denotation of the column (i.e. "C" or "D"). What I am struggling with is why this is the problem as it worked before and what a quick fix would be.

Range("C" & r) = (A + B) * (C ^ D)
Range("D" & r) = (A + B) * (C ^ D) * F
Range("E" & r) = (A + B) * (C ^ D) * (E ^ dblPowerToE) * F

Code:
Private Sub Workbook_Open()    Dim A As Long, B As Long, C As Long, D As Long, E As Long, F As Long, N As Long
    Dim dblNetWorkDays As Double, dblEstWorkHrs As Double, dblHoursInDay As Double
    Dim dblAvailHours  As Double, dblPowerToE   As Double
    
    N = Range("A" & Rows.Count).End(xlUp).Row
    
    For x = 4 To N
  
       If Range("A" & x).Value <> "" And Range("P" & x).Value <> "" Then
        
            dblEstWorkHrs = Val(Range("O" & x).Value)
            dblHoursInDay = 8
            If DateValue(Range("P" & x).Value) <= Date Then
            dblNetWorkDays = Application.NetworkDays(DateValue(Range("P" & x).Value), Date)
            dblAvailHours = dblEstWorkHrs - (dblNetWorkDays * dblHoursInDay)
            Else
                dblNetWorkDays = Application.NetworkDays(Date, DateValue(Range("P" & x).Value))
                dblAvailHours = dblNetWorkDays * dblHoursInDay
            End If
            dblPowerToE = 1 + (dblEstWorkHrs / dblAvailHours)
            
            'Lookup values from target row
            Select Case Range("I" & x).Value
                Case "Audit"
                    A = 10
                Case "PHA"
                    A = 7
                Case "Inspection"
                    A = 5
                Case "Other"
                    A = 3
                Case Else
                    A = 0
            End Select
            
            Select Case Range("J" & x).Value
                Case "Regulatory", "Quantitative"
                    B = 3
                Case "Corporate", "Semi-Quantitative", "External"
                    B = 2
                Case "Site", "Qualitative", "Internal", "Any"
                    B = 1
                Case Else
                    B = 0
            End Select
            
            Select Case Range("K" & x).Value
                Case "Gap"
                    C = 3
                Case "Requirement"
                    C = 2
                Case "Improvement"
                    C = 1
                Case Else
                    C = 0
            End Select
            
            Select Case Range("L" & x).Value
                Case "Regulatory"
                    D = 3
                Case "Corporate"
                    D = 2
                Case "Site"
                    D = 1
                Case Else
                    D = 0
            End Select
            
            Select Case Range("M" & x).Value
                Case "0-6"
                    E = 5
                Case "7-12"
                    E = 4
                Case "13-18"
                    E = 3
                Case "19-24"
                    E = 2
                Case "25+"
                    E = 1
                Case Else
                    E = 0
            End Select
            
            Select Case Range("N" & x).Value
                Case ">$250,000"
                    F = 4
                Case "<$250,000"
                    F = 3
                Case "<$100,000"
                    F = 2
                Case "<$25,000"
                    F = 1
                Case Else
                    F = 0
            End Select
            
            'recalculate column C value in target row based on lookup results
            If dblPowerToE <> 0 And A <> 0 And B <> 0 And C <> 0 And D <> 0 And E <> 0 And F <> 0 Then
                Range("C" & r) = (A + B) * (C ^ D)
                Range("D" & r) = (A + B) * (C ^ D) * F
                Range("E" & r) = (A + B) * (C ^ D) * (E ^ dblPowerToE) * F
            Else
                MsgBox "Could not calculate. Missing or Invalid Parameter."
                Range("E" & r) = "Error: Parameter"
            End If
        Else: Range("E" & x) = "Error: Date"
        End If
    Next


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Re: VBA Error 1004 (Global Range Failed) - Need help identifying problem

Shouldn't it be
Code:
Range("C" & [COLOR=#ff0000]x[/COLOR])
I can see no mention of a variable called r
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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