Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple conditions in an IF statement in Excel VBA

Probably an easy one but my prog skills are limited. I've created an account entry tool and want to warn the user if they've entered a credit amount for an Expenditure type of which there are two types; "Restricted_Expenditure" and "Unrestricted Expenditure". Typically values of this type should be debits.

I can get it to work for one type in the statement, but not if I add the other expenditure type in an "or" statement.

Any help appreciated - is there an "Or If..." type function I could use?

My code is

If inputWks.Range("d9") > 0 And inputWks.Range("d11") = "Restricted_Expenditure" Or "Unrestricted_Expenditure" Then

Dim Response As Integer

 Response = MsgBox(prompt:="You've entered a credit amount against an expenditure type.  If this is correct then press 'Yes' or else press 'No' to change", Buttons:=vbYesNo)

If Response = vbYes Then

GoTo Line1

Else

Exit Sub

End If
like image 523
Mark Tennant Avatar asked Jan 19 '16 19:01

Mark Tennant


People also ask

How do you add multiple conditions in if Excel?

Another way to get an Excel IF to test multiple conditions is by using an array formula. To complete an array formula correctly, press the Ctrl + Shift + Enter keys together. In Excel 365 and Excel 2021, this also works as a regular formula due to support for dynamic arrays.

Can you do a nested if in VBA?

In VBA, you can use one IF statement inside another IF statement to create nested IFs. In simple words, you can execute one test statement using IF based on the result of another IF statement. In this kind of conditional statement, you need to test complex conditions.

How do I write a conditional statement in Excel VBA?

Conditional Statements are the If ....Then statement, with the Else or ElseIf, and the Select case statement. The form is: [expression] [relational operator] [expression] where in 1 condition you have the Conditional operators and in 2 or more the Logical operators.

How many else if can I use in VBA?

Else statements can be nested to as many levels as you need.


1 Answers

In VBA we can not use if jj = 5 or 6 then we must use if jj = 5 or jj = 6 then

maybe this:

If inputWks.Range("d9") > 0 And (inputWks.Range("d11") = "Restricted_Expenditure" Or inputWks.Range("d11") = "Unrestricted_Expenditure") Then
like image 71
Fadi Avatar answered Oct 06 '22 01:10

Fadi