+ Reply to Thread
Results 1 to 11 of 11

ERROR "91" Object Variable or with block variable not set

  1. #1
    Registered User
    Join Date
    03-23-2015
    Location
    Raleigh, NC
    MS-Off Ver
    Office 2010
    Posts
    5

    ERROR "91" Object Variable or with block variable not set

    The little block of code below is giving me fits. It generates an error 91, and I have tried a large number of solutions but I am stumped. I feel pretty sure that I am not handling object variable assignments properly but don't know how to fix it. Any help would be appreciated. The degger indicates that the error occurs on the "with .controls ("textbox1")" line but I don't see VBD ever being set to a value so I think that is where the problem starts.

    Sub Change_Userform()

    Dim VBP As VBIDE.VBProject
    Dim VBC As VBIDE.VBComponent
    Dim VBD As UserForm


    Set VBP = ThisWorkbook.VBProject
    Set VBC = VBP.VBComponents("Calc") 'this is my userform name
    Set VBD = VBC.Designer

    with VBD
    with .controls("textBox1")
    .value = "12345"
    end with
    end with


    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,644

    Re: ERROR "91" Object Variable or with block variable not set

    What are you trying to do with this code and how/when are you running it?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    03-23-2015
    Location
    Raleigh, NC
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: ERROR "91" Object Variable or with block variable not set

    A userform is displayed when the routine first runs that displays about 30 parameters in text boxes that the user can edit/tweak/tune. When they are satisfied with the values (they don't actually have to change them they can use the defaults) they run the routine which predicts bandwidth use for different types of facilities. The parameters are the result of study by various groups and are changed rarely and then reused by a larger group who run the code for project planning. On the form there is a "save" button that they can use if they change a parameter as the new default. The sub routine is in the main code module and is called when the "save" button is pushed (a toggle button).

  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 "91" Object Variable or with block variable not set

    So, what is this code meant to do?

    Can't you just display the userform, populate it's textboxes with the default/existing custom values, allow the user to alter them and then save them?

    All without programming directly to the VBE?

  5. #5
    Registered User
    Join Date
    03-23-2015
    Location
    Raleigh, NC
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: ERROR "91" Object Variable or with block variable not set

    No you can't save them. It appears that the intent was to allow user to input data into a userform so it could be saved in a spreadsheet, more like a data entry GUI than anything else. The values for the controls are set at "design time", and facilities are provided to add forms and controls during "run time". But once the control has a value it can't be changed directly. Here is a link that demostrrates the code: http://www.xtremevbtalk.com/archive/.../t-317970.html

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

    Re: ERROR "91" Object Variable or with block variable not set

    Did you read the last comment in the thread you posted a link for?

    You can't run this code from the form you want to alter.

  7. #7
    Registered User
    Join Date
    03-23-2015
    Location
    Raleigh, NC
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: ERROR "91" Object Variable or with block variable not set

    The comment I think you are referring to is: " I had the subs in userform itself (and assigned them to actions such as Userform_Click) NOT in a separate module. They're working beautifully now.". It doesn't say you can't run the code "from" the form, it says you can't have the code "in" the form. I have it in an external module that is called by the form when the "save" button is pushed.

  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 "91" Object Variable or with block variable not set

    Can you upload an example workbook?

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

  9. #9
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: ERROR "91" Object Variable or with block variable not set

    you cannot access the designer of a loaded userform. it doesn't matter where the code is-if you call it from the form it won't work
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  10. #10
    Registered User
    Join Date
    03-23-2015
    Location
    Raleigh, NC
    MS-Off Ver
    Office 2010
    Posts
    5

    Re: ERROR "91" Object Variable or with block variable not set

    Josie - Thank you, thank you, thank you. That is the answer. External to the form the code works perfectly. This makes things just a bit more complicated (i need to create a flag that the "save" button was pushed, save the data in the form, query the flag and write to the values after the form exits if the user wants the new parameters saved). But my original goal is now achievable. I will have to admit that I am a bit puzzled by the designers intent in coding the language this way. But it won't be the last time I have that question!

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: ERROR "91" Object Variable or with block variable not set

    I've never seen any need to do what you are doing the way you are doing it so I'm kinda with the designers on that :-)

+ 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] Object variable or with block variable not set "Run time error 91"
    By imzhakmaya in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2014, 06:54 AM
  2. Getting "Object variable or With block variable not set" error with Find function
    By enphynity in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-19-2013, 05:47 PM
  3. [SOLVED] Runtime Error "91" - Object Variable or With Block Variable not set
    By damidre in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-05-2013, 04:45 AM
  4. Replies: 1
    Last Post: 09-07-2012, 12:08 AM
  5. Error msgs: "Object varible or with block variable not set"; "subscript out of range"
    By menyanthe in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-26-2009, 04:58 PM

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