簡體   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