Get the id of the selected value in a dropdown validation list on Excel in VBA - vba

Using Excel, lets say I have a validation list made of 5 values like this one :
Patate
Tomate
Courgette
Concombre
Patate
In a cell containing a drop down list made of these 5 value, I select the fifth value : "Patate".
I want to get in VBA that this cell contains the 5th value of my validation list. Something like :
x = Cell.Validation.GetIDValueSelected
x = 5
I can't use Vertical search because I might have 2 or even more time the same value in my list (too long to explain why).
This list is also dynamics (depending of another sheets) so it doesn't always contains 5 values.
I hope I made it clear for everyone to understand my needs but I will be glad to add more information if needed.
Thank you for your time.

Sadly, once you have used DV to fill a cell with junk, there is no way to tell which piece of junk you picked:
You would have to pad each piece of junk with a different number of blanks.

Related

Excel VBA - PivotItems returns inexesting value

In an Excel Workbook, I have "static" pivot table on a sheet, that is based on data from another sheet.
I'm refreshing the data on my data sheet (thank you captain Obvious !), then I want to show ALL the items, exept the blank one, so I'm running throw all the PivotItems to set them to visible, and, at the end, unselecting the blank one :
i = 1
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").ClearAllFilters
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotCache.MissingItemsLimit = xlMissingItemsNone
For Each PvI In ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems
PvI.Visible = True
Next
ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems("(blank)").Visible = False
At the last occurs of my loop, on the 4th PivotItems, I have an error of execution '1004' (I'll translate it from french, it may me some errors, sorry) "Impossible to define the property Visible of the class PivoItem", so I checked a few things :
?ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems.count
4
for x = 1 to 4 :
?ThisWorkbook.Sheets("TCD").PivotTables(i).PivotFields("CODETAT").PivotItems(x)
(blank)
SFT
ACQ
TEP
It look like I have 4 items in my Pivot Table, but
And also, when I check my datas, I only have 2 different stats :
So where does this 4th PivotItems' element is coming from, and how can I get ride of it ?
Thank you.
I had a surprising issue like that, you need to check in the Pivot Table options :
Right click on the pivot table,
Select Pivot Table options
Go in Data tab
Find Retain items deleted from the data source
Choose None for Number of items to retain per field!
Then refresh! All should be OK now! ;)
(that thing drove me mad for hours!^^)

Copy row if value of one cell is "x"; and, auto-hide blanks

I'm trying to copy a row, columns A-J, representing a person and associated info, when column J is filled. I'd fill it with either w, x, y or z based on the reason people are away, and I'd like the row (A-J) copied to another sheet as soon as J is filled.
Here's a screenshot with both Excel documents. Please ignore the "Reason" column in the Pers sheet - I know it could be used, but my production one doesn't have this column, and will not. I just used it to illustrate for you guys.
That other sheet (Main) has headers, as you can see, for each reason why people would be away. This one would permanently be displayed on a big-screen TV. So, what I'm trying to do is :
Copy the rows to Main as soon as the "Departed?" row is filled in Pers;
Make sure those rows go into the proper category, in the first blank row;
Ensure each category remains collapsible.
You can see I've tried using the If function (syntax in screenshot). That worked OK, but :
Left blanks;
Biggest one : only worked row-for-row! (Also the cause of reason #1, I suppose...) Past row 11, nothing. Not too sure what to do about that...?
Hope this is somewhat clear? =) Let me know if you have any questions - thanks for your help!

Development DropDowns List error on empty value

I have a simple dropdown list named DD8. It uses 50 rows as control, the problem is that for now only 45 rows are used. That means that in the dropdown list there are 5 empty rows. The problem is that if someone select one empty row, or don't select anything (default is empty) the fallowing code will show error :
With Worksheets(1)
NameProf = .DropDowns("DD8").List _
(.DropDowns("DD8").ListIndex)
End With
I tried things like if .DropDowns("DD8").List (.DropDowns("DD8").ListIndex) != "" but ofc, it shows error. I searched how to select only used rows with the DropDown list of the development tab but it doesn't seem to be possible.
I have to select 50 rows because new customers can be added.
Do you know how it can be achieved ?
If new customers can be added, then I imagine, and hope for you, that it goes above 50.... so it's not just an issue of having 5 blanks for now to not be an option, but also allowing customers 50-2,483 also be on there when they come along. ---- Without more details on your code, I believe this suggestion should help 'guide' you but not immediately solve your issue.
Essentially, whenever you call to have your dropdown populated, you want some code to find the last row of data in the customers column, and then assign your dropdown to populate with the starting row of your customers to the lastrow. This way no matter how many customers you have 2, 48, 189... they will show in your dropdown without that issue occurring. A simple google search will yield how to find the last row in excel.
Sorry I couldn't just bust out the code to make it work for you right this second, but I think this should be a good starting point for you.

VBA/EXCEL Dynamic Named Range That Includes Both Text and Numbers

Information
I have a column of about 300 part numbers that is constantly expanding. These part numbers can be only numbers, only letters, or a combination of both. I'm using dynamic named ranges with the part numbers to fill combobox lists. When the user types in the part number in the combobox, if we've done that part before it will autofill as they keep typing. I've recently ran into a problem with this however with a recent group of parts that was added.The part number is just a six digit number, and are the only part part numbers that solely consist of numbers, every other one has letters, or if its just numbers there are hyphens in the part number. The combobox I'm filling with this named range does show these numerical part numbers in its list, but does not autofill them when the user types and does not autofill the other information associated with the part number in other textboxes when the part number is done being typed in.
Question
Can anyone help me understand why it isn't autofilling the numerical value but it fills every other value?
Code
Here is the code I'm using to fill the combobox list and the dynamic range formula. Let me know if you need anything else to help you give an answer.
=OFFSET('Part List'!$A$1,0,0,COUNTA('Part List'!$A:$A),1)
comboxPartNumber.List = Range("Part_Number").Value
List of Sample Parts
X-600-ASSY-SM
LO-5093-020-023
LO-5093-020-025
AB-1541754
764761
766415
I cannot add a comment, sice I am 2 points short :(, so I am forced to post this as a solution:
Try to make all the numbers strings.
Dim R As Range
For Each R In Range("Part_Number").Cells
If IsNumeric(R.FormulaLocal) Then R.FormulaLocal = "'" & R.FormulaLocal
Next
Let me know if it worked.

Excel VBA Change last two digits of a number

I´m trying to make a number to change its last two digits to zero.
In Excel, in a certain Cell I will always get for sure a number always higher than 100. Let´s assume that I get the number 26312. What I want is to the number to be changed for 26300. Another example, if I get the number 14920,
I want to be changed to 14900. I would like to post some code but the truth is that I´m not succeding in anything. I would appreciate some help!
Done it!
I used this:
Range("U19").Select
ActiveCell.FormulaR1C1 = "=IF(LEN(R[-12]C[-1])=5,LEFT(R[-12]C[-1],3)&""00"")+0"
I want the cell change to appear in U19. The cell containing the original number is in T7