Excel VBA error 91: Object variable or With block variable not set

GeriJ

New Member
Joined
Jun 11, 2018
Messages
3
I can't find my error. I've tried Adding "Set" in front of "colcnt = rng.Columns.Count", where the error is highlighted. I tried several other changes, but no luck. I'm using Excel 2011 on a Mac.

Please help!
Code:
Sub FindSales()
    Dim rng As Range
    Dim colcnt As Long
    Dim sortv As String
    Dim x As Long
    
    Set rng = Range("VecC1")
    sortv = "Sales"
    colcnt = rng.Columns.Count
    
        For x = 1 To colcnt
            If rng.Cells(1, x).Value = sortv Then
                rng.Cells(1, x).EntireColumn.Hidden = False
            Else
                rng.Cells(1, x).EntireColumn.Hidden = True
            End If
        Next x
            Range("A1").Select


End Sub
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to the forum.

Your code looks fine to me as posted. I certainly can't see how you'd get an error 91 from it. You would get a different error if the range name were wrong.
 
Upvote 0
Nevertheless, this is what is happening. I have been trying different permutations all day. Still scratching my head.
Geri
 
Upvote 0
A silly suggestion:
copy the macro form your message and paste it into your project (replacing the current code), and test it

When in debug mode (after the error), press Cont-g to open the vba "Immediate window" and type
Code:
?rng.address
What does it respond?

Also, can you share a small test workbook?

Bye
 
Upvote 0
Thanks, Anthony,

I have no idea as to why, but here's what worked. In the original vba, I call the "FindSales()" macro in a variety of smaller macros, redefining sortv for each. When I copied and pasted the code, it worked the first time, but not the second!

Inspired to try odd things, I moved the "Dim sortv as String" from all the macros that called FindSales(), to the universal top. That worked!

The error message was not helpful and the highlighted code was a misdirect. I've got to study up of debugging!

Your suggestion, gave me a new way to approach the problem, so, thanks!

Geri

A silly suggestion:
copy the macro form your message and paste it into your project (replacing the current code), and test it

When in debug mode (after the error), press Cont-g to open the vba "Immediate window" and type
Code:
?rng.address
What does it respond?

Also, can you share a small test workbook?

Bye
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,693
Members
449,331
Latest member
smckenzie2016

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