Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Excel - VBA I need to access a variable from a worksheet on a userform?

How can i make reference to a variable that is on a worksheet code on a userform.

I have a Loop as part of a button click where I change the value of a variable multiple times. Some instances trigger a userform. I need to use that value in the userform

I have read that I would need to use a Module but I can't figure it out.

I have made my own beginner (sloppy) work around by setting a worksheet cell equal to the variable and then using that cell in my userform instead of a variable.

I have never used a module in VBA so i'm not sure where to start.

I'll be checking back on Sunday so ask questions if need be.

Thanks in advance,

Bru

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Aaron, you don´t know but if you already wrote a code inside a form, you already use a module.

    A form has it code inside it, so you see just the form in object explorer. A module is used to write code without dependency of a module, you can call its procedures or functions from any place inside excel. A form module keeps all its function, by design, as private, prevent access for them.

    To create a module, go to Insert menu, choose module inside visual basic editor.

    After this, create a sub, like this:

    function sheetsCounter() as long

    sheetsCounter = activeworkbook.sheets.count

    end sub

    To test, press CTRL + G (calls instant windows) and type:

    ?sheetsCounter (press enter)

    Excel will show you the number o sheets inside active workbook.

    So, if you write the same function inside your form, it will work only inside you form and other forms couldn~t use it.

    Think in a module like a place to share things, reuse things and form module to keep things hidden from other eyes.

  • 1 decade ago

    your question is extremely hard to understand. you say you don't know how to use code in a module, and yet you do use a set cell reference in the code?? then you are already in the module.

    what kind of variable do you need to set??

  • Anonymous
    5 years ago

    if you are in the same form and working with vba then you can define by the following: Me!Input_Name.Value or Form_Name!Input_Name.Value if you are trying to get a value in another form you have to use: Form_Name!Input_Name.Value

Still have questions? Get your answers by asking now.