+ Reply to Thread
Results 1 to 4 of 4

'Object variable or With block variable not set'

  1. #1
    Registered User
    Join Date
    04-26-2016
    Location
    Minneapolis
    MS-Off Ver
    2013
    Posts
    21

    'Object variable or With block variable not set'

    okay so I am still not really picking up on the syntax of VBA coming from having heavy SQL experience.

    I am pretty sure I am having an issue with the declarations but can't figure it out.

    - I totally stole this code from another post and was adjusting it based on my parameters.

    I am attempting to have it check on one sheet (Sheet1) if A1, A2.... is occupied, if it is, then I want to generate random numbers in Range A3 up to L65000 on the my current sheet (RAND_Input) depending on how many rows are occupied on Sheet1

    The error I am getting is 'Object variable or With block variable not set'


    Here is my code
    Sub RAND_Generator()
    Dim r As Range
    Dim c As Range
    Dim a As Range
    Dim b As Range
    Set b = Worksheets("Calc-Census - Current State").Range("A3:A65000")
    Application.ScreenUpdating = False
    For Each c In Worksheets("RAND_Input").Range("A3:L65000").Cells
    If b.Text <> "" Then
    If r Is Nothing Then
    Set r = c
    Else
    Set r = Union(r, c)
    End If
    End If

    Next c
    For Each a In r.Areas
    With a
    .Formula = "=rand()"
    .Value = .Value
    End With
    Next a
    End Sub



    ORIGINAL CODE

    Its base on if the cell color is red, which I did try using with conditional formatting but it didn't work for me.

    Sub randthing()
    Dim r As Range, c As Range, a As Range
    Application.ScreenUpdating = False
    For Each c In ActiveSheet.UsedRange
    If c.Interior.ColorIndex = 3 Then
    If r Is Nothing Then
    Set r = c
    Else
    Set r = Union(r, c)
    End If
    End If
    Next c
    For Each a In r.Areas
    With a
    .Formula = "=rand()"
    .Value = .Value
    End With
    Next a
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: 'Object variable or With block variable not set'

    Where do you get the error?

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: 'Object variable or With block variable not set'

    In your first For replace
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    04-26-2016
    Location
    Minneapolis
    MS-Off Ver
    2013
    Posts
    21

    Re: 'Object variable or With block variable not set'

    Thank you for the replies,

    kasan, I did change and I am still unfortunately getting that error.


    Norie,

    I am not sure I completely follow after reading your link about code tags, and I have never been asked this. If this is just a feature/setting I turn on I would gladly do it.

+ 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: 1
    Last Post: 07-24-2013, 02:45 PM
  4. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  5. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  6. Replies: 1
    Last Post: 09-25-2012, 08:03 PM
  7. [SOLVED] Intermittent Run-time Error 91: Object Variable or With Block variable not set
    By fraanchtoast in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-29-2012, 10:11 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