+ Reply to Thread
Results 1 to 2 of 2

Error: "Object Variable or With Block variable not set"

Hybrid View

  1. #1
    Registered User
    Join Date
    06-03-2007
    Posts
    1

    Error: "Object Variable or With Block variable not set"

    Dear all, I am fairly new to get into VBA again after a couple of years abstinence. I have the following code that raises an error:

    Public Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim ole As OLEObject

    If Target.Column > 1 _
    And Target.Column < 9 _
    And Target.Row > 1 _
    And Range("A" & (Target.Row)).Value <> "" Then

    'Remove all OLEObjects from sheet
    For Each ole In Me.OLEObjects
    If Not (ActiveCell Is Nothing) Then
    MsgBox (ole.Object.Value)
    End If
    ole.Delete
    Next ole
    CreateListBox (Target.Cells(1))
    End If
    End Sub

    The Error is Object variable or With block variable not set. I am not sure why this is.

    Thanks,

    Hans

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Hans,

    OLEObjects have an Object property. Setting an object variable equal to this property allows you to acces the methods, events, and properties of the object it represents. Here are the corrections to your code.

    Public Sub Worksheet_SelectionChange(ByVal Target As Range)
    
      Dim ole As Object
      Dim obj As Object
    
        If Target.Column > 1 And Target.Column < 9 _
           And Target.Row > 1 And _ 
           Range("A" & (Target.Row)).Value <> "" 
        Then
    
     'Remove all OLEObjects from sheet
        For Each ole In ActiveSheet.OLEObjects
          Set obj = ole.Object
            If Not (ActiveCell Is Nothing) Then
               MsgBox (obj.Value)
            End If
          obj.Delete
        Next ole
       
         CreateListBox (Target.Cells(1))
    
    End Sub
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 06-03-2007 at 06:20 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