+ Reply to Thread
Results 1 to 8 of 8

Object variable or With block variable not set

  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2016
    Posts
    4

    Object variable or With block variable not set

    Hello!

    I'm getting this error, "Object variable or With block variable not set" using this macro:


    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    For x = 1 To 2000
    BeginRow = 1
    EndRow = 2000
    ChkCol = 2

    Cells.Find(What:="TOTAL").Select
    StartRange = ActiveCell.Address
    Selection.Offset(3, 0).Select
    EndRange = ActiveCell.Address
    ActiveSheet.Range(StartRange & ":" & EndRange).Select
    Selection.EntireRow.Delete
    Next x



    How do I set the variable or otherwise overcome this? The macro is working, it's just not seeming to come to an error-free end. I want the macro to find each "total" cell and then hide a few rows. The sheet size will vary, so I set an arbitrary number of times to repeat, knowing it won't be more than 2000. But it seems like it's not finding an ending and I don't know how to fix that.

    Thank you!
    Last edited by constructionzone; 08-15-2016 at 03:43 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,298

    Re: Object variable or With block variable not set

    Please use Code Tags (see below).

    What are you trying to do? If you're trying to delete an entire row, or rows, there's not much point in determining a start and end range just so you can delete three rows.

    How many rows have the word TOTAL in them? If it's less than 2000, that's why you get the error. What is the purpose of BeginRow, EndRow and ChkCol? You don't use them.

    Please Login or Register  to view this content.
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Object variable or With block variable not set

    It fails when the find doesn't return a range as an object. You need to handle that.
    Please Login or Register  to view this content.
    Last edited by Neil_; 08-15-2016 at 01:27 PM.
    Frob first, tweak later

  4. #4
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Object variable or With block variable not set

    It is not clear for what cycle and other variables

    Replace part of code
    Please Login or Register  to view this content.
    with this
    Please Login or Register  to view this content.
    To do it for me and help me it is 2 different things!
    Sorry for my english, blame Google translator

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,298

    Re: Object variable or With block variable not set

    Note: the two other solutions offered will both work. However, you will go through the loop 2000 times regardless of how many rows you find to delete. So, potentially, up to 1999 more loops than you need.

  6. #6
    Valued Forum Contributor Neil_'s Avatar
    Join Date
    04-19-2013
    Location
    Yorkshire
    MS-Off Ver
    Office 365 Enterprise E3 2013 / 2016
    Posts
    479

    Re: Object variable or With block variable not set

    combining the two.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-08-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2016
    Posts
    4

    Re: Object variable or With block variable not set

    Thanks everyone, that works great. Looks like I was making things WAY too complicated.

    Appreciate the help, thanks!

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,298

    Re: Object variable or With block variable not set

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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. [SOLVED] Run-time error '91': Object variable or With block variable not set when closing userform
    By bishoposiris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2016, 08:59 AM
  2. Advanced Excel - Object variable or With block variable not set (Error 91)??
    By larryg003 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2014, 01:37 PM
  3. Replies: 2
    Last Post: 08-22-2014, 03:55 AM
  4. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  5. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  6. Object variable or With block variable not set Error during sorting program
    By BlahBlahBlahBlahBlah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-17-2012, 07:57 AM
  7. Error 91 object variable block variable not set when importing a CVS file
    By ReportBuilder2011 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-13-2011, 11:48 AM

Tags for this Thread

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