VBA错误1004选择工作表类字段

时间:2017-05-23 20:37:39

标签: excel vba excel-vba runtime-error

我有一个代码,在检查第2列中的电子邮件地址是否有效以及第6列中的值是否大于0之后,将电子邮件发送给联系人。

如果我单击按钮保存工作簿并尝试运行宏,则会崩溃并输出错误并显示错误:错误1004选择工作表类字段的方法。关闭错误并浏览工作表后,选择一些随机单元格,然后尝试运行宏,它运行正常,电子邮件发送出去。这是调试器突出显示的行: .Parent.Select

    Private Sub SendUpdates()
 Dim aEmailList As Variant
 Dim indx As Long
 Dim rng As Range
 Dim ws As Worksheet
 Dim lRow As Long
 Dim wsORPpivot As Worksheet
 Dim wsDSPContacts As Worksheet

 With ThisWorkbook
     Set wsORPpivot = .Worksheets(cShORPpivot)
     Set wsDSPContacts = .Worksheets(cShDSPContacts)
 End With

 Set ws = ActiveSheet

 'On Error GoTo ErrHandler

 With Application
    .ScreenUpdating = False
    .EnableEvents = False
 End With

 With wsDSPContacts
    aEmailList = .Cells(1).CurrentRegion.Value
 End With

 If Not IsArray(aEmailList) Then Exit Sub

 For indx = 2 To UBound(aEmailList)
    If ValidEmail(CStr(aEmailList(indx, 2))) And aEmailList(indx, 6) > 0 Then

    wsORPpivot.PivotTables("ORP").PivotFields("DSP").CurrentPage = aEmailList(indx, 1)
    wsORPpivot.Cells.EntireColumn.AutoFit

        lRow = wsORPpivot.Cells(Rows.CountLarge, "A").End(xlUp).Row
        Set rng = wsORPpivot.Range("A1:J" & lRow)
        ActiveWorkbook.EnvelopeVisible = True
        With rng
            .Parent.Select
            With .Parent.MailEnvelope
                '.Introduction = aEmailList(indx, 4)
                With .Item
                    .To = aEmailList(indx, 2)
                    '.CC = aEmailList(indx, 3)
                    .Subject = aEmailList(indx, 4)
                    '.Intro = aEmailList(indx, 5)
                    .Send
                End With
            End With
        End With

        With wsORPpivot ' Unselect the range, clear pivot filters, fit columns
            .Cells(1).Select
            .PivotTables("ORP").PivotFields("DSP").ClearAllFilters
            .Cells.EntireColumn.AutoFit
        End With
    End If
 Next
 ActiveWorkbook.EnvelopeVisible = False
 ws.Select

ErrHandler:
ActiveWorkbook.EnvelopeVisible = False
 With Application
    .ScreenUpdating = True
    .EnableEvents = True
 End With

 Set ws = Nothing
 Set rng = Nothing
 If Err.Number <> 0 Then
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description, vbCritical, cAppName
    Err.Clear
 End If

End Sub

0 个答案:

没有答案