Get Combobox Value when only one entry available - excel

I have a Combobox on a UserForm populated with multiple values.
Based on user input I keep redefining the Combobox to less and less entries in the DropDown.
At the point where there is only one value left in the Combo Dropdown, how do I get that value to appear in the Combobox Text field?

You can access ComboBox values via the .List property.
expression.List(pvargIndex, pvargColumn)
keep in mind, the pvargIndex (row) and pvargColumn (column) do begin with the index 0.
So to the access the first element, you would use ComboBox.List(0, 0) = something

I think I've found my own solution.
The .listindex, after the fact, always comes up as -1 but each time I re-filter the combobox elements I can keep overwriting a variable within the AddItem loop with what the Combobox value is. When my count of elements = 1 then the value stored in that variable will be the last, and only, value that I was looking for.

Related

Excel : have cell range stored in cell to be used by combobox

I am using a ComboBox (basically a drop-down list) in excel, and I would like to select a value from a range. Thing is, I have around 15 of those comboboxes that are linked to the same list, and I'd like to not have to modify the ranges for all of them if I add a value to my list.
Hence my idea of telling the dropdown to take the array given in cell A1 of my data sheet as input for the actual array...
I'm not sure I'm being clear so here's an example : my list is in the range A3:A25. I want to have "A3:A25" written in cell A1, and have my dropdown list take the value of A1 as the actual range, so that if I add an entry to the list I can change A1 to "A3:A26" and not have to change all of my lists.
Thanks in advance
Change the reference to:
=$A:$A
Be sure to tick Ignore blank.
Turns out I just selected my list and called it "List" (right click, -> define name), and defined the input range as "List".
Now if I add a value, I just have to redefine List.
Thanks

VBA Combobox, choose item using 2nd column item

I have a UserForm containing ComboBox and items added to it (2 columns). When adding data of invoice I have button to insert data from latest invoice - to save some time.
ComboBox shows user only 1st column items and saving data using 2nd column items. When I want to copy data from last invoice it shows me error using below code. Code tries to select item using 2nd column item.
Is it possible to select proper item in 1 column using 2nd column item?
kraj_dostawcy is ComboBox name
kraj_dostawcy.List(kraj_dostawcy.ListIndex, 1) = fak_path.Offset(-1, 5)
If you use the control's BoundColumn property to set the column containing the values you want returned from it, you can just assign the cell value to the Value of the combobox, not to its List property.

Excel VBA Pass Id of Value in Combobox

Looking for advice on the best way to pass an Id of a value into a parameter in excel VBA.
Essentially I'm trying to replicate getting the value rather than the text itself like for example in html:
<option value="1">Option one</option>
Would return 1. I could concatenate the Id to the start or end of the string with something like:
.additem varList(0, 1) & " | " & varList(1, 1)
But I'm looking for a 'cleaner' option if that makes sense?
Cheers
Create your combobox with at least 2 columns. This can be set using the ColumnCount property, via the VBE or through VBA code.
You can then adjust the ColumnWidths property to make one of the columns a width of 0 so it will not be displayed/visible to users.
WHen you populate the combobox, simply put the ID in one column of the ComboBox, and put the value in the other visible column. The interface will look like this, unless you adjust the columnwidths
Use the BoundColumn of the ComboBox to return the appropriate value, or you can do some iteration over the selected item(s) and refer to the indexed position:
Debug.Print Me.ComboBox.List(0, 0) '# Display the first row item in the first column
Debug.Print Me.ComboBox1.List(0, 1) '# Display the first row item in the SECOND column

How to select next item in Data Validation list using VBA

I have a Data Validation list with dates in it. When you change the date it effects what data is shown in the rest of the worksheet.
I would like to to create 2 command buttons,
"Next" - when clicked will move to the next date in the list, when it reaches the end of the list it goes back to the beginning of the list
"Previous" - when clicked will move to the previous date in the list, when it reaches the beginning of the list it will go to the end of the list
Is this possible?
I did look at List Box and Combo Box but got highly confused with the coding!!!
Any help would be great!
You have to distinguish 3 cases:
data validation with in-line list elements (e.g. Source = "1;2;3;4;5")
data validation with list elements in a range
List/Combo box
Case 1 is maybe the most difficult, because you can access the list elements only in a string and have to split them up into an array, get the index of the current selection and move the index with two buttones in order to get the wanted result
Case 2 is a bit simpler, but again you need to somehow keep track of the current position within the range defining your dates
Case 3 is maybe the most easiest to implement ... but still requires a certain coding effort, like
load list of dates into the list/combo box before first display (OnLoad or OnActivate
create code for the UP and DOWN buttons to increment/decrement the list box index, with automatic wrap-around
I suggest a 4th case to you ... use an ActiveX Spin Button ... this provides up and down functions in one element:
create your list of dates in a vertical named range DateList
reserve one more cell for the index of the Spin Button and name it DateIndex
using Developer ribbon, insert an ActiveX Spin Button (default name is SpinButton1)
set the LinkedCell property in SpinButton1 (be sure to be in Developer / Design mode; right click the Spin button and select "properties") to DateIndex
create the following code (still in design mode, right click the SpinButton and select "view code")
code
Private Sub SpinButton1_SpinDown()
If SpinButton1 = 0 Then
SpinButton1 = Range("DateList").Rows.Count
End If
End Sub
Private Sub SpinButton1_SpinUp()
If SpinButton1 = Range("DateList").Rows.Count + 1 Then
SpinButton1 = 1
End If
End Sub
At the cell where you want to display the selected date, enter formula =INDEX(DateList,DateIndex)
since you are working with named ranges, DateList and DateIndex may be in a different sheet (even a hidden one) than the user sheet.
if you want to create a copy of the currently chosen date at the cell where the user has currently put the cursor, add the following statement to the end of the SpinDown/Up Sub's (after End If: Selection = Range("DateList").Cells(SpinButton1, 1)

Get selected multi value data into string

I have a field in a sharepoint list which is a look-up from another list. It is displayed on the infopath form as a multi value checkbox.
I am trying to work out a way to perhaps concat each of the selected items into one string.
For example; the Multi-Value checkbox contains
(! = checked, * = unchecked)
! Example A
* Example B
! Example C
I would like a new field called MyString to equal "Example A, Example C"
I have tried to do a rule with a button press to query the secondary datasource for the external list, and filter it on the Multi-Value checkbox value. This works brilliantly for attaining the second column (Dollar Amount) by doing a sum(secondary.amount) with the filter - however I am still unable to work out how to join the selected strings.
Any hints would be most appreciated!
You could have a rule on each of those checkboxes that runs when the checkbox is changed. Each rule would look up the other checkbox values, concatenate them how you need it, and set the value of the final MyString textbox field to that string.

Resources