获取运行时错误“6”:溢出错误

时间:2017-11-08 12:52:37

标签: excel excel-vba excel-2010 vba

代码工作正常,直到几天前,但现在得到主题行错误。帮助

Sub CopyRows()
    Dim bottomL As Integer
    Dim x As Integer
        bottomL = Sheets("Pacer").Range("L" & Rows.Count).End(xlUp).Row: x = 1

    Dim c As Range
    For Each c In Sheets("Pacer").Range("A1:L" & bottomL)
        If (c.Value = "AFSB" Or c.Value = "TEIGIP4T" Or c.Value = "EPP") Then
            Intersect(c.Parent.Columns("A:Q"), c.EntireRow).Copy Worksheets("Portfolio").Range("A" & x + 1)
            x = x + 1
        End If
    Next c

End Sub

2 个答案:

答案 0 :(得分:1)

变量 bottomL As Integer

超过32,767行时会出现溢出错误。尝试声明它

bottomL As Long

编辑:该规则适用于X,并且正在递增。

答案 1 :(得分:0)

试试这个

Option Explicit

Sub CopyRows()
    Dim bottomL As Long
    Dim x As Long
        bottomL = Sheets("Pacer").Range("L" & Rows.CountLarge).End(xlUp).Row: x = 1

    Dim c As Range
    For Each c In Sheets("Pacer").Range("A1:L" & bottomL)
        If (c.Value = "AFSB" Or c.Value = "TEIGIP4T" Or c.Value = "EPP") Then
            Intersect(c.Parent.Columns("A:Q"), c.EntireRow).Copy Worksheets("Portfolio").Range("A" & x + 1)
            x = x + 1
        End If
    Next c

End Sub

原因解释here