简体   繁体   English

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

[英]Object defined error while looping through worksheets

I have a project I am working on that entails looping through a series of worksheets, each of which is named after a series of values in a separate sheet. 我有一个正在处理的项目,需要循环浏览一系列工作表,每个工作表均以单独工作表中的一系列值命名。 I then perform some functions on each sheet, adding a formula to the next empty column. 然后,我在每个工作表上执行一些功能,将公式添加到下一个空列。 However, my code is erroring out at this line: 但是,我的代码在这一行出错了:

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

The specific error is 具体错误是

"Application-defined or Object-defined error" “应用程序定义或对象定义的错误”

and I'm not sure why this is occurring. 我不确定为什么会这样。 I've switched up the way I reference the worksheets, moving around the With-blocks etc. Note that this is just a Sub where I've been testing out different components of the full macro. 我已经切换了引用工作表的方式,在With块周围移动。请注意,这只是我一直在测试完整宏的不同组成部分的Sub。 Any help on this error or what I'm doing wrong would be appreciated! 对此错误或我做错的任何帮助,将不胜感激!

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

Look at my annotation. 看我的注释。

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: Fixed innapropriate call for range() on a single cell. Edit1:修复了单个单元格上对range()的不当调用。 Props to u/PeterT for calling it out 向u / PeterT发出呼唤的道具

You've taken the time to build a With Worksheets(Name) ... End With block but failed to take advantage of it. 您花了一些时间来构建With With Worksheets(Name)... End With块,但未能利用它。 Additionally, .Range(.Cells(...)) is bad syntax unless you provide two .Cells for a start and stop. 此外,.Range(.Cells(...))的语法不好,除非您提供两个.Cell作为开始和停止位置。

To rewrite your With Worksheets(Name) ... End With block, 要重写您的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