当我尝试运行我的代码时,我收到错误:
运行时错误'1004':对象'_Global'的方法'范围'失败
我已经搜索了不同的解决方案,但我不知道如何解决它,我经历了几次代码,但问题仍然存在。
所有这些代码都在Excel VBA中,并且范围函数似乎是导致问题的因素,因此我无法找到可能导致范围有缺陷的确切更改。
Sub qq()
Dim d(23), LastRow As Integer
Dim c(23) As String
Dim e(23) As Date
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 0 To LastRow
If Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 32 And Range("l" & (i + 1)).Value = 32 Then
c(0) = Range("d" & i).Value
d(0) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(0) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 10 And Range("l" & (i + 1)).Value = 10 Then
c(1) = Range("d" & i).Value
d(1) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(1) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 2 And Range("l" & (i + 1)).Value = 2 Then
c(2) = Range("d" & i).Value
d(2) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(2) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 3 And Range("l" & (i + 1)).Value = 3 Then
c(3) = Range("d" & i).Value
d(3) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(3) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 22 And Range("l" & (i + 1)).Value = 22 Then
c(4) = Range("d" & i).Value
d(4) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(4) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 8 And Range("l" & (i + 1)).Value = 8 Or (Range("l" & i).Value = 27 And Range("l" & (i + 1)).Value = 27) Then
c(5) = Range("d" & i).Value
d(5) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(5) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 15 And Range("l" & (i + 1)).Value = 15 Or (Range("l" & i).Value = 28 And Range("l" & (i + 1)).Value = 28) Then
c(6) = Range("d" & i).Value
d(6) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(6) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 6 And Range("l" & (i + 1)).Value = 6 Then
c(7) = Range("d" & i).Value
d(7) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(7) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 4 And Range("l" & (i + 1)).Value = 4 Then
c(8) = Range("d" & i).Value
d(8) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(8) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 19 And Range("l" & (i + 1)).Value = 19 Then
c(9) = Range("d" & i).Value
d(9) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(9) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 5 And Range("l" & (i + 1)).Value = 5 Then
c(10) = Range("d" & i).Value
d(10) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(10) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 9 And Range("l" & (i + 1)).Value = 9 Then
c(11) = Range("d" & i).Value
d(11) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(11) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 29 And Range("l" & (i + 1)).Value = 29 Then
c(12) = Range("d" & i).Value
d(12) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(12) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 17 And Range("l" & (i + 1)).Value = 17 Then
c(13) = Range("d" & i).Value
d(13) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(13) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 13 And Range("l" & (i + 1)).Value = 13 Then
c(14) = Range("d" & i).Value
d(14) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(14) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 18 And Range("l" & (i + 1)).Value = 18 Then
c(15) = Range("d" & i).Value
d(15) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(15) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 14 And Range("l" & (i + 1)).Value = 14 Or (Range("l" & i).Value = 33 And Range("l" & (i + 1)).Value = 33) Then
c(16) = Range("d" & i).Value
d(16) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(16) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 20 And Range("l" & (i + 1)).Value = 20 Or (Range("l" & i).Value = 21 And Range("l" & (i + 1)).Value = 21) Then
c(17) = Range("d" & i).Value
d(17) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(17) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 11 And Range("l" & (i + 1)).Value = 11 Then
c(18) = Range("d" & i).Value
d(18) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(18) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 12 And Range("l" & (i + 1)).Value = 12 Then
c(19) = Range("d" & i).Value
d(19) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(19) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
ElseIf Range("c" & i).Value = 0.25 And Range("c" & (i + 1)).Value = 0.25 And Range("l" & i).Value = 25 And Range("l" & (i + 1)).Value = 25 Then
c(20) = Range("d" & i).Value
d(20) = Application.WorksheetFunction.Max((Range("a" & i).Value), (Range("a" & (i + 1)).Value))
e(20) = Application.WorksheetFunction.Min(Range("k" & i).Value, Range("k" & (i + 1)).Value)
Else
End If
Next
MsgBox (c(0))
End Sub
答案 0 :(得分:1)
您正在开始循环,i
为0,表单中不存在行。表格中的第一个单元格为(1,1)
,即A1
所以你的Range("l" & i)
是Range("L0")
,在循环开始时,它不存在,因此你的错误。
你的循环需要从至少1开始,即For i = 1 To LastRow