使用自动筛选时Excel VBA循环错误

时间:2018-10-11 13:51:52

标签: excel vba excel-vba

我在Excel文件中有2个工作表 工作表1 =“说明”,我的客户代码从A2保存到A 工作表2 =我需要根据该表自动过滤B列               工作表1中的客户代码               将整个表格复制到新的Excel工作表中               并将其保存在具有自定义名称的文件夹中

下面是我编写的代码。 但是,在宏末尾有错误,我无法按如下所述进行修复

错误1 “ .Rows(” 1:1“)。选择”

错误2 该宏仅创建1个文件,无法循环保存

错误3 有时出现错误,并且t表示无法保存文件

Sub ARSOA()

Dim varItemsToReplace As Variant
Dim varItem As Variant
Dim wksSource As Worksheet
Dim wksDest As Worksheet
Dim rngSource As Range
Dim rngSource2 As Range
Dim rngCell As Range

Set wksSource = Worksheets("Instruction")
Set wksDest = Worksheets("AR")

With wksSource
    Set rngSource = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
End With

For Each rngCell In rngSource
    With wksDest
        .Rows("1:1").Select
         Selection.AutoFilter
         ActiveSheet.Range("$A$1:$L$136").AutoFilter Field:=2, Criteria1:=rngCell.Value
         Cells.Select
         Selection.Copy
         Workbooks.Add
         ActiveSheet.Paste
         Range("J3").Select
         ChDir "C:\Users\XXXX\Desktop\Automation\XXXX"
        ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\XXXX\Desktop\SOA\" & ActiveSheet.Range("C2").Value & " - " & ActiveSheet.Range("B2").Value & ".xlsx", FileFormat:= _
       xlOpenXMLWorkbook, CreateBackup:=False
    End With
   Next rngCell

  End Sub

能否请您指出错误

0 个答案:

没有答案