with a VBA userform, how do you populate a combobox with a different text and value? - vba

I am trying to populate a VBA userform combobox with a named range, and I want the text value to display in the combobox for the user to select, but the value I want returned is the cell reference to the initial named range. That way it would point back to the named range, and if I change that text, it would automatically update all the references I have on the sheet that were entered by the form.
Right now I can get the named range to populate the combobox by iterating through the range, and using an .AddItem to give it the textual value, but then it just dumps that textual value into my spreadsheet, when instead I would like it to have the cell reference from the range that populated it.

The easiest way to do this is to create an Array(X,2) X being the number of items in the array. Then you put the String you want to display in Array(1,1) and the data you want in Array(1,2) then simply populate the combo box like normal. When you and use the array to reference which item they choose. They choose item 8, you know it is Array(8,2) If you need more help please post some code.

So, after more research it would seem that I am unable to directly accomplish what I want with the combobox. I ended up just looping through the range to fill the combobox with the textual values, and then when I submit the form, I looped through the same range of values, checked it if the submitted value matches one from the range of values, and if it does, then return the .Address instead of the .Value, and that gave me the cell reference I was looking for. Too bad the combobox doesn't work more like a real html select menu, it would have made my life a little easier, but in the end I was able to make it work, it just isn't as clean as I would have liked.

Related

How to maintain formatting for numbers in multi-value combo box?

I have a table that holds data of type Number that has a format of "R-"00 and in my Form that is linked to this table, I have a multi-combo box. When I go to select the values, they are correct - they all have the "R-00n" formatting where n is the actual number entry (auto-numbered). However, when I make my selection, the value will just read as 1, 2, 3 instead of R-001, R-002, R-003. How do I maintain my formatting after selections?
My query is SELECT Reference_ID FROM References for the Data of the combo-box in the form and it is pulling the right records, formatted correctly ("R-001, R-002, R-003") but when I make my selections, the value in the form is only 1, 2, 3. It drops the formatting. Is there something I need to configure in my Form to maintain the custom formatting?
Thank you.
I suppose you mean ListBox and not Combobox, right?
So I also suppose you already have some code to read the selected items of your ListBox like this:
Dim item As Variant
For Each item In YourListBox.ItemsSelected
Debug.Print YourListBox.ItemData(item)
Next
Regarding your problem:
You wrote that the tables field Reference_ID stores the number itself, but because it is formatted by "R-"000 it displays values like R-003.
So you already know, that the value itself stored is without formatting.
You could check that by retrieving the data like this:
?DLookup("Reference_ID","References","Reference_ID = 3")
It will show up 3 and not R-003.
So the same belongs when you read the data from the ListBoxes selected items.
As far as I know you can't read out the formatted text from the ListBox. But what you can do is to format the read value yourself:
Dim item As Variant
For Each item In YourListBox.ItemsSelected
Debug.Print Format(YourListBox.ItemData(item), "R-000")
Next
Another approach could be to set the Row Source of your ListBox to already formatted values:
Select Format(Reference_ID, "R-000") From References

Index Match in excel userform

I have my userform which contain 2 textbox for the target value and the actual value.
TextBox1 will be my target value while TextBox2 will be my actual value.
When the actual value is being key in, the userform will look into the worksheet "Target" and check whether is there any amount need to top up to reach the target value or not.
Here will be my data inside my "Target" worksheet:
Cell G1:S1 will be my target value while F2:F19 will be my actual value.
If textbox2(actual value) equal to 20A and my textbox1(target value) is 5S,
When go to the next field, textbox3 to fill in others data, it will show a pop up message to alert user to add 50ml conpac in order to reach the target value.
I have been research through webpage but seem like index match does not help in this condition. Please help.
Thank you
If you create the following named ranges: G1:S1 named "Target_Value", F2:F19 named "Actual_Value" and G2:S19 named "Additions" then:
=INDEX(Additions,MATCH("20A",Actual_Value,0),MATCH("5S",Target_Value,0))
Will return 50. You can put this in a cell somewhere or modify it to work in VBA (using Application.Worksheetfunctions)

