循环通过工作表时对象定义的错误

时间:2018-06-12 17:15:23

标签: vba excel-vba excel

我有一个我正在研究的项目需要循环遍历一系列工作表,每个工作表都是在一个单独的工作表中以一系列值命名的。然后,我在每个工作表上执行一些函数,将公式添加到下一个空列。但是,我的代码错误地出现在这一行:

Worksheets(Name).Range(.Cells(2, LastColumn + 1)).Formula = "=F2"     

具体错误是

  

"应用程序定义或对象定义的错误"

我不确定为什么会这样。我已经改变了我引用工作表的方式,在With-blocks等处移动。请注意,这只是一个Sub,我已经测试了完整宏的不同组件。任何关于此错误的帮助或我做错了什么都将不胜感激!

Sub Test()
    Dim ws2 As Worksheet
    Dim wb As Workbook
    Dim LastRow As Long, LastColumn As Long
    Dim LastRow2 As Long
    Dim Name As Variant, SheetR As Variant

    Set wb = ActiveWorkbook
    Set ws2 = wb.Sheets("Comm")

    LastRow2 = 6

    'sort each sheet on date descending
    With wb
        SheetR = ws2.Range("A3:A" & (LastRow2 + 2))
        For Each Name In SheetR
            LastColumn = 0
            LastRow = 0
            With Worksheets(Name)
                Worksheets(Name).Rows("1:1").AutoFilter
                Worksheets(Name).AutoFilter.Sort.SortFields.Add Key:=Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                With Worksheets(Name).AutoFilter.Sort
                    .Header = xlYes
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                LastColumn = Worksheets(Name).Cells(1, Columns.Count).End(xlToLeft).Column
                LastRow = Worksheets(Name).Cells(Rows.Count, 1).End(xlUp).Row
                If LastRow = 1 Then
                ElseIf LastRow = 2 Then
                ElseIf LastRow = 3 Then
                ElseIf LastRow = 4 Then
                ElseIf LastRow > 4 Then
                    'The error is occurring at this next line
                    Worksheets(Name).Range(.Cells(2, LastColumn + 1)).Formula = "=F2"
                    Worksheets(Name).Range(.Cells(3, LastColumn + 1)).Formula = "=F3+O2"
                    Worksheets(Name).Range(.Cells(3, LastColumn + 1)).Select
                    Selection.AutoFill Destination:=Sheets(CStr(Name)).Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault
                Else
                End If
            End With
        Next Name
    End With

End Sub

2 个答案:

答案 0 :(得分:2)

看看我的注释。

Sub Test()
    Dim ws2 As Worksheet, wb As Workbook, LastRow As Long, LastColumn As Long, LastRow2 As Long, Name As Variant, SheetR As Variant
    Set wb = ActiveWorkbook
    Set ws2 = wb.Sheets("Comm")
    LastRow2 = 6
    'sort each sheet on date descending
    SheetR = ws2.Range("A3:A" & (LastRow2 + 2))
    For Each Name In SheetR
        LastColumn = 0
        LastRow = 0
        With Worksheets(Name)
            .Rows("1:1").AutoFilter
            .AutoFilter.Sort.SortFields.Add Key:=.Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal  'Added "." before the Key range
            With .AutoFilter.Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column 'Added "." before Columns.Count
            LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Added "." before Rows.Count
            If LastRow = 1 Then
            ElseIf LastRow = 2 Then
            ElseIf LastRow = 3 Then
            ElseIf LastRow = 4 Then
            ElseIf LastRow > 4 Then
                'The error is occurring at this next line
            .Cells(2, LastColumn + 1).Formula = "=F2"  'Removed .range() as this is only a single cell being called
            .Cells(3, LastColumn + 1)).Formula = "=F3+O2"  'Removed .range() as this is only a single cell being called
            .Cells(3, LastColumn + 1)).Select  'Removed .range() as this is only a single cell being called
            Selection.AutoFill Destination:=Sheets(CStr(Name)).Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault 'Need to check your qualifiers in this line... using source, not destination
            Else
            End If
        End With
    Next Name
End Sub

Edit1:修复了对单个单元格上的range()的不适当调用。转发给你/ PeterT叫它出来

答案 1 :(得分:0)

您已经花时间构建了一个带有工作表(名称)...结束有块但未能利用它。另外,除非你为启动和停止提供两个.Cells,否则.Range(.Cells(...))是不好的语法。

使用工作表(名称)重写...结束于块,

...
With Worksheets(Name)
    .Rows("1:1").AutoFilter
    .AutoFilter.Sort.SortFields.Add Key:=.Range("H1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    If LastRow = 1 Then
    ElseIf LastRow = 2 Then
    ElseIf LastRow = 3 Then
    ElseIf LastRow = 4 Then
    ElseIf LastRow > 4 Then
        'The error is occurring at this next line
        .Cells(2, LastColumn + 1).Formula = "=F2"
        .Cells(3, LastColumn + 1).Formula = "=F3+O2"
        .Cells(3, LastColumn + 1).AutoFill Destination:=.Range(.Cells(4, LastColumn + 1), .Cells(LastRow, LastColumn + 1)), Type:=xlFillDefault
    Else
    End If
End With
...