简体   繁体   English

运行时错误6 - 溢出,变量设置为Long

[英]Runtime error 6 - Overflow, variable set as Long

I am having problem executing the copy+paste for a filtered data. 我在执行过滤数据的复制+粘贴时遇到问题。 My code does not encounter error if filtered field will have a 0 result or > 1. However, if there's 1 record visible after filter, Runtime error 6 appears. 如果过滤字段的结果为0或> 1,则我的代码不会遇到错误。但是,如果过滤后有1条记录可见,则会出现运行时错误6。 Please see code used below: 请参阅下面使用的代码:

Dim wsDue As Worksheet
Dim wsTarget As Worksheet
Dim y As Long
Dim x As Long

x = Range("A65536").End(xlUp).Row

Range("A1").AutoFilter Field:=2, Criteria1:=Array("Yes"), Operator:=xlFilterValues
Set wsDue = Worksheets("Due")
Set wsTarget = Worksheets("Target List Consolidated")
y = wsDue.Range("B" & wsDue.Rows.Count).End(xlUp).Row

If wsDue.Range(wsDue.Cells(2, 2), wsDue.Cells(y, 2)).SpecialCells(xlCellTypeVisible).Count > 1 Then
    wsDue.Range("B2:B" & x).Copy
    wsTarget.Range("A65536").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
Else:
End If

First off, make sure your Excel (aka MS-Office) has all applicable service packs. 首先,确保您的Excel(也称为MS-Office)具有所有适用的服务包。 The problem with a single filtered row being interpreted as all rows was a known bug but it was rectified in subsequent service packs. 将单个筛选行解释为所有行的问题是已知错误,但在后续Service Pack中已得到纠正。

You can also apply some 'best practices' code to avoid it happening altogether. 您还可以应用一些“最佳实践”代码,以避免它完全发生。 The Range.CurrentRegion property can be used to localize the Range.AutoFilter Method . Range.CurrentRegion属性可用于本地化Range.AutoFilter方法 Use progressive With ... End With statements to further isolate the data to be transfered. 使用progressive With ... End With语句进一步隔离要传输的数据。

Dim wsDue As Worksheet, wsTarget As Worksheet

With Worksheets("Due")
    If .AutoFilterMode Then .AutoFilterMode = False
    'work on the contiguous block of cells radiating out from A1
    With .Cells(1, 1).CurrentRegion
        'apply the AutoFilter
        .AutoFilter Field:=2, Criteria1:=Array("Yes"), Operator:=xlFilterValues
        'shift one row down (off the header row) and resize one less row
        'isolate column B
        With .Offset(1, 1).Resize(.Rows.Count - 1, 1)
            'non-destructive test to see if there are any rows visible
            If CBool(Application.Subtotal(103, .Cells)) Then
                Set wsTarget = Worksheets("Target List Consolidated")
                .Copy
                wsTarget.Range("A65536").End(xlUp).Offset(1).PasteSpecial _
                    Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
            End If
        End With
    End With
End With

I figured out another way to work around as a solution. 我想出了另一种解决方案。 I used the following code instead: 我使用以下代码:

Dim x As Long
Dim wsDue As Worksheet
Dim wsTarget As Worksheet

x = Range("A65536").End(xlUp).Row
Range("A1").AutoFilter Field:=2, Criteria1:=Array("Yes"), Operator:=xlFilterValues
Set wsDue = Worksheets("Due")
Set wsTarget = Worksheets("Target List Consolidated")

If wsDue.Range("B1:B" & x).Offset(1, 0).SpecialCells(xlCellTypeVisible).Count > 1 Then
    wsDue.Range("B1:B" & x).Offset(1, 0).SpecialCells(xlCellTypeVisible).Copy
    wsTarget.Range("A65536").End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
Else:
End If

The solution was able to get the result of filter and will copy the desired range excluding the header in Row 1. 该解决方案能够获得过滤器的结果,并将复制除第1行中的标题之外的所需范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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