VBA ComboBox Value by Index

32,406

I'm not sure if I got it right, but I think you want to get the list index of the selected item. You can get that by

x = io6.ListIndex + 1

The +1 is there since ListIndex is 0 based, but you wanted it 1 based.
And something completely different,
Remove the brackets. Method calls without return values does not use them in VBA

Edit after comment
To take this the other way, i.e getting a value from an index value, do like this:

y = io6.List(x - 1)
Share:
32,406
addohm
Author by

addohm

Updated on August 30, 2020

Comments

  • addohm
    addohm over 3 years

    I've got a form that contains a combobox which contains the values

    With io6
       .AddItem "90°"
       .AddItem "180°"
       .AddItem "270°"
       .AddItem "360°"
    End With
    

    The member of this method I am actually using is .ListIndex. When a user selects 270 degrees, I am sending a 2 to a variable. When I read the variable, I want have the combobox correctly show the appropriate value. In other words, if the variable is 1, I want the combo box to show 180 degrees. Is that achievable without a select\if statement that writes into .Value?

    Sub varChange(val as integer)
    
       With comboBox
    
         Select Case val
           Case 1
             .value = "90°"
           Case 2
             .value = "180°"
           Case 3
             .value = "270°"
           Case 4
             .value = "360°"
         End Select
    
        End With
    
    End Sub
    

    The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?

    • CLR
      CLR almost 7 years
      .Value = ((.ListIndex + 1)*90) ?
    • addohm
      addohm almost 7 years
      No, my question is being misunderstood. See example of current execution.
    • A.S.H
      A.S.H almost 7 years
      myCombo.ListIndex = idx; did you get a problem with this?
    • addohm
      addohm almost 7 years
      This is not what I am asking. I know that .ListIndex provides the index of the item selected. What I want is to set the item selected by providing the index.
    • A.S.H
      A.S.H almost 7 years
      The statement I suggested above does not provide the index of the item selected; it rather sets the combo by providing some index idx*.
    • jsotola
      jsotola almost 7 years
      i just created a combo box in excel, input range: A1:A4 . call link: B1 . A1:A4 has the four values in your example. ... select 270 in combo box ... B1 changes to 3 ... combo box shows 270 ... enter 4 into B1 ... combo box changes to 360 ....... is that how you want the combo box to behave with VBA control?
    • addohm
      addohm almost 7 years
      Yep, that sounds like it. Except I'm doing it in an MSForm, but it's probably the same.
  • addohm
    addohm almost 7 years
    No. I guess I need to be more clear. I want to show the combobox value based on the variable. In other words, if variable = 1, I want to show the combobox value who's listindex is = 1.
  • addohm
    addohm almost 7 years
    I do appreciate your effort, but that's still not right. In fact, that wouldn't work at all. The .List property is used for creating list items from an array.
  • Sam
    Sam almost 7 years
    msdn.microsoft.com/VBA/Language-Reference-VBA/articles/… says "Returns or sets the list entries of a ListBox or ComboBox"
  • M--
    M-- almost 7 years
    I think adding explanation about your solution would be necessary.
  • braX
    braX almost 7 years
    There isnt much to explain... combobox is the name of the combobox, and the .List property is how you get data out of the combobox, specifying the ListIndex as the row argument, and 0 for the column argument. that will return the value in the first column of the selected item to the sValue variable. In this case, there is only one column in the combobox.
  • M--
    M-- almost 7 years
    I know all of these. But even editing and adding a line and maybe a link to documentation would make your answer to not show up in the low quality post queue.
  • addohm
    addohm almost 7 years
    This is where you and everyone else seems to be getting it wrong. I'm not trying to get data out. I'm trying to change what data is displayed based on an index variable.
  • braX
    braX almost 7 years
    Not sure what you mean, but you can do the reverse as well... combobox.List(combobox.ListIndex,0)="New Value"