1

Here is my code below:

Dim m As String, n As Long

n = InputBox("Enter sales amount: ")

If n < 500 Or n > 5000 Then
ActiveCell.Value = n
ActiveCell.Interior.Color = RGB(255, 0, 0)
m = InputBox("Reason why increase/decrease? ")
ActiveCell.Offset(0, 1).Value = m
ActiveCell.Offset(1, 0).Select

Else
ActiveCell.Value = n
ActiveCell.Offset(1, 0).Select
End If

I was figuring out how do I validate value if input was string then inputbox will prompt and ask for integer ? Thanks in advance for the answers.

1 Answer 1

2

Code:

Dim m As String, n As String, sales as long
TryAgain:

n = InputBox("Enter sales amount: ")

If Not IsNumeric(n) Then
    MsgBox "Entry should be a number!"
    GoTo TryAgain
End If

sales = CLng(n)

If sales < 500 Or sales > 5000 Then
ActiveCell.Value = sales
ActiveCell.Interior.Color = RGB(255, 0, 0)
m = InputBox("Reason why increase/decrease? ")
ActiveCell.Offset(0, 1).Value = m
ActiveCell.Offset(1, 0).Select

Else
ActiveCell.Value = sales
ActiveCell.Offset(1, 0).Select
End If
Sign up to request clarification or add additional context in comments.

4 Comments

Hi ! What if I want to put validation too in "reason why increase/decrease" ? if integer was an input then gonna ask for string
From a business needs standpoint that will be much more difficult. Would you reject 5, but accept awuifbouiqebv?
Sorry, but does my second question sounds impossible to code? please give me some tips. Thanks in advance !
I don't think that it will be possible to vaidate something that is free text. You'll have to decode what your business rules are for the response and test for all of those. I doubt testing if it's a number will actually satisfy the business requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.