+ Reply to Thread
Results 1 to 9 of 9

Error in a Code. Object variable or With block variable not set????

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Wisbech England
    MS-Off Ver
    Excel 2010
    Posts
    308

    Error in a Code. Object variable or With block variable not set????

    Hi All :-)

    I am getting an error in my code and have no clue what is wrong?? It was working before and now I am getting this :
    Run time error 91
    Object variable or With block variable not set



    This is the whole code:

    Private Sub CommandButton1_Click()
      If Me.ComboBox1.ListIndex < 0 Then Exit Sub
    
      With MyRange.Cells(1).Offset(ComboBox1.ListIndex)
    
        .Offset(, 0).Value = ComboBox1.Value
        .Offset(, 21).Value = TextBox1.Value
        .Offset(, 19).Value = TextBox2.Value
        .Offset(, 20).Value = TextBox3.Value
        .Offset(, 25).Value = TextBox4.Value
        .Offset(, 22).Value = ComboBox29.Value
        .Offset(, 23).Value = TextBox32.Value
    
      
         
      End With
    
      Unload Me
      MsgBox "Record Updated"
      UserForm1.Show
      
    End Sub
    Thanks for help
    Dan

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

    Re: Error in a Code. Object variable or With block variable not set????

    What is MyRange?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Wisbech England
    MS-Off Ver
    Excel 2010
    Posts
    308

    Re: Error in a Code. Object variable or With block variable not set????

    No Idea Mate I got this from my Buddy. so not really sure what this mean????

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

    Re: Error in a Code. Object variable or With block variable not set????

    Dan

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  5. #5
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Wisbech England
    MS-Off Ver
    Excel 2010
    Posts
    308

    Re: Error in a Code. Object variable or With block variable not set????

    Not too sure as my project is huge and contains sensitive data.
    Let me have a look...

  6. #6
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Wisbech England
    MS-Off Ver
    Excel 2010
    Posts
    308

    Re: Error in a Code. Object variable or With block variable not set????

    Well I can't do it as I would have to breakdown the whole file which will have an impact on the code as some codes referring to different userforms which I can't show.....

  7. #7
    Registered User
    Join Date
    01-03-2014
    Location
    Johannesburg South Africa
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Error in a Code. Object variable or With block variable not set????

    The MyRange could differ from workbook to Workbook

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

    Re: Error in a Code. Object variable or With block variable not set????

    Do you know anything about the code you posted and/or what MyRange is meant to be?

    Is there code elsewhere that defines it?

  9. #9
    Forum Contributor
    Join Date
    08-29-2011
    Location
    Wisbech England
    MS-Off Ver
    Excel 2010
    Posts
    308

    Re: Error in a Code. Object variable or With block variable not set????

    Hi All
    I have sorted it please see the below code, it had a range missing.....


    Private Sub CommandButton1_Click()
    
    Application.ScreenUpdating = 0
    Dim MyRange As Range
     Set MyRange = Sheets("Sheet1").Range("A3:A" & Range("A" & Rows.Count).End(xlUp).Row)
    
      If Me.ComboBox1.ListIndex < 0 Then Exit Sub
    
      With MyRange.Cells(1).Offset(ComboBox1.ListIndex)
        .Offset(, 0).Value = ComboBox1.Value
        .Offset(, 21).Value = TextBox1.Value
        .Offset(, 19).Value = TextBox2.Value
        .Offset(, 20).Value = TextBox3.Value
        .Offset(, 25).Value = TextBox4.Value
        .Offset(, 22).Value = ComboBox29.Value
        .Offset(, 23).Value = TextBox32.Value
         
      End With
    
      Unload Me
      MsgBox "Record Updated"
      UserForm1.Show
      
    End Sub
    Thanks

+ 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] Excel VBA error code 91: Object variable or with block variable not set
    By wilnexpc in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-24-2013, 11:46 AM
  2. Replies: 0
    Last Post: 04-16-2013, 07:15 AM
  3. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  4. 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
  5. Error Code 91 Object Variable or With Block variable Not Set
    By florencia in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-30-2008, 04:46 AM

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