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
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
44,521
Office Version
365
Platform
Windows
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,105,960
Messages
5,508,411
Members
408,682
Latest member
alifarhat

This Week's Hot Topics

Top