I am trying to achieve the following:
The user is shown an excel spread sheet with a list of assumption which they can change.
Title | Value |
Input01 | 10 | =
Input02 | 2 | >=
Input03 | 800 | >=
Input04 | 4 | >=
Input05 | 2 | <=
There is an If .. Then Statement that pulls in data if the assumption are met. However if an assumption is blanc, it should not be included in the If .. Then Statement.
If x = Input01Value And y >= Input02Value _
And z >= Input03Value And a >= Input04Value _
And b <= Input05Value Then
User ommits Input03
If x = Input01Value And y >= Input02Value _
And a >= Input04Value And b <= Input05Value Then
Now I could check to see if each value exist, and then follow it by another If statement with the appropriate variables. But this seems a bit redundant.
I was wondering if something like the following is possible:
Input 01 = ""
If Input01Value != "" Then Input01 = "x = " & Input01Value
'Then use join or something similar to join all of them ..
And Then use this Input01 directly in the If .. Then statement. This way when a variable is empty the And .. are not included and the If statement will not fail.
Eg. (I know this doesn't work, just illustrating the scenario)
VBA: If Input01 Then
Result while compiling: If x = Input01Value Then
Please Note, I know I could do something like the following:
If Boolean And Variable2 > 4 Then and then have Boolean and Variable2 populate with a value in the cell, however the issue with this is that if the user, for example, decides to omit the Variable2 (which is reasonable) it will fail. eg. If (Boolean = True) And > 4 Then.
Hope my question is clear, thanks for the help.
What about using a function which operates on a select case depending on a string operator and two values?
Function conditionalString(condition As String, x As Variant, y As Variant) As Boolean
Select Case condition
Case "="
If (x = y) Then
conditionalString = True
Else
conditionalString = False
End If
Exit Function
Case ">="
conditionalString = (x >= y)
Exit Function
Case "<="
conditionalString = (x <= y)
Exit Function
Case ">"
conditionalString = (x > y)
Exit Function
Case "<"
conditionalString = (x < y)
Exit Function
Case Else
conditionalString = False
End Select
End Function
You could then just have another function, say "check if value isn't blank" before calling all assumptions.
Expanding on my comment, you can use something like this to test each row of input.
Function TestIt(testValue,inputOperator,inputValue) As Boolean
If Len(inputValue)=0 Then
TestIt=True 'ignore this test: no value supplied
Else
TestIt=Application.Evaluate(testValue & inputOperator & inputValue)
End If
End function
Related
I am trying to figure out another way to write this line. Currently, I have it to where if any of the ranges AA2:AA7 = 1 then call code OneLineItem. Issue is, the parameter I need set is if only one of those cells equals 1 and only one other cell to be greater than 1. I.e. AA2 = 1 and AA7=200 (for example). A problem i'm running into is that AA2 = 1, AA3 = 100, AA7 = 200. However I just need one cell to equal 1 and another cell to be >1 and everything else to be 0. If that criteria is met, then call code OneLineItem. Thank You.
If ActiveSheet.Range("AA2") = 1 Or ActiveSheet.Range("AA3") = 1 Or
ActiveSheet.Range("AA4") = 1 Or ActiveSheet.Range("AA5") = 1 Or
ActiveSheet.Range("AA6") = 1 Or _
ActiveSheet.Range("AA7") = 1 Then
Call OneLineItem
Else
There are 6 numbers so:
1 should be 1
1 should be greater than 1
4 should be 0
so we can use COUNTIF() to find if it follows the pattern
Dim OneTrue As Boolean
Dim MoreTrue As Boolean
Dim RestTrue As Boolean
RestTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 0) = 4 [AA2:AA7].Cells.Count - 2
OneTrue = Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1
MoreTrue = Application.WorksheetFunction.CountIf([AA2:AA7], ">1") = 1
If RestTrue And OneTrue And MoreTrue Then
Call OneLineItem
End If
Another method would be to nest the IF:
IF Application.WorksheetFunction.CountIf([AA2:AA7], 0) = [AA2:AA7].Cells.Count - 2 Then
IF Application.WorksheetFunction.CountIf([AA2:AA7], 1) = 1 Then
'we do not need the third, If the others are true then the last must be true.
'Unless you can have negative numbers. Then you can add the third.
Call OneLineItem
End If
End If
The advantage to the second is that it only does the COUNTIFs necessary till it find a False return, then it does not do any more. while the first does all three no matter what.
I don't know why this code doesn't work ..i wanna just convert text to number .. It doesn't give me any error but it doesn't work
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Select Case chain
Case "1 - Très difficile"
ConvertCOMPLEXITYTToNumber = 1
Case "2 - Difficile"
ConvertCOMPLEXITYTToNumber = 2
Case "3 - Modérée"
ConvertCOMPLEXITYTToNumber = 3
Case "4 - Facile"
ConvertCOMPLEXITYTToNumber = 4
Case Else
ConvertCOMPLEXITYTToNumber = 0
End Select
Exit Function
End Function
That may be because you may have unwanted leading or trailing spaces which fails the comparison. Also you do not need Exit Function at the end of the code. It will exit any ways :)
Try this
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
Dim Num As Integer
Select Case Trim(chain)
Case "1 - Très difficile": Num = 1
Case "2 - Difficile": Num = 2
Case "3 - Modérée": Num = 3
Case "4 - Facile": Num = 4
Case Else: Num = 0
End Select
ConvertCOMPLEXITYToNumber = Num
End Function
If IsNumeric(Left(Trim(chain),1)) Then
ConvertCOMPLEXITYToNumber = Left(Trim(chain),1)
Else
ConvertCOMPLEXITYToNumber = 0
End If
Here, my approach for you:
Public Function ConvertCOMPLEXITYToNumber(ByVal chain As String) As Integer
'Get the first character
chain = Left(Trim(chain), 1)
'If frist character is numeric
If IsNumeric(chain) Then
'If first number is less than 5, return value
If chain < 5 Then
ConvertCOMPLEXITYToNumber = CInt(chain)
End If
End If
End Function
Using ASPPDF, I am creating a pdf form from user input.
When the user selects a radio option, I am able to set where the data will write within the PDF like this.
If Request("type") = 1 Then x=57
If Request("type") = 1 Then y=506 else
If Request("type") = 2 Then x=57
If Request("type") = 2 Then y=400 else
Page1.Canvas.SetParams "color=black, linewidth=2"
Page1.Canvas.DrawLine x, y, x + 7, y - 7
Page1.Canvas.DrawLine x, y - 7, x + 7, y
This generates an X mark in an appropriate box in my PDF.
My problem is that the value of those fields needs to be a string, not a number. When I try this, I do not receive any errors but it also doesn't write anything.
If Request("type") = AP Then x=57
If Request("type") = AP Then y=506 else
If Request("type") = AR Then x=57
If Request("type") = AR Then y=400 else
Page1.Canvas.SetParams "color=black, linewidth=2"
Page1.Canvas.DrawLine x, y, x + 7, y - 7
Page1.Canvas.DrawLine x, y - 7, x + 7, y
I am not able to simply change the values within the form to numbers as those same values are used in multiple places throughout the script and I need it as that value, not a number.
I've also tried adding " " (quotes) around the value, but that doesn't work either.
...
If Request("type") = "AP" Then x=57
...
Any help out there?
Wrong structured if .. then .. else statement. Right syntax is as follows:
' Single-Line syntax:
If condition Then statements [Else elsestatements ]
' Or, you can use the block form syntax:
If condition Then
[statements]
[ElseIf condition-n Then
[elseifstatements]] . . .
[Else
[elsestatements]]
End If
Hence, your code snipped could be as follows:
If UCase(Request("type")) = "AP" Then
x=57
y=506
ElseIf UCase(Request("type")) = "AR" Then
x=57
y=400
Else
'
End If
Or
Select Case UCase(Request("type"))
Case "AP"
x=57
y=506
Case "AR"
x=57
y=400
Case Else
'
End Select
Note: UCase function returns a string that has been converted to uppercase, as we could not know which letter case the Request("type") is (e.g. ap, aP, Ap or AP?).
Resource: VBScript Language Reference
I am still new to vba.
I am trying to create a new function however, it keeps giving me an output that I am not expecting.
my code is as follows:
Function bonusplanA(a, b, c)
Dim example As Range
Set example = Range("a:c")
Value = Application.WorksheetFunction.CountIf(example, ">=90")
Value1 = Application.WorksheetFunction.CountIf(example, ">=80")
If Value = 3 Then
bonusplanA = "$20,000 bonus"
Else
If Value1 = 3 Then
bonusplanA = "$10,000 bonus"
Else
bonusplanA = "NO BONUS"
End If
End If
End Function
You need to define your function like this:
Function bonusplanA(a, b, c)
If a >= 90 And b >= 90 And c >= 90 Then
bonusplanA = "$20,000 bonus"
Else
If a >= 80 And b >= 80 And c >= 80 Then
bonusplanA = "$10,000 bonus"
Else
bonusplanA = "NO BONUS"
End If
End If
End Function
The problem in your example was that Range("a:c") does not make a range of your a,b,c variables; instead it selects the range composed of columns A, B and C.
You need to use parameters a, b and c directly, not through the Range function.
Otherwise, the logic was sound. :)
I may be asking a silly question but I am self teaching myself VBA and I am just stumped and I am not even sure what terms I can use to look up a solution.
I am writing a code that will compare three variables to three other variables then I want to display which variables have changed.
So if x = a but y <> b and z <> c then the output should be b/c
I have worked out a code that works fine
Dim Str As String
If X <> A Then
If Y <> B Then
If Z <> C Then
Str = "a/b/c"
Else
Str = "a/b"
End If
ElseIf Z <> C Then
Str = "a/c"
Else
Str = "a"
End If
ElseIf Y <> B Then
If Z <> C Then
Str = "b/c"
Else
Str = "b"
End If
Else
Str = "c"
End If
But as I increase the number of variables this becomes extremely complex very quickly.
If anyone can help direct me to a simpler method without the exponential complexity I would be very grateful.
Thank you all so much!
You need to test each variable pair independently from each other -- not link them together in one giant If construct tree.
Example:
str = "" 'Start with blank string. Append as required.
If x <> a Then str = str & "a/"
If y <> b Then str = str & "b/"
If z <> c Then str = str & "c/"
'Remove the extra / at the end
If Right(str, 1) = "/" Then str = Left(str, Len(str - 1))
You could put the 2 strings in 2 arrays, and then use a FOR...NEXT construct to loop both arrays. You can use UBound(arValues) to dynamically find out the number of items in the array.
Good luck