Search code examples
excelvbacombobox

Assign Numeric Value as a Variable for an Equation Based on Input From Combobox


I have a workbook that has various "calculators" for weld engineering on each sheet. For one of the calculators the equation is based on material density but rather than have the user input the correct value I want to use a combobox with a list of materials which I can then have the macro use assign the correct density value to in the equation.

I haven't used combobox before, so I honestly have no idea how to accomplish this step and my searches haven't yielded anything useful thus far.

I have the combobox populating the materials when the workbook opens (located in "ThisWorkbook") and once I get to the point where I can assign, say .2854 to "Steel" in the macro for the sheet ("Deposited Weight") I can get the rest.

Sub Workbook_open()

With ThisWorkbook.Worksheets("Deposited Weight").ComboBox1
    .AddItem "Steel"
    .AddItem "Stainless"
    .AddItem "Aluminum"
End With

End Sub

Solution

  • I was able to find a solution that allowed me to keep everything in the macro rather than building a table somewhere on the worksheet. I really want to keep the calculator sheets clean.

    I found another question where the value of the combobox was needed as a string which turned on a lightbulb, I could use a couple if-then statements to define different values to a single variable based on the input from the combobox.

    It's not the most efficient solution but for just 3 selectable options in the drop down it works. (Combobox name was changed from "ComboBox1" to "CboMaterials")

    Dim MatType as String
    MatType = Me.CboMaterials.Text
    
    If MatType = "Steel" Then
        Dens = 0.2854
    End If
    
    If MatType = "Stainless" Then
        Dens = 0.2901
    End If
    
    If MatType = "Aluminum" Then
        Dens = 0.0975
    End If