+ Reply to Thread
Results 1 to 4 of 4

Error handling not working in "vba object variable or with block variable not set" in Loop

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Error handling not working in "vba object variable or with block variable not set" in Loop

    Hi


    I have On Error "GoTo ErrorHandler" in my code

    It works fine in first loop and Goes to ErrorHandler on error but in second loop of 'heads' it gives error as follows.

    "vba object variable or with block variable not set"

    For I = 28 To WS_Count
        With Worksheets(I)
            Sheet_Name = .Name
            Name_Start = Left(Sheet_Name, 1)
            Entries = Application.WorksheetFunction.CountA(.Range("D:E"))
            Filled = Application.WorksheetFunction.CountA(.Range("F:F"))
            If IsNumeric(Name_Start) And Sheets(I).Visible = True And Entries - Filled = 1 Then
                .Activate
                On Error GoTo ErrorHandler
                For heads = 1 To 5
                    entry = Sheets("Analyzer").Range("C" & 30 + heads).Value
                    TargetSheet = Sheets("Analyzer").Range("B" & 30 + heads).Value
    Error Line>>>                EntryRwFirst = .Columns(6).Find(What:=entry, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False).Row
                    '(irrelevant code removed)
    ErrorHandler:
                Next heads
            End If
            On Error GoTo 0
        End With
    Next I

    Note: Error is happening because in both of 1st and 2nd loop the value is not found from the sheet. That is Ok but I want the code to go to ErrorHandler: when there is error in 2nd loop, instead of showing the error.

    Thanks
    Last edited by caabdul; 11-23-2019 at 06:40 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: Error handling not working in "vba object variable or with block variable not set" in

    Sub NotEnoughInfo()
        Dim EntryRwFirst As Variant 
    
        For I = 28 To WS_Count
            With Worksheets(I)
                Sheet_Name = .Name
                Name_Start = Left(Sheet_Name, 1)
                Entries = Application.WorksheetFunction.CountA(.Range("D:E"))
                Filled = Application.WorksheetFunction.CountA(.Range("F:F"))
                If IsNumeric(Name_Start) And Sheets(I).Visible = True And Entries - Filled = 1 Then
                    .Activate
                    For heads = 1 To 5
                        entry = Sheets("Analyzer").Range("C" & 30 + heads).Value
                        TargetSheet = Sheets("Analyzer").Range("B" & 30 + heads).Value
                        Set EntryRwFirst = .Columns(6).Find(What:=entry, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False).Row
                        If Not EntryRwFirst Is Nothing Then      'search successfull, so,,,
                            '(irrelevant code removed)
                        End If
                    Next heads
                End If
            End With
        Next I
    End Sub
    Ben Van Johnson

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,280

    Re: Error handling not working in "vba object variable or with block variable not set" in

    As to why it happens: http://excelmatters.com/2015/03/17/on-error-wtf/
    Remember what the dormouse said
    Feed your head

  4. #4
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: Error handling not working in "vba object variable or with block variable not set" in

    Thanks Rorya, it worked

+ 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] exit a form getting "Runtime Error 91: Object variable or with block variable not set"
    By Hammer_757 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2019, 08:49 AM
  2. [SOLVED] Object variable or with block variable not set "Run time error 91"
    By imzhakmaya in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2014, 06:54 AM
  3. Getting "Object variable or With block variable not set" error with Find function
    By enphynity in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-19-2013, 05:47 PM
  4. [SOLVED] Runtime Error "91" - Object Variable or With Block Variable not set
    By damidre in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-05-2013, 04:45 AM
  5. Replies: 1
    Last Post: 09-07-2012, 12:08 AM
  6. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM
  7. Keep getting "Object Variable or With block variable not set" error with macro
    By MSFSC in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-17-2009, 09:02 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