Good day,
I am having problems with Set Ranges and it has been quite frustrating when using set ranges from non-active sheets.
The problem is:
I have a sheet called "Dashboard". In this sheet i have a Listbox that when selected will filter values (based on listbox.column value) on a Table in another sheet called "Budget". However, i get Error 1004 (Autofilter method of Range class failed), after closing the error it filters the range. So it seems it works somehow, however it gives me error.
The code below is the one i'm using to filter the range. It is inserted in the "Dashboard" Sheet object.
Private Sub DashboardBudgetlst_Change()
Dim rng As Range
Dim i As Integer
i = Me.DashboardBudgetlst.ListIndex
If i >= 0 Then
If Me.DashboardBudgetlst.Selected(i) And Me.DashboardBudgetlst.Column(0, i) <> "" Then
Set rng = Budget.Range("B1:E" & lrow(Budget, "A"))
rng.AutoFilter 1, Me.DashboardBudgetlst.Column(1, i)
Set rng = Nothing
End If
End If
End Sub
The macro will filter a range that is used for a chart, therefore will filter the values of my chart. Also i don't want to use pivot tables as it is very slow.
Further exploring the question. How can i use Ranges from one Worksheet that are Set in another Worksheet without having to Activate the Sheet of that range? (most of the time i have to do Sheet.Activate before using the Set range for that sheet).
Would you guys know the workaround and why there is this problem with Set Ranges?
I know there are similar questions about ranges, but none with the same specifications.
Additional Information (Edit):
1- Error is on line:
rng.AutoFilter 1, Me.DashboardBudgetlst.Column(1, i)
2- Listbox index >= 0 to ensure that listbox is not empty and there's an item selected. When a listbox is empty the listindex = -1.
3- lrow(Budget, "A") calls the following function to get the last row in the specified sheet:
Function lrow(SH As Worksheet, col As String)
lrow = SH.Cells(Rows.Count, col).End(xlUp).Row
End Function
4- With msgbox rng.address just before the error line, i receive $B$1:$E$5 as the address.
5- I did a temporary workaround using
On Error Resume Next
6- Value for Me.DashboardBudgetlst.Column(1, i) is a keyword to be filtered and depends on the selection. The listbox is fed with the same range that i am filtering. So i am selecting the column "1" from the list which is under the header "Item". When i select something from the listbox i want it to filter by that Budget Item, sometimes can be "Accommodation" or anything else i have there.
7- Debug.Print on :
Debug.Print rng.AutoFilter; 1, Me.DashboardBudgetlst.Column(1, i)
Selected on Travel Expenses in listbox Returns on Immediate Window:
True 1 Travel Expenses
8- Some Screenshots:
Listbox in Dashboard Sheet (Excel View)
Range in Budget Sheet (Excel View)
It works as after i closed the error the filter would apply. However i would like to know if there's another workaround and i'm not sure about using "On Error Resume Next" (Is it bad for your code?)
llstIndex
be if not>=0
? Is there a-1
? – ashleedawglrow
? And what's the value ofrng.Address
just before the error? (Find out by adding a line right before the error:MsgBox rng.Address
) – ashleedawgMe.DashboardBudgetlst.Column(1, i)
? It looks like you are setting the proper ranges to the other sheet, if the sheet Code Name is indeed "Budget" – paul bicaBudget.Range("B1...
which is great! On Error Resume Next is a bad specially for beginners because you don't understand or know the errors you are hiding - so your motivation is great as well! – paul bica