Object variable or With Block variable not set

sparky2205

Well-known Member
Joined
Feb 6, 2013
Messages
507
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi folks,
in the following code I'm getting the above error.
VBA Code:
Sub Filter_By_Category()

Dim lastrow As Long
Dim count As Long
Dim count1 As Long
Dim lvalue As Long
Dim category As String
Dim srange As Long

    srange = 9
    lastrow = Cells(Rows.count, 3).End(xlUp).Row
    ' Hide all vendors
    Rows("9:" & lastrow).EntireRow.Hidden = True
    category = Application.InputBox("By which Category do you wish to filter?", Type:=2)
    count = WorksheetFunction.CountIf(Range("I9:I" & lastrow), category)

    For i = 1 To count
        If i = 1 Then
            lvalue = Range("I" & srange & ":I" & lastrow).Find(category).Row + 8
        Else
            lvalue = Range("I" & srange & ":I" & lastrow).Find(category).Row
        End If
'        lvalue = Application.Match(category, Sheets("Revised").Range("I"& srange & ":I" & lastrow), 0) + 8
        Rows(lvalue & ":" & (lvalue + 3)).Hidden = False
        srange = lvalue + 3
    Next i

    'lvalue = WorksheetFunction.XLookup(category, Range("I9:I" & lastrow), Range("I9:I" & lastrow), "Selected category not found")

End Sub

It errors at:
VBA Code:
lvalue = Range("I" & srange & ":I" & lastrow).Find(category).Row + 8
i.e. the first IF statement.
"category" is populated with a value that exists in column I

It worked fine yesterday, but has decided not to work today.
Any thoughts?
 
Yeah, I understand it's difficult when you don't know the exact scenario you are dealing with. Thanks for persevering.
The new code is returning the first entry but then it returns part of another entry and then nothing else.
This is complicated by the fact that I didn't have that part of the code working properly in the first place. I was working on that when I suddenly encountered the error.
I'm attaching a screenshot of what the file looks like, that might help.
I could also create a dumbed down version of the file and upload a link if that would help?
1718880573024.png

There are more columns in the file but this should give you a good idea of what I'm trying to achieve.
When the user clicks a button they are prompted for the category.
The result is e.g. selecting category C will display rows 21-24, 25-28, 33-36, 41-44 etc. like this:
1718880993824.png


Your latest code resulted in:
1718880879523.png
 

Attachments

  • 1718880866849.png
    1718880866849.png
    50.3 KB · Views: 5
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
This is a link to a dumbed down version of the file:
 
Upvote 0
I am not sure it wouldn't be easier to just do a normal loop with a step 4 but try this using the current methodology

VBA Code:
    Dim FoundRow As Long
    For i = 1 To count
        With Application
            FoundRow = .IfError(.Match(category, Range("I" & srange & ":I" & lastrow), 0), 0)
        End With
        If FoundRow = 0 Then Exit For
        lvalue = FoundRow + srange - 1
        Rows(lvalue & ":" & (lvalue + 3)).Hidden = False
        srange = lvalue + 4
    Next i
 
Upvote 0
I just tried it on your file and its too slow for my liking. You definitely need to add Application.ScreenUpdating = False at the beginning and True at the end.

Have you considered using helper columns in say AY so that you can populate every line (fill down) and then use that for the autofilter ?
 
Upvote 0
I have a little bit more testing to do on this and I'm tied up for the next couple of hours but, first impressions are that this is working.
I will get back to you in a few hours when I've had the time to fully test it but I wanted to let you know it looks good.
Thanks a million Alex.
 
Upvote 0
Turning off calculations made a big difference and unhiding in blocks (of range areas) helped a little too.

VBA Code:
Sub Filter_By_Category_Match_Areas()

Dim lastrow As Long
Dim count As Long
Dim count1 As Long
Dim lvalue As Long
Dim category As String
Dim srange As Long
Dim i As Long

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
    srange = 9
    lastrow = Cells(Rows.count, 3).End(xlUp).Row
    ' Hide all vendors
    Rows("9:" & lastrow).EntireRow.Hidden = True
    category = Application.InputBox("By which Category do you wish to filter?", Type:=2)
    count = WorksheetFunction.CountIf(Range("I9:I" & lastrow), category)
                          
    Dim FoundRow As Long
    Dim strRowsToHide As String
    Dim rngVisible As Range
    For i = 1 To count
        With Application
            FoundRow = .IfError(.Match(category, Range("I" & srange & ":I" & lastrow), 0), 0)
        End With
        If FoundRow = 0 Then Exit For
        lvalue = FoundRow + srange - 1

        If rngVisible Is Nothing Then
            Set rngVisible = Rows(lvalue & ":" & (lvalue + 3))
        Else
            Set rngVisible = Union(rngVisible, Rows(lvalue & ":" & (lvalue + 3)))
        End If
        srange = lvalue + 4
    Next i
  
    Dim areaVisible As Range
    For Each areaVisible In rngVisible.Areas
        areaVisible.EntireRow.Hidden = False
    Next areaVisible
  
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
This works well, except where the user enters a nonexistent category.
Then areaVisible Is Nothing and we're back to our old friend, the withblock variable not set error.
Where is areaVisible set equal to something?

I've tried a bit of error proofing but it got stuck in a loop.
What I wanted was, if a user enters an incorrect category, they are presented with a msgbox telling them that and then presented with the input box again.
You'll see my name against my attempt below.
VBA Code:
Sub Filter_By_Category1()

Dim lastrow As Long
Dim count As Long
Dim count1 As Long
Dim lvalue As Long
Dim category As String
Dim srange As Long
Dim i As Long

'On Error GoTo errhandler

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
    srange = 9
    lastrow = Cells(Rows.count, 3).End(xlUp).Row
    ' Hide all vendors
    Rows("9:" & lastrow).EntireRow.Hidden = True
cat:    category = Application.InputBox("By which Category do you wish to filter?", Type:=2)
    count = WorksheetFunction.CountIf(Range("I9:I" & lastrow), category)
                          
    Dim FoundRow As Long
    Dim strRowsToHide As String
    Dim rngVisible As Range
    For i = 1 To count
        With Application
            FoundRow = .IfError(.Match(category, Range("I" & srange & ":I" & lastrow), 0), 0)
        End With
        If FoundRow = 0 Then Exit For
        lvalue = FoundRow + srange - 1

        If rngVisible Is Nothing Then
            Set rngVisible = Rows(lvalue & ":" & (lvalue + 3))
        Else
            Set rngVisible = Union(rngVisible, Rows(lvalue & ":" & (lvalue + 3)))
        End If
        srange = lvalue + 4
    Next i
  
    Dim areaVisible As Range
    If areaVisible Is Nothing Then                  '******Joe******
        MsgBox "Category not found"                 '******Joe******
        GoTo cat                                    '******Joe******
    Else                                            '******Joe******
    For Each areaVisible In rngVisible.Areas
        areaVisible.EntireRow.Hidden = False
    Next areaVisible
    End If                                          '******Joe******
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

errhandler:
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This line should return count = 0 if they pick an non-existent category:
VBA Code:
    count = WorksheetFunction.CountIf(Range("I9:I" & lastrow), category)
You can trap that and give a message box and maybe exit the sub.

If you want to do a loop then typically you would just allow 3 tries and that would need some extra code.

I am login off for the night though.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top