I'm unable to find out this information: Is it possible to show labels in these two type of lists rather than the real values ? of course when a label is selected, the value of the cell or the combobox (as a Form control) gets the real value.
Example:
* Data
Product A <------> 10
Product B <------> 11
Product C <------> 22
Combobox shows: Product A, Product B and Product C
If I select A I get the 10 value, B the 11 value and C the 22 value
Thank you in advance
Miloud B.
The typical way of doing this is to have a lookup table. There's a function named something like VLOOKUP (that's a partial name) that you can use in formulas to retrieve the value. I did something like this at my job, and I created a lookup table in a separate spreadsheet, and then called the lookup function in my main one.
You can also use macros, but that is too much bother in my opinion for the current problem.
Data Validation: You can't have two values per row. You can use a lookup table to convert one value to another. Or you can combine the values like "ProductA_10" and use a formula to extract the "10" where you need it.
Form Combobox: This also has limited options and doesn't really offer anything more than DV does for what you want.
Toolbox Combobox: This can do just about anything you want. You would set the ColumnCount property to 2, the BoundColumn property to 2, and the ColumnWidths property to something like "1;0" to hide the second column. If I had ProductA-C in A1:A3 and 10-12 in B1:B3, then I would use code like this in a standard module to fill the combobox
Sub LoadCombobox()
Dim rCell As Range
For Each rCell In Sheet1.Range("A1:A3").Cells
Sheet1.ComboBox1.AddItem rCell.Value
Sheet1.ComboBox1.List(Sheet1.ComboBox1.ListCount - 1, 1) = rCell.Offset(0, 1).Value
Next rCell
End Sub
And code like this in the sheet's module to put a value in a cell (F1 in this example)
Private Sub ComboBox1_Change()
Me.Range("F1").Value = Me.ComboBox1.Value
End Sub
Whenever a new value in the combobox is selected, F1 is updated. We can use the Value property of the combobox because we set the BoundColumn property to the column with the value we want.
Related
I have an excel file with 2 sheets
In the first one, I have two columns:
In the second, I have an SQL query that loads into Excel that looks something like this:
I have validation on the first column, limiting the value to column A, so the user can either start typing which item he wants, or use the dropdown to pick.
My issue is that I want to create validation on the second column, so that only the options applicable to that Item are available (ex. if the first column is Item 1, only options A, B, and C are available to be selected.) The SQL data is dynamic (possibly different options in Column B for each A), and new items are added and removed. The solutions I saw (ex. https://trumpexcel.com/dependent-drop-down-list-in-excel/) involved creating the two filters on two axis, but I'm not sure how that would work in my situation.
If you can create a list of unique values from the first column to use as the source for the first drop-down (e.g. like in ColD below), you can configure the second drop-down to use a "Source" formula like:
=OFFSET($B$1,MATCH(I2,$A:$A,0)-1,0,COUNTIF($A:$A,I2),1)
Where I2 here is the corresponding drop-down pointed at ColA values (see example screenshot)
Assuming your source data is sorted on ColA.
EDIT: adding a different (perhaps more flexible) approach:
Define a workbook name like "validation" which points to a VBA function which returns a range:
Corresponding function in a regular code module:
Function getOptions() As Range
Dim m As Long, c As Range, v
Set c = Application.Caller 'getOptions is called from the cell
' where the drop-down is clicked
v = c.Offset(0, -1).Value 'read cell (e.g.) to left of the one being filled
'Debug.Print v
If Len(v) > 0 Then
'do the same thing we did with the DV list Source formula above
m = Application.Match(v, c.Parent.Columns("A"), 0)
If Not IsError(m) Then
'return a range (must be contiguous)
Set getOptions = c.Parent.Cells(m, "B").Resize( _
Application.CountIf(c.Parent.Columns("A"), v), 1)
End If
End If
End Function
Finally, set up your cell validation list to point to =validation
The advantage of this approach (despite being a little more set-up and requiring a macro-enabled workbook) is that the VBA in the function can look at the context of the calling cell (via Application.Caller) and decide what range of cells to return to be shown in the drop-down.
I have in my userform listbox which display 10 columns. All rows have an individual ListIndex.
I would like to display f.e. in Msgbox value after select some row and click button "Show".
But this value is not avalible in listbox. So, I thought that all rows have individual ListIndex, I can use VLOOKUP for finding my target.
For example: I selected row no. 1 then ListIndex is 1 and this is my Look up Value. In my database sheet I have the same individual ID values.
Then of course, I have to declarate range, number of column and parameter "False".
Theoretically, I expect the result after that but it doesn't work.
My code:
Dim indexno As Long 'this is my delcaration for finding Look up Value
Dim myVLookupResult As Long 'this is my declaration for VLookup Result
indexno = ListBoxResult.ListIndex 'my Lookup Value is dynamic and depend of selected row
myVLookupResult = Application.VLookup(indexno, Worksheets("DataBase").Range("A1:J100"), 5, False)
MsgBox myVLookupResult 'should display result of VLOOKUP
But the result is error: Run-Time error: 13 - Type mismatch.
I guess the problem is with convert type of Dim from int to string.
Someone could support me, please? Thanks in advance!
The lookup value of a VLOOKUP worksheet function must be of the same data type as the data in which it is to be found. In your code the lookup value is of Long data type. If the column in which you are looking for it has text, the number you are looking for will not be found. So, you change the lookup value to Variant and hope that Excel will be able to work out what you want. But the better way is to examine your data column and look up the type of value you actually have there.
Next, given you are looking for a number which you have assigned to a Variant and Excel, in consequence, managed to find the string-equivalent of that number, that would be the functions return value, a text string. In most cases Excel is quite generous in this sort of situations but if it does complain about "Data Type" then it's because you are trying to assign a text string to a variable of Long data type.
Other than that, as #Michal Palko already pointed out, the ListBox's ListIndex is 0-based. If the numbers in your worksheet are 1-based the return of VLOOKUP won't be "totally different" but it will be from the adjacent row and therefore unexpected.
But I want to alert you to another possibility. As you know, you can load many columns into your list box. You can show some of the columns and hide others but you can access them all with code like this:-
With ListBox1
Debug.Print .List(.ListIndex, 3)
End With
This snippet will print out the value of the 3rd column in the row of ListIndex. You might also assign this value to a variable and, perhaps, have no need for VLOOKUP.
Good day,
I'm at a loss on this problem.
I have a group of cells that contain words, like apple, this word would be the value. It is separated by a symbol for completing the math. They can be changed by the user to make custom calculations.
Cell A1 is "apple", B1 is "+", cell C1 is "apple", cell D1 is "*", cell E1 is "apple", call F1 is "=" and cell G1 is the suggested total, in this case would be "6".
It would be posted as | apple | + | apple | * | apple | = | 6 |
The legend holds the value for the word, so if you enter 2 in the legend, apple would be 2.
The logic would determine that the formula would be 2+2*2= if written in excel, I would like to combine all the cells and calculate this.
I tried using =sum, sumproduct, concate and the like to no avail.
Any head way I did make, I ended up getting BEDMAS wrong as it calculated it as 2+2=4*2=8, instead of the correct 2*2=4+2=6.
Anyone know of a way to combine these cells and properly sum the values to the correct total?
Thank you for your time!
Go to the Name manager and create named range Eval, into Refers to field add formula:
=EVALUATE(CONCATENATE(VLOOKUP(Sheet1!A1,Sheet1!$A$3:$B$5,2,0),Sheet1!B1,VLOOKUP(Sheet1!C1,Sheet1!$A$3:$B$5,2,0),Sheet1!D1,VLOOKUP(Sheet1!E1,Sheet1!$A$3:$B$5,2,0)))
In range A3:B5 I have legend.
Change references as you need. Then in cell G1 write formula =Eval.
Sample:
This is a UDF based solution. Its advantage is that it's more versatile and by far easier to maintain if you learn its elements of code. The disadvantage is in that you do have to learn the elements of code and you have an xlsm macro-enabled workbook which isn't welcome everywhere.
The setup is simple. I created a table with columns Item and Value. I placed it on another sheet than the task. In fact, you could make the sheet with the table VeryHidden so that the user can't look at it without access to the VBA project, which you can password protect. I called the table Legend. The item columns has names like apple, pear, orange. The Value column has the numeric values associated with each name.
Note that, since this is a VBA project, the entire list can be transferred to VBA leaving no trace on the sheet. You could have a function to display the value of each item as the user clicks on it and have it disappear as he clicks elsewhere.
Next I created a data validation drop-down in A1 with the List defined as =INDIRECT("Legend[Item]"). Copy this cell to C1 and E1.
Then I created another Data Validation drop-down in B1 with the list as +,-,*,/. This drop-down must be copied to D1.
Now the code below goes into a standard code module. Find the way to create it because it isn't any of those Excel sets up automatically. It's default name would be Module1. Paste the code there.
Function Evalue(Definition As Range) As Double
Dim Task As String
Dim Fact(2) As Double
Dim C As Long
Dim i As Long
With Definition
For C = 1 To 5 Step 2
On Error Resume Next
Fact(i) = Application.VLookup(.Cells(C).Value, Range("Legend"), 2, False)
i = i + 1
Next C
Task = "(" & Fact(0) & .Cells(2).Value _
& Fact(1) & ")" & .Cells(4).Value _
& Fact(2)
End With
Evalue = Evaluate(Task)
End Function
Now you are ready for testing. Call the function from the worksheet with a call like
=Evalue(A1:E1). You can use it in comparisons like =IF(G6 = Evalue(A1:E1), "Baravo!", "Try again"). As you change any of the components the cell with the function will change.
Remember to use absolute addressing if you copy formulas containing the range. If you need to get a result in VBA while testing, use this sub to call the function.
Private Sub TestEvalue()
Debug.Print Evalue(Range("A1:E1"))
End Sub
My Sheet
Here is what I have.
In cells M - U, i count all the instances of the word from cells E, G and I from the legend.
=SUMPRODUCT((LEN(E3)-LEN(SUBSTITUTE(E3,$B$3,"")))/LEN($B$3))
In cells W - AE, I multiply the instances with the value to give me a total each time the word appears.
=SUM(M3*$C$3)
In cell E8 - I8, i add the three possible values together.
=SUM(W3:Y3) so each worded cell is now a number.
I'd like to take the cells E8 - I8 to make a calculation in k8 and so on.
So, each cell is put together to make an
=SUM(E8:I8)
statement, which all works except E11 - I11 which equates to 26 instead of 43.
I have created dependent dropdown using indirect function and data validation using which i have to create dynamic charts.
Whenever i am selecting an option in the first dropdown, the values gets updated accordingly but i need to click on the second dropdown to view the related options.
For example:
The first dropdown contains fruits, vegetables and colors. I have selected fruits first and the options are banana, apple, mango. I select mango in the second dropdown. Now, when i am selecting colors in the first dropdown, the second dropdown still shows mango, until i click on it and see various 'colors'(red, yellow, green) option.
Is there a way so that the first option in colors(red) appears in the second dropdown as soon as i select colors in the first dropdown?
I am using indirect of the cell link for dependent dropdowns.
Please suggest answers without VBA
I don't think you can achieve this W/O VBA help. If you wish to consider VBA approach, then please paste this code in you Worksheet module of sheet where you have data validation.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
Dim x As String, y As String
x = Range("A1").Value
y = Application.WorksheetFunction.VLookup(x, Range("A11:B13"), 2, False)
Range("B1").Value = y
End If
End Sub
This assumes that you have your data validations in Cells A1 and B1. you will need to add one simple lookup table (A11:B13 in my example) where you will put name of category in A column and related first value in B. column.
Worksheet_Change event will trigger only when cell A1 change its value.
I have very little experience with VBA and I'm now stumped by what I'm trying to accomplish with a macro. Excel 2010.
I have 3 relevant columns. B, C, and AD. (Columns 2, 3, and 30)
My data is filtered down to about 30 rows, almost none of which are contiguous (about 500 rows total).
I want the following to happen:
A formula is entered in ONLY THE VISIBLE ROWS in column AD, which will look at the value in column B of that same row and check for that value in all of the VISIBLE CELLS in column C. It cannot look at all of the cells in column C, only the visible ones.
If the value from column B in that row is found anywhere in the VISIBLE CELLS in column C, then "True" should be returned in column AD. I don't care about what is returned when the value is not found, as I will be filtering for the "True" values only.
As an added requirement, if the first 3 characters of the value in column B are "010" I need it to return a value of "True" in column AD. I then need this formula copied down column AD for each VISIBLE row.
Right now, I have a formula that will conduct the search in column C for the value in column B. (found on stackoverflow)
=NOT(ISNA(VLOOKUP(B4,C:C,1,0))))
This provides a "True" in column AD when the value from column B is found somewhere in column C. With the "010" constraint, the formula looks like this:
=IF(LEFT(B4,3)="010","True",NOT(ISNA(VLOOKUP(B4,C:C,1,0))))
I am having a problem in that this looks at even the hidden (filtered out) rows. Every one of my values in column B will appear in column C at some point, so I'm only getting "True" for all my entries.
I think there must be a better way to do this than just having a macro paste the formula down (even considering I can't get the formula to work). So, 2 questions:
Is the formula the right way to go in this case, and, if so, can anyone tell me how to get it to only search the visible cells in column C?
If code is the best way (I'm guessing it is), can anyone show me an example of code that might work?
You're almost there. On its own, COUNTIF does not have this capability, but if you throw user-defined functions (UDF functions) into the mix, you have an elegant solution.
Add the following code to a module in your code-behind.
Function Vis(Rin As Range) As Range
'Returns the subset of Rin that is visible
Dim Cell As Range
Application.Volatile
Set Vis = Nothing
For Each Cell In Rin
If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
If Vis Is Nothing Then
Set Vis = Cell
Else
Set Vis = Union(Vis, Cell)
End If
End If
Next Cell
End Function
Function COUNTIFv(Rin As Range, Condition As Variant) As Long
'Same as Excel COUNTIF worksheet function, except does not count
'cells that are hidden
Dim A As Range
Dim Csum As Long
Csum = 0
For Each A In Vis(Rin).Areas
Csum = Csum + WorksheetFunction.CountIf(A, Condition)
Next A
COUNTIFv = Csum
End Function
Now you can use the new COUNTIFv() function to do the same as count, but only include visible cells. This code example was sampled from Damon Ostrander's answer to a similar question, so you will probably need to tweak it slightly. You can either use the COUNTIFv function in the macro itself, or modify the VLOOKUP function in a similar fashion to use the worksheet function example you have already. Neither method is really better than the other, so either should work for you.