简体   繁体   中英

Run-time error '1004' - Method 'Range' of object '_Global' failed

in the code:

Sub sync()

          Dim Dindex As Long 'the index of the D value we are on currently.
          Dim Aindex As Long 'the index of the A value we are on currently
          Dim Gindex As Long 'the index of the A value we are on currently
          Dim Dvalue As Double 'the cell in D value we want to normalize by -time
          Dim Avalue As Double 'the A cell value - time
          Dim Bvalue As Double
          Dim Hvalue As Double
          Dim Gvalue As Double 'the G cell value - time

          Dindex = 3
          Aindex = 3
          Gindex = 3

          Dvalue = Cells(Dindex, 4).Value 'start value

          Avalue = Cells(Aindex, 1).Value 'start value

          Gvalue = Cells(Gindex, 7).Value

          Do While Dvalue <> 0
                    Do While Avalue < Dvalue
                              Aindex = Aindex + 1
                              Avalue = Range("A" & CStr(Aindex)).Value
                    Loop
                    Do While Gvalue < Dvalue
                              Gindex = Gindex + 1
                              Gvalue = Range("G" & CStr(Gindex)).Value
                    Loop
                    Bvalue = Cells(Aindex, 2)
                    If Avalue <> Dvalue Then
                              Aindex = Aindex - 1
                              Bvalue = (Bvalue + Range("A" & CStr(Aindex)).Value) / 2
                    End If
                    Hvalue = Cells(Gindex, 8).Value
                    If Gvalue <> Dvalue Then
                              Gindex = Gindex - 1
                              Hvalue = (Hvalue + Range("G" & CStr(Gindex)).Value) / 2
                    End If
                    Cells(Dindex, 10).Value = Dvalue
                    Cells(Dindex, 11).Value = Bvalue
                    Cells(Dindex, 12).Value = Hvalue
                    Dindex = Dindex + 1
                    Dvalue = Cells(Dindex, 4).Value
          Loop

End Sub

I got the error when in the line: Avalue = Range("A" & CStr(Aindex)).Value when the value Aindex got to 1048577. I suspect that the problem is in me trying to reach to such a large cell, the goal of the code is to put 3 different time systems values on the same time system, so I need to reach beyond that cell.

thanks to BigBen I found out that that the maximum value of an excel column is 1048576, and successfully fixed my code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM