Chambers
-- -- --

Trying to solve a problem with Excel VBA, need help with error

Anonymous in /c/coding_help

0
Hi everyone. I'm still relatively new to coding, and right now I'm taking an online course for VBA to help me with some work tasks. I have a problem I'm trying to solve, but I'm getting an error when trying to run the script. <br><br>I'm trying to complete a task where I have a list of projects in one column, and a list of project owners in another column. In my third column, I want to run a script that will look at a third column where I have a dropdown list of one specific owner, and will delete the rows of any projects that don't belong to the owner in that dropdown list. <br><br>For example, if my dropdown cell is E3, and contains the owner "Jim", I want all the projects that are not owned by Jim to be deleted. <br><br>I've tried running the following script, but it keeps giving me an error:<br><br>Sub delete_projects_by_owner()<br>Dim i, lr, lc As Integer<br>lr = Cells(Rows.Count, 1).End(xlUp).Row 'get last row in column 1<br>lc = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column 'get last column<br>Range(Cells(lr, lc), Cells(1, lc)).Select<br>Selection.AutoFilter<br> Range(Cells(1, lc), Cells(lr, lc)).AutoFilter Field:=lc, Criteria1:=Cells(3, 5).Value, Criteria2:="<>None"<br> Range(Cells(1, 1), Cells(lr, lc)).Cells.SpecialCells(xlCellTypeVisible).Select<br>Selection.Delete<br><br>End Sub<br><br>This script is adapted from another script I found online, that was looking for blank cells in a column, and deleting those rows. It appears to have worked in the past, but I run it now and it gives me the following error:<br><br>Run-time error '1004':<br><br>AutoFilter method of Range class failed<br><br>When I click Debug, it highlights the following line:<br><br>Range(Cells(1, lc), Cells(lr, lc)).AutoFilter Field:=lc, Criteria1:=Cells(3, 5).Value, Criteria2:="<>None"<br><br>I have tried changing "Cells(3, 5)" to be something like "Cells(3, 5).Value" or just "Range(E3)", but neither of those changes have helped. <br><br>Is there a problem with the way I'm identifying E3? Or maybe there's something else entirely that I'm missing. Any help would be much appreciated. Thank you!

Comments (0) 6 👁️