繁体   English   中英

遍历工作表时对象定义的错误

[英]Object defined error while looping through worksheets

我有一个正在处理的项目,需要循环浏览一系列工作表,每个工作表均以单独工作表中的一系列值命名。 然后,我在每个工作表上执行一些功能,将公式添加到下一个空列。 但是,我的代码在这一行出错了:

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

具体错误是

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

我不确定为什么会这样。 我已经切换了引用工作表的方式,在With块周围移动。请注意,这只是我一直在测试完整宏的不同组成部分的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

看我的注释。

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()的不当调用。 向u / PeterT发出呼唤的道具

您花了一些时间来构建With With Worksheets(Name)... End With块,但未能利用它。 此外,.Range(.Cells(...))的语法不好,除非您提供两个.Cell作为开始和停止位置。

要重写您的With Worksheets(Name)... End With块,

...
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
...

暂无
暂无

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

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