+ Reply to Thread
Results 1 to 4 of 4

Run Time Error 91, Object Variable or With Block Variable not set

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-28-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    105

    Run Time Error 91, Object Variable or With Block Variable not set

    I have this macros below that keeps giving me a Run Time Error 91, Object Variable or With Block Variable not set prompt at the line that begins "Loop While". I was wondering if this can be fixed. This was a problem that arose from an existing thread, as its a new issue I am reposting. Thank You.


    Dim c As Range, firstaddress As String, a As Long
    Dim pSheet As Excel.Worksheet
    
    For Each pSheet In ActiveWorkbook.Sheets
    pSheet.Activate
    
    With Columns(1)
      Set c = .Find("Investor", LookIn:=xlValues, LookAt:=xlWhole)
      If Not c Is Nothing Then
        firstaddress = c.Address
        Do
          a = a + 1
          If a > 1 Then c = "Holder"
          c.Offset(, 3) = "% of CSO"
          Range("C" & c.Row & ":C" & c.Row + 20).Value = Range("D" & c.Row & ":D" & c.Row + 20).Value
          Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstaddress
       Range(firstaddress) = "Holder"
      Else
       
      End If
    
    End With
    Next pSheet

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Question regarding Run Time Error 91, Object Variable or With Block Variable not

    Try changing that line to
    Loop While c.Address <> firstaddress

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Question regarding Run Time Error 91, Object Variable or With Block Variable not

    This line, which comes directly from Help, should NEVER be used:
    Loop While Not c Is Nothing And c.Address <> firstaddress
    If c is Nothing, then c.Address will error; and if c has an Address, it is not Nothing. (VBA always evaluates all arguments to a logical expression; if it paid attention to the And operator and quit after evaluating the first, it wouldn't be a problem)

    If the search ultimately ends by finding the first entry again, then you should use
    Loop While c.Address <> firstaddress
    If it ends because you are destroying the found value, then use
    Loop While Not c Is Nothing
    If either could happen then
        If c is Nothing then Exit Do
    Loop While c.Address <> firstaddress
    Last edited by shg; 01-12-2011 at 02:11 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    01-28-2010
    Location
    NY
    MS-Off Ver
    Excel 2003
    Posts
    105

    Re: Question regarding Run Time Error 91, Object Variable or With Block Variable not

    Thank you it worked great, I used the following code:

     If c Is Nothing Then Exit Do
    Loop While c.Address <> firstaddress
       Range(firstaddress) = "Holder"
      Else
    Last edited by undergraduate; 01-12-2011 at 02:11 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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