I currently build a dashboard in Excel to track forex trades. One worksheet is for adding new trades to the database. Within this worksheet i have several fields that the user needs to fill in order to add the trade to the database. Now I want to avoid that the user inserts wrong data (regarding format) in the cells and therefor crashes the outputs from the database later on.
How can I define such criteria via VBA? For example the field date to have the format YYYY-MM-DD and the field Trading Volume XX,YY so the user can't (accidentally) insert X,YY etc. For dropdown fields it is easy by defining the inputs within the dropdown menue itself, but i need some solution for the manually filled cells.
I will then define a msgbox to pop up and inform the user to meet the required input formats.
Thank you very much in advance!
Some key words would help me, I can then get into deeper it and code it by myself.
Rather than using code could you not use data validation and force entry of a date, numeric value etc.?
If you define the cell format to be custom 'yyyy-mm-dd' then apply data validation with a defined date range Excel will auto format it for you even if they enter dd/mm/yyyy and alert if an invalid entry is added.
Then for a belt and braces approach run a routine at Save that will mimic what your database will do and alert if there is still invalid data see https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.beforesave
I would also lock down the sheet to avoid users adding rows / columns or moving your cells around.
enter image description here
Related
Think I might be reaching a bit here, but I’m trying to find a way of sending / submitting cell values in excel to other cells. Here’s the example;
In a cell I have a car name
I’ve set my workbook up to use this cell as a field to enter data and the system calculates where what I enter into a separate table, and returns a confirmation that the car I want to look for exists. I might then delete the data in the field to repeat the search. Here’s what I’m trying to solve…
If the car doesn’t exist, I want to somehow send or copy the data from the field (by clicking something or a formula) and send it into another table automatically to list as “cars that were searched for but didn’t exist” type area, how could I do this?
Many thanks
Background
When the Pivot table seems to have a custom NumberFormat, the created fields will not carry it.
OffTopic: I guess it is normal, sometimes it displays 2, 3 or 5 digits among the same dataset, this is for convience for the user (thus making each cell in the column likely to have a number format different from each), I am not really able to change it, there is another ruling that basically formats cell by cell on the source data (I know it's not ideal, but it is what is convinent for the user and avoids overflooding the workbook with conditional formattings).
I would like to keep that number formatting on the value field when the user adds it to the PT by dragging that field to the Value field.
Problem
I have not found the right event to detect when the field is being added to the Pivot Table, I have tried all the events related to PivotTables in the worksheet, but even by reading to their inputs, I can see that such event may not exist.
Question
Is there an event that detects the field adding?
Further thoughts
I am not quite sure if it exists, my alternative would be to create a button where the user would get the format from the original source by clicking it by using the same code that I use to format cell by cell, but I would like to this be ran automatically when the Value add happens.
I am creating a user form for work where the user can select up to 15 clients from a list using excel data validation. However, I only want the second cell to be available to populate once the first has been populated, and the third when the second is populated etc. I also want the color of the cell to change when it becomes available to populate
I've tried using regular excel data validation and conditional formatting, but it seems limited in that I would have to create a separate data validation for each cell, rather than making one data validation command for the entire range
I would think using an if(IsEmpty) would solve my problem but as a fairly new VBA user I am having trouble determining the most efficient way to incorporate the list data validation into the general entry restriction.
I'm trying to implement data validation in Excel using VBA. I'm able to implement data validation before save excel event but I'm having trouble in finding range, where user gave the input in excel.
For example, I want to implement data validation from A1:C50 but if user enters data from A1:C45, remaining cells will fail due to validations. SO I want to find range A1:C45 and do validations.
Can you please let me know how can I find user data range in Excel?
If I am understanding your question correctly you only want test the data if the row above it has been filled in. If so, you can do this without VBA. In your data validation select 'Custom' and enter the formula below (example for cell A46):
=AND(A46>0, A46<=10, COUNTBLANK(A45)=0)
This will check for a value between one and ten but only if there is an entry in the row above.
If this doesn't help please provide more information around your question.
I am building a multilevel dependent dropdown structure in Excel based on a parent-child structure. Building this with vba is not a problem but the problem is reopening the file. It removes the validation because they have too many characters (more than 255?)
The generated validation strings are too long for Excel to store so they are removed after reopening.
A solution could be to write the validation string in a cell and base the validation on this cell value. Only problem is when i link the data validation to this cell its only one option.
the value in the cell is something like A,B,C,D (already tried with or without "")
Does anyone have a suggestion for an excel formula to use in the data validation to generate multiple options from a string like A,B,C,D located in one single cell.
TNX for the support.
The solution was a bit more difficult.
I was able to make it work but only by combining VBA and Excel Formulas. Also i added a value to the dataSource with both the id and optionid in it.
Excel:
For The data validation i use an fuction who utilizes an offset which is dynamic and based on 2 values. This way i dont get the error that a validation is more than 255 characters.
data validation formula:
=OFFSET(List!$A$2;MATCH(CONCATENATE(V18;"|";E18);List!$F:$F;0)-2;1;COUNTIF(List!$F:$F;CONCATENATE(V18;"|";E18)))
One is for the id and one is the optionid.
VBA:
I now use the on change event to produce the values needed for the dropdowns(data validation) but not letting vba insert the values for the data validation. It gets these values via a pre-loaded array which is started with a workbook_open event. (if this isnt loaded it will load it again).
When i select an item it writes the id and optionid to a different cell. I then use these to produce the next dropdowns etc with the offset function. (one dropdown can result in multiple rows of new dropdowns)
Also added some functionality for delete and change (in the middle) events.
Also the onChange event now triggers the next dropdown and write its value if there is only one option. This will of course trigger the next.