PDA

View Full Version : VBA - CELL VALUES EXCEED 12 (MONTHS)



stevembe
02-25-2013, 10:26 AM
I have two cells that you have to input the number of months into which needs to add up to 12 which is obviously the number of months in a year. Is there a way that I can embed some VBA so that if somebody puts in a figure in cell A1 of 8 and in A2 of 5 (=13) it pops up a message to say "The number of months you have added does not add up to 12". Same goes for if they were to put in two values of 10 and 1 (=11).

Many thanks in advance for anyone who can assist.

Kenneth Hobs
02-25-2013, 01:00 PM
Use Data Validation.

p45cal
02-25-2013, 01:33 PM
Data Validation, custom formula for both cells:
=OR(SUM($A$1:$A$2)=12,COUNTBLANK($A$1:$A$2)>0)
and add a message in the Errr Alert tab to the effect of: "Cells A1 and A2 should add up to 12"

But perhaps a formula in A2 instead:
=12-A1
?

Or some vba could be written to enter the right value in the other cell when someone adjusts one of these cells.

stevembe
02-26-2013, 02:03 AM
Data validation is a better idea, thank you but the formula does not seem to work when I paste it in both cells, it returns an error

p45cal
02-26-2013, 02:16 AM
I tested this in Excel 2003, and copy/pasted the cells' data validation formula here. It worked. What was the error?
You're not pasting the formula directly into the cell are you?