+ Reply to Thread
Results 1 to 7 of 7

Object or With Block Error

Hybrid View

  1. #1
    Registered User
    Join Date
    11-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    42

    Object or With Block Error

    Guys,

    I am getting an error Run-time error 91: Object variable or With Block variable not set. Can you guys help me fix the error to make this code work. The code just needs to Delete the visible rows and then clear the existing filter to display the results after the deletion.

    I appreciate your help!


    Sub DeleteRows()
    '
    
    
    Dim lastrow As Range
    
    
    Sheets("TEST").Select
    
    
    lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    
    
    'Delete Visible Rows
    
    Range("A5:O" & lastrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    
    
    'Clear Filter
    
    
    With Sheets("TEST")
    
      If .FilterMode Then .ShowAllData
    
    End With
    
    
    End Sub

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Object or With Block Error

    try this

    change the DIM statement for lastrow to an integer and try this for the lastrow

    lastrow = Cells(999999, 1).End(xlUp).Row
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Object or With Block Error

    Just change Dim lastrow As Long

    @judgeh59, even if it's save to assume that lastrow will be less than the max allowed integer number it's bad practice to declare variable that will hold row number as integer and it's a call for future problems - error#6: OverFlow.
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Object or With Block Error

    @buran - yup - I usually set a global variable as long and then use that....because of course 999999 wouldn't work for integers....thanks

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: Object or With Block Error

    :-)
    @judgeh59: in this case just hope that user will not save the file as xls and face the fact that there are just around 65K rows

  6. #6
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Object or With Block Error

    correct -

  7. #7
    Registered User
    Join Date
    11-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: Object or With Block Error

    Works perfect! While playing with it, I noticed that it also worked with Dim lastrow as Variant.

    I appreciate your support.

    Thanks guys!

+ 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] New Error/Old Code - Run Time Error 91; Object variable or With block variable not set
    By humboldtguy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-15-2014, 12:39 AM
  2. [SOLVED] Run-time error '91' - Object variable or With block not set // getting HTML elements of we
    By Bootwalker in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-02-2014, 11:33 AM
  3. [SOLVED] Run-time error 91: Object viable or with block varible not set
    By bensox in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-29-2012, 03:03 PM
  4. Error #91 Object Variable or With Block Not Set
    By t0m46 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-01-2010, 12:07 PM
  5. Error Message: Object variable or with block not set?
    By esawah in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2010, 03: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