-4

So I need help finishing up a app lookup on elements from the periodic table. The idea if to have the user input the element in the combo box in Userform1 which has a drop down list. To then depending on the element chosen be directed to Userform2 that has a text box, and in that text box be given the atomic mass for each element . I need an example of how and where to write the code in either userfroms. This is what i have so far. UserForm1 name is Element_LookUp Userform2 Name is Element_LookUp_Result

Userform1 :

Private Sub CommandButton1_Click()

    Unload Me                   'Closes The Screen after the click
    Element_LookUp_Result.Show  'Shows The result scrren in a new pop up

End Sub

Private Sub UserForm_Initialize()

    ' All 118 Elements will be shown on a drop down list
    ' The elements are in order ; autofill helps input the element.

    ComboBox1.AddItem "Hydrogen"
    ComboBox1.AddItem "Helium"
    ComboBox1.AddItem "Lithium"
    ComboBox1.AddItem "Beryllium"
    ComboBox1.AddItem "Boron"
    ComboBox1.AddItem "Carbon"

End Sub ' Not all the elements are listed below i just wanted to save time

UserForm 2 :

Private Sub UserForm_Terminate()

    Unload Me               'Once the (X) is clicked the result screen goes away
    Element_Lookup.Show     'Result scrren goes back to the input screen again

End Sub
3
  • Possible duplicate of Pass data between UserForms Commented Nov 30, 2018 at 16:14
  • I think you want to pass data between two different userforms. But you also seem to have other issues in understanding how to build and use userforms in general. For that please have a look here Commented Nov 30, 2018 at 16:17
  • I'm still a bit confused could you show me how the code would work for lets say if input hydrogen in the comboBox in userform 1 and then want to display the "Atomic mass:1.007" in the textbox in userform 1. @Storax Commented Nov 30, 2018 at 16:44

1 Answer 1

-2

As you're maintaining data from different userforms/modules, etc., I believe you're looking to have a global variable... At the top of the module for your userform (for the purpose of this post, will say UF1 is userform1, UF2 as userform2), you will put (not inside of a subroutine):

Public ElementName as String

Now, in your commandbutton1_click:

ElementName = Combobox1.Value

In UF2 initilize subroutine, you will put something like:

Textbox1.value = UF1.ElementName

The above example would be how to just pass the variable. You could also use ElementName such as:

Textbox1.Value = Application.Index(OutputRange,Application.Match(UF1.ElementName,LookupRange,0))

With that index/match you have used ElementName to find an output, where you would want the column for atomic mass.

Sign up to request clarification or add additional context in comments.

3 Comments

Worked like a charm! Thanks! Now what if I want to display something else other then the name of the element. The atomic mass lets say. Would i have to make if statements for each element?
I would recommend having a single table with all of your data... columns(1) has element name, columns(2) has atomic mass, etc. That table can be used for many things... like initializing your combobox to use the element name range, then you use index/match to output the table data from any column based on the element name.
Also, if this solved the initial issue, please check as answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.