Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Method range of global object" error

Tags:

excel

vba

Below is the format of the program I am trying to run. The idea is that a user enters values into columns E through L in Excel and this program uses those values to auto-calculate a sum, which it puts into column M.

Sub Button12_Click()

Dim JobTwoDescription() As String
Dim JobOneJobTwoDescription() As String
Dim Length() As Range
Dim Width() As Range
Dim Quantity() As Range
Dim JobTwoTime() As Range
Dim JobOneTime As Range
Dim TotalTime As Range
Dim i As Integer

ReDim JobTwoDescription(i), JobOneJobTwoDescription(i), Length(i), Width(i), Quantity(i), JobTwoTime(i), Staples(i), Grid(i)

For i = 1 to 10

    JobTwoTime(i) = Range("Mi")
    JobTwoDescription(i) = Range("Ei")
    JobOneJobTwoDescription(i) = Range("Fi")
    Length(i) = Range("Ji")
    Width(i) = Range("Ki")
    Quantity(i) = Range("Li")
    JobOneTime = 0 'Initialize to zero
    TotalTime = 0 'Initialize to zero

    If LCase(JobOneDescription(i)) = "Option 1" Then JobOneTime(i) = A

    If LCase(JobOneDescription(i)) = "Option 2" Then JobOneTime(i) = B

    If LCase(JobOneDescription(i)) = "Option 3" Then JobOneTime(i) = C


    If LCase(JobTwoDescription(i)) = "Option4" Then

    If Length(i) + Width(i) <= X Then JobTwoTime(i) = + D* Quantity(i)

    ElseIf Length(i) + Width(i) > X & Length(i) + Width(i) <= Y Then JobTwoTime(i) = E* Quantity(i)

    ElseIf Length(i) + Width(i) > Y & Length(i) + Width(i) <= Z Then JobTwoTime(i) = F * Quantity(i)

    End If

    TotalTime(i) = JobTwoTime(i) + JobOneTime(i)

    ws.Range("Mi") = TotalTime(i)

Next i
End Sub

The error is coming up at "JobTwoTime(i) = Range("Mi")". I know it's an issue with i, though I can't quite figure it out.

like image 508
Matt R Avatar asked Feb 19 '26 00:02

Matt R


1 Answers

Change the declarations of your variables. You don't need arrays of Range objects.

Change

Dim JobTwoTime() As Range

To

Dim JobTwoTime As Double

And change

JobTwoTime(i) = Range("Mi")

to

JobTwoTime = Range("M" & i).Value

The .Value is not required as it is the default property of the Range object, but I use it.

like image 169
MatthewD Avatar answered Feb 21 '26 14:02

MatthewD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!