Run-time error ' 6' overflow: Microsoft Excel VBA

前端 未结 2 1060
旧巷少年郎
旧巷少年郎 2021-01-27 18:45

I tried to solve this overflow in my excel sheet and still not able to solve it. I knew that my data is really a lot (more than 32767 rows). please take a look a my code and hel

相关标签:
2条回答
  • 2021-01-27 18:53

    Change all your Integer declarations to Long

    Dim Ctr As Long
    
    0 讨论(0)
  • 2021-01-27 19:09

    you've been already been given the answer

    you may also consider the following refcatoring of your code:

    Option Explicit
    
    Sub Remove()
        '
        ' Remove Macro
        '
        Dim Ctr As Long
        Dim EOF As Boolean
        Dim LstStr As String
        Dim CurStr As String
    
        Ctr = 8
        Do
            CurStr = Join(Application.Transpose(Application.Transpose(Range("A" & Ctr).Resize(, 11).Value)), "-")
            If CurStr <> "----------" Then
                If CurStr = LstStr Then
                    'Clear content A to N, except L
                    Range("A" & Ctr & ":L" & Ctr & ",N" & Ctr & ":O" & Ctr).ClearContents
                Else
                    LstStr = CurStr
                End If
            Else
                EOF = True
            End If
            Ctr = Ctr + 1
        Loop While Not EOF
    End Sub
    

    where

    • Option Explicit

      would have spotted that Cutstr = "" typo (it should have been Curstr = "")

    • no need for

      Dim CtrS As String
      

      and all its subsequent initialization and use

      since Ctr variable of Long type will be implicitly cast to a String when combined to another String variable through the & operator

    • no need for

      LstStr = ""
      Cutstr = ""
      

      since String variables are always initialized to an empty one at their declaration

    • no need for

      EOF = False
      

      since Boolean variables are always initialized to False at their declaration

    • While Not EOF

      moved to the Loop part of the Do - Loop block

      since EOF first value is always False

    • change

      CurStr = Range("A" & CtrS).Value & "-" & _
               Range("B" & CtrS).Value & "-" & _
               ...
               Range("K" & CtrS).Value
      

      to

      CurStr = Join(Application.Transpose(Application.Transpose(Range("A" & Ctr).Resize(, 11).Value)), "-")
      

      where you take advantage of the Resize() property of Range object and the Join() function

      the double Application.Transpose() is there to return a one dimensional array since:

      • Range("A" & Ctr).Resize(, 11).Value

        would return a two dimensional array of 1 row and 11 columns

      • Application.Transpose(Range("A" & Ctr).Resize(, 11).Value))

        would return a 2 dimensional array of 11 rows and 1 columns

      • Application.Transpose(Application.Transpose(Range("A" & Ctr).Resize(, 11).Value)))

        would finally return a 1 dimensional array of 11 elements

    • Range("A" & Ctr & ":L" & Ctr & ",N" & Ctr & ":O" & Ctr)

      takes advantage of one of possible Range() arguments specifications, specifically

      Range("firstRangeAddress, secondRangeAddress"")
      

      where that comma between the two addresses acts as a Union() operator

    0 讨论(0)
提交回复
热议问题