Excel VBA循环中缺少数字会产生错误

Excel VBA循环中缺少数字会产生错误,excel,vba,Excel,Vba,我有以下代码,它使用数字序列搜索不同的可用产品 如果一个产品编号丢失,此代码将抛出错误消息 我的代码示例:- Sub mycode() icnt1 = 0 max1 = Range("N1").Value For store = 1 To max1 icnt1 = icnt1 + 1 Windows(File6).Activate ActiveSheet.Range("$A$1:$K$10000").AutoFilte

我有以下代码,它使用数字序列搜索不同的可用产品

如果一个产品编号丢失,此代码将抛出错误消息

我的代码示例:-

Sub mycode()

    icnt1 = 0
    max1 = Range("N1").Value

    For store = 1 To max1
        icnt1 = icnt1 + 1

        Windows(File6).Activate
        ActiveSheet.Range("$A$1:$K$10000").AutoFilter Field:=1, Criteria1:=icnt1

        Range("H2", Range("H" & Rows.Count).End(xlUp)).Select
        Selection.Copy

        Sheets("template").Copy Before:=Sheets("template")
        ActiveSheet.Name = "s" & icnt1

        Sheets("s" & icnt1).Select
        Range("T3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Next

End Sub
如果我有一个准确的产品列表(1、2、3、4、5等),那么我不会面临任何问题


如果一个或多个产品丢失(如1、2、4、5、6等),我将收到一条错误消息。我试图在“错误恢复下一步”中添加一个选项
,但这也不起作用。

您需要首先检查过滤器是否返回任何内容。将其分配到一个范围,并在继续之前检查它是否为空

请阅读(1)使用Option Explicit并声明变量,(2)避免


在继续复制/添加/粘贴之前,需要检查过滤器范围中是否有可见的单元格

Sub mycode()

    dim icnt1  as long, max1 as long, store as long
    icnt1 = 0
    max1 = Range("N1").Value
    For store = 1 To max1
        icnt1 = icnt1 + 1

        with Windows(File6)
            if .autofiltermode then .autofiltermode = false
            .Range("$A$1:$K$10000").AutoFilter Field:=1, Criteria1:=icnt1
            with .resize(.rows.count-1, 1).offset(1, 7)
                if cbool(application.subtotal(103, .cell)) then
                    .Copy

                     Sheets("template").Copy Before:=Sheets("template")
                     ActiveSheet.Name = "s" & icnt1
                     Sheets("s" & icnt1).Select

                     Range("T3").PasteSpecial Paste:=xlPasteValues
                 end if
             end with
         end with

    Next store

End Sub

您的代码的目标是什么以及它的实际用途还不清楚。获取错误消息并不能很好地描述您的问题。请包括错误以及从何处获得的信息。感谢您的消息,我的代码正在尝试复制每个产品的销售情况(文件6的H列),并将其粘贴到新添加的表格s1、s2等的T列。我在搜索项目3时收到错误消息,但该消息不可用。实际上,我没有回答问题,但是如果它们的值完全相同,为什么要同时使用
icnt1
store
?重命名后,为什么要重新选择已存在的活动工作表?还有,读一读。。。(例如,
ActiveSheet.Range(“T3”).PasteSpecial
)嗯。。。使用
WorksheetFunction.CountIf
在过滤前检查A列中的
icnt1
是否会更快/更高效?对于特定的单个筛选值,是的,但这更通用。
Sub mycode()

    dim icnt1  as long, max1 as long, store as long
    icnt1 = 0
    max1 = Range("N1").Value
    For store = 1 To max1
        icnt1 = icnt1 + 1

        with Windows(File6)
            if .autofiltermode then .autofiltermode = false
            .Range("$A$1:$K$10000").AutoFilter Field:=1, Criteria1:=icnt1
            with .resize(.rows.count-1, 1).offset(1, 7)
                if cbool(application.subtotal(103, .cell)) then
                    .Copy

                     Sheets("template").Copy Before:=Sheets("template")
                     ActiveSheet.Name = "s" & icnt1
                     Sheets("s" & icnt1).Select

                     Range("T3").PasteSpecial Paste:=xlPasteValues
                 end if
             end with
         end with

    Next store

End Sub