+ Reply to Thread
Results 1 to 9 of 9

Runtime error 91 on range variable

  1. #1
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Runtime error 91 on range variable

    I have the following code within a fairly lengthy (I think) module.

    As opposed to typing out 'IF mmmYY.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).count' for each IF statement, I have created a range variable called rC but I am getting a Runtime error 91 and I'm not sure why?

    I'm guessing it may have something to do with the fact the mmmYY worksheet variable hasn't been set before rC has been set however the code doesn't run unless I set the mmmYY variable within the loop and it also doesn't run if I set the rC within the loop either.

    Please Login or Register  to view this content.
    Thanks in advance.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Runtime error 91 on range variable

    From the looks of it, you haven't set mmmYY to valid worksheet object.

    First set mmmYY to the worksheet you should reference and it should work.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime error 91 on range variable

    Thanks for the reply CK76. I've set mmmYY within the loop. If I set it outside the loop before rC is set, I get the same error message.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Runtime error 91 on range variable

    Try something like below then. I'm not 100% on AutoFilter.Range method.

    Please Login or Register  to view this content.
    You may need to adjust .Cells(1) to where your data range starts.

  5. #5
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime error 91 on range variable

    No joy with that either. That's not pasting in anything in to the closedTools tab. I changed to .cells(3) as row 3 is the header row?

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Runtime error 91 on range variable

    Looking at your code more closely. You are changing visible range within loop (by applying Autofilter condition). That defeats the purpose of setting "rC" outside of the loop.

    I'd suggest you upload small sample workbook with enough data to demonstrate your need, with manually created output as well.

    I think you are better off rethinking code logic.

  7. #7
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime error 91 on range variable

    I spotted another error in the code which I have now corrected. The full code is shown below and is running fine.

    I'm just trying to understand the correct way to declare a variable to prevent me from having to show the full lines of code in bold.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: Runtime error 91 on range variable

    I don't think you can avoid it, with current code structure/logic. But you really should use With... End With to avoid repeat typing of "mmmYY". Actually, you can do away with "mmmYY" variable, all together.

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    04-11-2017
    Location
    Edinburgh, Scotland
    MS-Off Ver
    2010
    Posts
    110

    Re: Runtime error 91 on range variable

    Yeah, I was thinking that. I had starting adding With statements but got sidetracked.

    Appreciate your help/efforts on this. Thanks a lot.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2016, 05:12 AM
  2. Runtime Error 91 - object variable or with block variable not set
    By 0celj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2013, 09:40 PM
  3. [SOLVED] Runtime Error '91': Object variable... When Running in Range Selection
    By Stonesteel15 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-01-2012, 09:50 AM
  4. runtime error 91 object variable or With block variable not set
    By tullemann in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-14-2011, 01:27 PM
  5. Runtime error 91 Objecr Variable of With block variable not set
    By s_ali_hassan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-04-2010, 05:28 AM
  6. Replies: 8
    Last Post: 02-17-2009, 02:24 PM
  7. Runtime error 91 - Object variable or with block variable not set
    By Christian411 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-04-2008, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1