I have a script in Excel that requires some vba code. I am a relative newbie and have come to a standstill trying to find a solution.
Say, for example, the user enters a numerical value in cell A1.
Then they must also enter values ββin cells A5, A6, A7 and A8.
The total amount of this amount is displayed in cell A9 using the generic SUM excel function.
None of the cells A5: A8 can be left empty, although entering zero ('0') is permissible.
The value of A9 can be less, equal, but not exceed the value in A1.
If A9 exceeds A1, an error message should appear to warn them that it is.
Alpha characters cannot be entered. An error message will appear to warn them, if any.
Entering numbers must be in the range from 0 to 9999.999. An error message will appear to warn them if they do not.
I was given a piece of vba code (below) that I use for a similar purpose, which works very well. However, I cannot figure out how to include code that will identify and return an error message if the value in A9 exceeds A1. This is what I tried to do, but I know that it is wrong! The code is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
If Not Intersect(Target, Range("A9")) Is Nothing Then
Application.EnableEvents = False
For Each c In Intersect(Target, Range("A9"))
If IsEmpty(c) Then
Application.EnableEvents = True
Exit Sub
End If
If Not VarType(c.Value2) = vbDouble Or c.Value < 0 Or c.Value > 9999999 Then
MsgBox "Entry in cell " & c.Address(0, 0) & " must be a number from 0 and 9,999,999"
Application.Undo
ElseIf WorksheetFunction.Count(Range("A9")) = 1 And _
WorksheetFunction.Sum(Range("A9")) > Range("A1").Value Then
MsgBox "The sum of A99 cannot exceed A1 when all entries are completed"
Application.Undo
End If
If anyone could help me with this, he would really appreciate it!
Feces