+ Reply to Thread
Results 1 to 8 of 8

Setting up variables for the userForm on Workbook_Open() - ERROR 91

  1. #1
    Registered User
    Join Date
    06-13-2013
    Location
    Katowice
    MS-Off Ver
    Office 2007
    Posts
    19

    Setting up variables for the userForm on Workbook_Open() - ERROR 91

    Dear colleagues,
    Since my last EXCEL VBA application passed two years and now I started the whole new project. I did already a lot of works and now I wanted to set some variables to be set on excel opening. So this is what I did: the userform is called "CACalc" and at the beginning I'm declaring variables:

    Please Login or Register  to view this content.
    I would like to have those two variables already set when the excel file is opened. I created then this method in the "ThisWorkbook" object:

    Please Login or Register  to view this content.
    When I run the project I receive the "Error 91, Object Variable or with block not set " in the shown line of above code. This can be solved if I change the variable "wb" type to "String", but I'd like to have it as "Workbook". What I'm missing? The goal is to use those two variables in the userForm code to get the data from the excel sheet:

    Please Login or Register  to view this content.
    And by the way: I get the error 9 in above code although the variables were correct . When I enter the object "Workbook(wb)." after pressing CTRL + SPACE I get the list of possible methods and object to select. However whatever I select "Worksheets" or "Sheets" followed by the index (e.g. Sheets(2) ) after the next '.' I don't have a list with other methods and objects. I was expecting "Range" or "Cell" available but I get nothing. I will appreciate you help.

    Best regards,
    Michal

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

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    you must use set and pass an object

    Please Login or Register  to view this content.
    Josie

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

  3. #3
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    You need to set properties in the userform to do it that way, have you done that?

    Please Login or Register  to view this content.

    Sorry ignore me, i misread the variables bit. thought you had them in Workbook_OPen.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

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

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    no you don't-public variables are directly accessible as public members of the class. sure it's better code if you use properties but you don't have to ;-)

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    Sorry ignore me, i misread the variables bit. thought you had them in Workbook_OPen.
    I misread.

  6. #6
    Registered User
    Join Date
    06-13-2013
    Location
    Katowice
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    Thank you very much for your advice. Of course it works, unfortunately partially.
    Please Login or Register  to view this content.
    I tried to set the object type as "Worksheets" or "Sheets" but this generated the same error message.

    Regards,
    Michal

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

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    Try this.
    Please Login or Register  to view this content.
    By the way, where are you going you be using these variables?

    If it's only in the userform module you could set them when you open the userform.
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    06-13-2013
    Location
    Katowice
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Setting up variables for the userForm on Workbook_Open() - ERROR 91

    Hi Norie,
    The solution you proponed worked. Thank you.
    I am setting those variables because I remember problems when I was creating an application 2 years ago. The app worked fine until the next excel document was opened and "active". If I switched with my mouse and activated the "main" excel document, that I didn't have any problems, but when I saw the user form, but some other excel document was in the background there were some errors shown - I was doing also some other works an work and sometimes it required some other excel document to be opened (casually) - 10 times app worked without a glitch and suddenly the errors about missing variables, object required and so on. Then (suddenly) it works again and few hours later the same. Two days until I connected those two facts :-) This is why now I am setting those two variables at the beginning, so even if I have more excel documents opened my app will always work correctly.

+ 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. Run-time error '76' - Path not found - (Workbook_Open) - UserForm
    By Francisco Sousa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-23-2013, 07:15 PM
  2. Setting Global Variables from UserForm
    By vikasnitk85 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-13-2012, 11:04 AM
  3. [SOLVED] Range.Select error and screen maximizing error in Workbook_Open()
    By Punsterr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-10-2006, 05:20 PM
  4. Range.Select error and screen maximizing error in Workbook_Open()
    By Punsterr in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2006, 03:00 PM
  5. Setting Public Variables Error
    By clmarquez in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-16-2006, 01:30 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