提问人:dfs 提问时间:11/3/2023 最后编辑:Communitydfs 更新时间:11/6/2023 访问量:65
自动筛选行错误:运行时错误“91”:对象变量或未设置块变量
Autofilter rows error: Run-time error '91': Object variable or With block variable not set
问:
我正在尝试向多个用户发送多行电子邮件。我使用自动过滤器来过滤掉行。
有时它会出错:
运行时错误“91”:对象变量或未设置块变量
Set rng = Sheet20.AutoFilter.Range.Resize(Sheet20.AutoFilter.Range.Rows.Count - 1).Columns("$M:$U")
完整代码:
Sub CashEmail()
Application.ScreenUpdating = False
Sheet20.AutoFilterMode = False
Dim OutApp As Object, OutMail As Object
Dim rng As Range, i As Long, v As Variant
v = Sheet20.Range("$M$20:$V" & Sheet20.Cells(Sheet20.Rows.Count, "$M").End(xlUp).Row).Value ' Modify the range to include columns A to L and V to W
Set OutApp = CreateObject("Outlook.Application")
With CreateObject("Scripting.Dictionary")
For i = 2 To UBound(v, 1)
If Not .Exists(v(i, 10)) And v(i, 10) <> "" Then
.Add v(i, 10), Nothing
With Sheet20.Range("$M21:V$21") ' Modify the range to include columns A to B and E to F
.CurrentRegion.AutoFilter Field:=10, Criteria1:=v(i, 10)
Set rng = Sheet20.AutoFilter.Range.Resize(Sheet20.AutoFilter.Range.Rows.Count - 1).Columns("$M:$U")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = v(i, 10)
.Subject = "xxxx"
.HTMLBody = RangetoHTML(rng)
.Display
End With
End With
End If
Next i
End With
Sheet20.AutoFilterMode = False
Sheet20.ShowAllData
Application.ScreenUpdating = True
End Sub
我试图在开始时关闭自动过滤器 = false。
答: 暂无答案
评论
Sheet20.AutoFilterMode
Sheet20.AutoFilter.Range