私は次のサブを書きました:
Public Sub filterEmployeeSheets(Sheets As Excel.Worksheet, SearchRange As String, Indicator As String, FilterString As String)
'This Sub is used to filter sheets by deleting any rows
'that do not contain the value stated in variable filterString
'@Parameter Sheets to declare sheet(s) name
'@Parameter SearchRange to set the column to filter
'@Parameter Indicator determines the =, <> setting
'@Parameter FilterString to set the string to keep
Dim lngLr As Long
With Sheets
lngLr = .Cells.Find(What:="*", SearchDirection:=Excel.XlSearchDirection.xlPrevious, SearchOrder:=Excel.XlSearchOrder.xlByRows).Row
If lngLr > 1 Then
With .Range(SearchRange & lngLr)
**.AutoFilter(Field:=1, Criteria1:=Indicator & FilterString)** 'Error is here
.EntireRow.Delete()
End With
End If
End With
End Sub
Public Function ClientSheets(Index As Long) As Excel.Worksheet
'This function indexes all of the Employee sheets
'to use in various loops during he instal process
'@param EmployeeSheets, are the sheets to index
Select Case Index
Case 1 : Return xlWSAllEEAnnul
Case 2 : Return xlWSAllEEHourly
End Select
Throw New ArgumentOutOfRangeException("Index")
End Function
以下の手順で呼び出すと:
Dim xlRefSheets As Excel.Worksheet
For i As Long = 1 To 2 Step 1
Dim strOperatorSymbol As String = "<>"
xlRefSheets = ClientSheets(i)
filterEmployeeSheets(xlRefSheets, "K5:K", "<>", "Y")
Next
End Sub
次のエラーが表示されます: 指定された範囲を使用してコマンドを完了できませんでした。範囲内の単一のセルを選択して、コマンドを再試行してください。ただし、Public Sub を呼び出すのではなく、単一のシートで For ループを使用せずにプロシージャとして使用すると、問題なく動作します。