“全局对象的方法范围”错误

时间:2015-10-16 17:45:18

标签: excel vba

以下是我尝试运行的程序的格式。这个想法是用户在Excel中将值输入到E到L列中,并且该程序使用这些值来自动计算总和,并将其放入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

错误出现在“JobTwoTime(i)= Range(”Mi“)”。我知道这是i的一个问题,虽然我无法弄明白。

1 个答案:

答案 0 :(得分:1)

更改变量的声明。您不需要Range对象数组。

更改

Dim JobTwoTime() As Range

Dim JobTwoTime As Double

并改变

JobTwoTime(i) = Range("Mi")

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

.Value不是必需的,因为它是Range对象的默认属性,但我使用它。