How to perform a VB vlookup from a userform entry

I am trying to create a script for a invoice, that can perform a vlookup based on the entry in a userform. I have a userform, which has a combobox with a named range. I would like four text boxes, which display the result of the vlookup.
I have a vlookup set up, which just goes into the normal cell in the worksheet. it is:
=VLOOKUP($A22,Products!$A$1:$B$1679,2,FALSE)
I would like it, so that rather than looking for cell A22, it performs the vlookup from the combobox, which is just named test. I would then like to repeat it for:
=VLOOKUP($A47,Products!$A$2:$A$1679,1,FALSE)
=VLOOKUP($A47,Products!$A$2:$A$1679,1,FALSE)
=VLOOKUP($A47,Products!$A$2:$C$1679,3,FALSE)
Thanks for reading, and I hope that you can help. If you could
Refer to the combobox control's .Value property, pseudo-code:
=VLOOKUP(UserForm1.ComboBox1.Value,Products!$A$2:$A$1679,1,FALSE)
Etc.
Actual code:
Dim lookupRange as Range
Set lookupRange = Worksheets("Products").Range("A2:A1679")
Product = Application.Vlookup(UserForm1.Selectprodcutcombo.Value‌​, _
lookupRange, 1, False)
Of course, you will need to modify the name of your form and combobox, based on your form's design.

Is it possible to Populate a listbox with a non-contiguous named range?

I have a single column list box, which I want populated by table names that are on one worksheet. I've Ctrl clicked all the names and named them, however when I enter the range name into the properties table of the userform, they don't show up. What am I doing wrongly?
I would also state that I want to avoid creating a separate list on another worksheet.
What is intriguing is the fact that when you copy non contiguous data and paste it, it pastes contiguously. How does excel do this? I've recorded the macro and it's just 'activesheet.paste'. Can you paste into an array? ...to then use the array as the list, without having to create another list?
you can do this:
arrayname = rangename.value 'set named range to array
sheet1.listbox1.list = arrayname 'set array to listbox values

Show Cell Range on UserForm; then update

I've been using a crude method to help the user update some cells - by having them in a sheet. Is there any way I can display the various ranges in a userform, one by one, then have the user update them, click a button and move onto the next one?
Essentially, can I have Excel automatically generate an input form based on a range? The process of updating and saving back to the sheet I can do; it's the production of the correct form that I can't.
It's possible to do this, but the only way I can think of is to make a userform that automatically populates itself based on a range passed in. This way you could have different macros in Excel that call the form to populate based on different ranges. I built a proof of concept Excel file for trying this, and it seems to work, the only issue I can think of being that you need to figure out a way to tell the user what input field is what.
I think what needs to be done is to add controls programmatically to a userform (I name the textboxes as the cell address it's going to populate) then when the form is closed loop through all the textboxes and populate the cells with the textbox values.
You can see what I did at:
https://my.syncplicity.com/share/uicgbs3rl0/InputForm.xls
I think all that would need to be done is for you to work out how to add labels for the textboxes, and make sure the form is resized based on the controls you add...
I am not quite shure what you are looking for, but you could insert a second sheet and use it as a "form". An other way could be a dialog box with an input field.
Either way, you present the cells you want the user to change one by one, using a vba-function. You implement a "previous field" and a "next field" button, so the user can step through the range of cells. If the user hits "next field", you save his input and take the next cell from a previous defined range of cells.
You could have a "config field" in which you define the range of cells you want to change.
This is pretty rough and old-fashioned but if you have the data in standard list format - i.e. column headers in the first row of your range and then one record of data in each row below - then selecting a cell within the range and going Data > Form will give you a crude input form with roughly the functionality you need.
You can also do this in VBA by calling the ShowDataForm method of the appropriate worksheet. Just select a cell within whichever range you need first. The macro will remain paused until the user closes the data form