+ Reply to Thread
Results 1 to 13 of 13

Object variable or With block variable not set error

Hybrid View

maraieth Object variable or With block... 11-07-2016, 08:27 AM
AB33 Re: Object variable or With... 11-07-2016, 08:37 AM
maraieth Re: Object variable or With... 11-07-2016, 08:42 AM
AB33 Re: Object variable or With... 11-07-2016, 08:50 AM
maraieth Re: Object variable or With... 11-07-2016, 09:00 AM
AB33 Re: Object variable or With... 11-07-2016, 09:05 AM
maraieth Re: Object variable or With... 11-07-2016, 09:10 AM
AB33 Re: Object variable or With... 11-07-2016, 09:17 AM
maraieth Re: Object variable or With... 11-07-2016, 10:22 AM
AB33 Re: Object variable or With... 11-07-2016, 10:28 AM
maraieth Re: Object variable or With... 11-07-2016, 10:34 AM
AB33 Re: Object variable or With... 11-07-2016, 10:46 AM
maraieth Re: Object variable or With... 11-07-2016, 10:53 AM
  1. #1
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Object variable or With block variable not set error

    I am relatively new to VBA and came across this error for a segment of code I have been working on - it worked when I recorded the macro but not when I run it, and I know that there are definitely cells available of the format that I am looking for, but I just wondered if anyone could point me in the right direction?

    I am trying to find the first occurrence of a cell with a particular colour background, regardless of the text within the cell, and what I have so far (from recording a macro, then updating it slightly) is the following:

    Sheets("KPI (2)").Visible = True
    Sheets("KPI (2)").Select
    Range("A1").Select

    With Application.FindFormat.Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent2
    .TintAndShade = 0.599963377788629
    .PatternTintAndShade = 0
    End With

    Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=True).Activate
    Thanks in advance!

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    Dim rng As Range
    
    set rng=Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=True).Activate 
    
    If Not rng Is Nothing Then
    
    ' the rest of the code
    
    end if

  3. #3
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    I have just tried this, but I still get the same error message.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    Which line of the code errors (Highlighted) ?
    Probably this line

    With Application.FindFormat.Interior

  5. #5
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    It's the Cells.Find part of it that errors

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    I do not know why the code errors on the line.
    What is "What:="" ?

    Why are you to find a blank cell any way?
    If this does not work, please attach a sample with the code.


    Set rng = cells.Find(" ", LookIn:=xlValues, lookat:=xlWhole)

  7. #7
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    The "What:="" part is what the macro recording gave me when I searched based on no text criteria

    I think my problem may be that I don't want to find a blank cell, I want to find the cell based on the background colour, regardless of what text is in the cell, but I assumed that as that was how the find worked when recording the macro, it would do the same thing when I played it back.
    In which case, do you know of a way of doing a search by format of cell, regardless of the text within the cell?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    In that case, should be

    Set rng = cells.Find("*", searchformat:=True)

  9. #9
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    I'm afraid I'm still getting errors on that line!
    The following it a screenshot of a section of the sheet I'm working on, in case it helps identify where I am going wrong! (edited to remove anything confidential)
    Excel sheet.jpg
    I am basically trying to navigate to the start of the red cells to be able to update them (as they are missing from the above data in blue, which comes from PowerPivot) where the length of the blue and red sections are variable.

    Code now reads (up to point of error) -

    Sheets("KPI (2)").Visible = True
    Sheets("KPI (2)").Select
    Range("A1").Select

    With Application.FindFormat.Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent2
    .TintAndShade = 0.599963377788629
    .PatternTintAndShade = 0
    End With

    Dim rng As Range

    Set rng = Cells.Find("*", SearchFormat:=True).Activate

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    Please attach an excel sample book, not a picture. I can not test your code on a pic.

  11. #11
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    Sorry about that, apparently not all with it today - see attached!
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Object variable or With block variable not set error

    The code does not error. Yes, it is not returning any values as the interior color is wrong.

    Sub test1()
    Sheets("KPI (2)").Visible = True
    Sheets("KPI (2)").Select
    
      With Application.FindFormat
            .Clear
            .Interior.Color = 12040422
        End With
    
    Dim rng As Range
    
    Set rng = Cells.Find("*", SearchFormat:=True)
    
     If Not rng Is Nothing Then
       MsgBox rng.Address
     
     End If
    End Sub

  13. #13
    Registered User
    Join Date
    11-07-2016
    Location
    Leicester, England
    MS-Off Ver
    Microsoft Office 365 ProPlus
    Posts
    9

    Re: Object variable or With block variable not set error

    Very strange, as I definitely recorded the macro using the colours in the sheet I sent!
    But regardless, thank you so much for your time in helping! It would have taken me considerably longer to spot that, so very much appreciated

+ 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. Replies: 2
    Last Post: 08-22-2014, 03:55 AM
  3. Replies: 0
    Last Post: 04-16-2013, 07:15 AM
  4. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  5. Replies: 1
    Last Post: 09-25-2012, 08:03 PM
  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. Object Variable or With Block Variable Not Set- Error on Another User Machine
    By dailem in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-01-2010, 09:28 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