Autofilter error: the object invoked has disconnected from its clients
Autofilter error: the object invoked has disconnected from its clients
我在其他帖子上看到过此错误消息,但 none 符合我的情况。
我一个接一个地完成了一些自动筛选功能,检查目标单元格是否为空,如果为空则继续。
代码一直运行到第 6 次迭代,其中出现错误
"the object invoked has disconnected from its clients"
Option Explicit
Private Sub CommandButton49_Click()
'
Dim Wsdnd As Worksheet
Set Wsdnd = Sheets("DO NOT DELETE")
Dim A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ... (more variables) As Range
Set A3 = Wsdnd.Range("BD3")
Set A4 = Wsdnd.Range("BD4")
Set A5 = Wsdnd.Range("BD5")
Set A6 = Wsdnd.Range("BD6")
Set A7 = Wsdnd.Range("BD7")
Set A8 = Wsdnd.Range("BD8")
... (More variables)
'
Wsdnd.Range("BC3:BE50").Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value" Lists on DO NOT DELETE sheet
Application.Calculation = xlManual 'Restarts manual calculations only for workbook speed
'
'Filter #4
If Not IsEmpty(A6.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE6"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A6.Value, Operator:=xlFilterValues
Else
End If
'
'Filter #5
If Not IsEmpty(A7.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE7"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A7.Value, Operator:=xlFilterValues
Else
End If
'
'Filter #6
If Not IsEmpty(A8.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE8"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A8.Value, Operator:=xlFilterValues
Else
End If
'
'... Filters continue
变量中调用的数据是:
字符串(“F8442”)
字符串(“未指定”)
整数(“345”)
我不确定上面的数据类型是否会成为问题,因为它们指的是一个单元格,而且很多时候单元格值可以从文本更改为数字,反之亦然。
我遗漏了 Range.Calculate
和 Application.Calculation = xlManual
,因为我认为它们不会对代码产生影响,但由于我不确定而将它们添加到此处。当从代码中取出时,它会在同一位置给出错误。
好吧,问题似乎是双重的,一个是 Criteria 需要是一个字符串并且它得到一个整数,其次 Operator:=xlFilterValues 被放置在只有一个变量的指令之后被认为很糟糕excel 众神。
至少就我而言,这不再是问题。希望这些信息对以后的其他人有所帮助!
修改后的代码:
Option Explicit
Private Sub CommandButton49_Click()
'
Dim Wsdnd As Worksheet
Set Wsdnd = Sheets("DO NOT DELETE")
Dim A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ... (more variables) As Range
A4 = Wsdnd.Range("BD4").Value
A5 = Wsdnd.Range("BD5").Value
A6 = Wsdnd.Range("BD6").Value
A7 = Wsdnd.Range("BD7").Value
A8 = Wsdnd.Range("BD8").Value
... (More variables)
'
Wsdnd.Range("BC3:BE50").Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value" Lists on DO NOT DELETE sheet
Application.Calculation = xlManual 'Restarts manual calculations only for workbook speed
'
'Filter #4
If Not IsEmpty(A6) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE6"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A6
Else
End If
'
'Filter #5
If Not IsEmpty(A7) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE7"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A7
Else
End If
'
'Filter #6
If Not IsEmpty(A8) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE8"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A8
Else
End If
过滤工作表
Option Explicit
Private Sub CommandButton49_Click()
FilterWorksheet
End Sub
Sub FilterWorksheet()
Const ProcName As String = "FilterWorksheet"
On Error GoTo ClearError
Const sName As String = "DO NOT DELETE"
Const srgAddress As String = "BC3:BE50" ' ?
Const srrgAddress As String = "BD4:BD10" ' resize
Const scOffset As Long = 1 ' ('BE4:BE10')
Const dName As String = "Database"
Const drgAddress As String = "B23:BL71499"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim srg As Range: Set srg = sws.Range(srgAddress)
Dim srrg As Range: Set srrg = sws.Range(srrgAddress)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
If dws.AutoFilterMode Then dws.AutoFilterMode = False
Dim drg As Range: Set drg = dws.Range(drgAddress)
Dim dhrg As Range: Set dhrg = drg.Rows(1)
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
srg.Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value"
Dim sCell As Range
Dim sValue As Variant
Dim dField As Variant
For Each sCell In srrg.Cells
sValue = sCell.Value
If Not IsError(sValue) Then
If Len(CStr(sValue)) > 0 Then
dField = Application _
.Match(sCell.Offset(, scOffset).Value, dhrg, 0)
If IsNumeric(dField) Then
drg.AutoFilter Field:=dField, Criteria1:=sValue
End If
End If
End If
Next sCell
SafeExit:
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume SafeExit
End Sub
我在其他帖子上看到过此错误消息,但 none 符合我的情况。
我一个接一个地完成了一些自动筛选功能,检查目标单元格是否为空,如果为空则继续。
代码一直运行到第 6 次迭代,其中出现错误
"the object invoked has disconnected from its clients"
Option Explicit
Private Sub CommandButton49_Click()
'
Dim Wsdnd As Worksheet
Set Wsdnd = Sheets("DO NOT DELETE")
Dim A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ... (more variables) As Range
Set A3 = Wsdnd.Range("BD3")
Set A4 = Wsdnd.Range("BD4")
Set A5 = Wsdnd.Range("BD5")
Set A6 = Wsdnd.Range("BD6")
Set A7 = Wsdnd.Range("BD7")
Set A8 = Wsdnd.Range("BD8")
... (More variables)
'
Wsdnd.Range("BC3:BE50").Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value" Lists on DO NOT DELETE sheet
Application.Calculation = xlManual 'Restarts manual calculations only for workbook speed
'
'Filter #4
If Not IsEmpty(A6.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE6"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A6.Value, Operator:=xlFilterValues
Else
End If
'
'Filter #5
If Not IsEmpty(A7.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE7"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A7.Value, Operator:=xlFilterValues
Else
End If
'
'Filter #6
If Not IsEmpty(A8.Value) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE8"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A8.Value, Operator:=xlFilterValues
Else
End If
'
'... Filters continue
变量中调用的数据是:
字符串(“F8442”)
字符串(“未指定”)
整数(“345”)
我不确定上面的数据类型是否会成为问题,因为它们指的是一个单元格,而且很多时候单元格值可以从文本更改为数字,反之亦然。
我遗漏了 Range.Calculate
和 Application.Calculation = xlManual
,因为我认为它们不会对代码产生影响,但由于我不确定而将它们添加到此处。当从代码中取出时,它会在同一位置给出错误。
好吧,问题似乎是双重的,一个是 Criteria 需要是一个字符串并且它得到一个整数,其次 Operator:=xlFilterValues 被放置在只有一个变量的指令之后被认为很糟糕excel 众神。
至少就我而言,这不再是问题。希望这些信息对以后的其他人有所帮助!
修改后的代码:
Option Explicit
Private Sub CommandButton49_Click()
'
Dim Wsdnd As Worksheet
Set Wsdnd = Sheets("DO NOT DELETE")
Dim A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, ... (more variables) As Range
A4 = Wsdnd.Range("BD4").Value
A5 = Wsdnd.Range("BD5").Value
A6 = Wsdnd.Range("BD6").Value
A7 = Wsdnd.Range("BD7").Value
A8 = Wsdnd.Range("BD8").Value
... (More variables)
'
Wsdnd.Range("BC3:BE50").Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value" Lists on DO NOT DELETE sheet
Application.Calculation = xlManual 'Restarts manual calculations only for workbook speed
'
'Filter #4
If Not IsEmpty(A6) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE6"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A6
Else
End If
'
'Filter #5
If Not IsEmpty(A7) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE7"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A7
Else
End If
'
'Filter #6
If Not IsEmpty(A8) Then
Sheets("Database").Range("B:BL499").AutoFilter Field:=WorksheetFunction.Match(Sheets("DO NOT DELETE").Range("BE8"), _
Worksheets("Database").Range("B23:BL23"), 0), Criteria1:=A8
Else
End If
过滤工作表
Option Explicit
Private Sub CommandButton49_Click()
FilterWorksheet
End Sub
Sub FilterWorksheet()
Const ProcName As String = "FilterWorksheet"
On Error GoTo ClearError
Const sName As String = "DO NOT DELETE"
Const srgAddress As String = "BC3:BE50" ' ?
Const srrgAddress As String = "BD4:BD10" ' resize
Const scOffset As Long = 1 ' ('BE4:BE10')
Const dName As String = "Database"
Const drgAddress As String = "B23:BL71499"
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
Dim srg As Range: Set srg = sws.Range(srgAddress)
Dim srrg As Range: Set srrg = sws.Range(srrgAddress)
Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
If dws.AutoFilterMode Then dws.AutoFilterMode = False
Dim drg As Range: Set drg = dws.Range(drgAddress)
Dim dhrg As Range: Set dhrg = drg.Rows(1)
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
srg.Calculate 'Refreshing "Category", "Apply All Data", "Match Lookup Value"
Dim sCell As Range
Dim sValue As Variant
Dim dField As Variant
For Each sCell In srrg.Cells
sValue = sCell.Value
If Not IsError(sValue) Then
If Len(CStr(sValue)) > 0 Then
dField = Application _
.Match(sCell.Offset(, scOffset).Value, dhrg, 0)
If IsNumeric(dField) Then
drg.AutoFilter Field:=dField, Criteria1:=sValue
End If
End If
End If
Next sCell
SafeExit:
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
Exit Sub
ClearError:
Debug.Print "'" & ProcName & "' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume SafeExit
End Sub