Excel VBA: Larger than behaves as larger or equal - excel

I use the follow code in Excel VBA:
If ((B - A) > 180) Then
result = 1
ElseIf ((B - A) < -180) Then
result = 2
Else
result = 0
End If
In case (B - A) = 180, VBA returns result = 1.
The debugger shows the first statement of the If as True even if (B - A) = 180
Is it VBA acting funny or more probably me missing something?
Any help will be much appreciated.

Related

Visual Basic Got 424 Object Required Error

Hi I've tried to run this code, but I have received 424 error, I'm new to Visual Basic and I don't know how to fix this bug :( Thanks so much
Dim a, b As Integer
'compute weekly change BAS
For a = 1 To 8
For b = 1 To 9
If Range("AA4").Cells(a, b) <> 0 Then
Changed.Cells(a, b) = (Range("AB4").Cells(a, b) - Range("AA4").Cells(a, b)) / Range("AA4").Cells(a, b)
Else
Changed.Cells(a, b) = "n/a"
End If
Next b
Next a
If Changed is a Worksheet Object, you need to Dim it and Set it.

EXCEL Evaluate function inconsistent result

I have one data worksheet which is as per below format (100+ cols)
then I have another file containing some rules (multiple rules for each column in data file). I formulated vba conditions for every rule; for example, one rule was to check column A of data file should have only BP or Trip value for all rows (50K+ rows). So I translated that to below VBA
DataWB.Worksheets(1).Cells(J,X) = "BP" OR DataWB.Worksheets(1).Cells(J,X) = "Trip"
validate button code
f = Trim(ThisWorkbook.Worksheets(1).Cells(2, 3))
f = Replace(f, "J", 2)
f = Replace(f, "X", 1)
Debug.Print Application.Evaluate(f)
now problem is that evaluate throws type mismatch and if I use CBOOL it always return TRUE. I tried different variations too (like adding = or ? prefix) but nothing seem working. Any help will be highly appreciated.
Evaluate cant understand VBA Code rather excel worksheet formula can only be used, so i replaced with formula as in picture and it works perfectly now
For J = 2 To lRow
For x = 2 To 26
f = Trim(ThisWorkbook.Worksheets(1).Cells(x, 3))
f = Replace(f, "WB", SourceWB.Name)
f = Replace(f, "WS", SourceWB.Worksheets(1).Name)
f = Replace(f, "R#", J)
test = Evaluate(f)
If Not test Then Call LogError(J, x)
Next x
Next J

How to count the recursion times in Haskell?

I am trying to write a function, given two dates, calculates the distance (in days) between the two dates, using recursion. The function dateDistance takes two dates and uses nextDate to find the next valid date. goodDate makes sure its a valid date.
I am trying to get the result by counting recursions and returning the count. Each time the recursion happens the count variable n should increase and at the end of the recursion when it reaches the end condition (fy == sy && fm == sm && sd == fd) = n it shall return the n.
leapYear x = if ((x `mod` 4) == 0) && ((x `mod` 100) /= 0) || ((x `mod` 400) == 0)
then True
else False
goodDate (y,m,d)
| (d<1 || m>12 || m<1 || y==0) = False
| m `elem` [1,3,5,7,8,10,12] = d<32
| m `elem` [4,6,9,11] = d<31
| leapYear y = d<30
| otherwise= d<29
nextDate (y,m,d) =
if goodDate(y,m,(d+1))
then (y,m,(d+1))
else if goodDate(y,(m+1),1)
then (y,(m+1),1)
else ((y+1),1,1)
dateDistance (fy,fm,fd) (sy,sm,sd)
|(fy == sy && fm == sm && sd == fd) = n
|otherwise = dateDistance nextDate (fy,fm,fd) (sy,sm,sd)
where n = 0
Actually, you don't need to remember it, even though you could do so, by passing it from call to call as an argument.
Let's answer two questions:
What is the base case of your recursion?
Exactly, when both dates are equal; the distance is 0 then.
What is one recursion step?
Well, if the dates f and s are not equal, we search for the nextDate (rather next-day!), lets call it f1. f1 is one day distant from f, but one day nearer to s. f2 ( = nextDate f1) is two days from f, but even nearer to s. So: dateDistance f s = 1 + dateDistance f1 s and dateDistance f1 s = 1 + dateDistance f2 s.
You need to pass the "n" explicitly through the recursive calls. Make it an extra parameter of your dateDistance function and put n+1 in the recursive call. Then call it with an initial value of 0.
You can rename the dateDistance function to something else and rewrite dateDistance to call it with the initial value of 0.

VBA - Check whether a parameter is set using the bit key

i have a dialog with a few parameters (arround 20).
Every parameter gets a value like 1,2,4,8,16,32 and so on.
By closing dialog an integer will be set with the sum of the parameter like 2+8+16+64.
Now i have a few options to run my program.
For example:
The first option needs to run the parameter 2,8 and 16 so i need to check if 2,8 and 16 will be checked in my integer.
I know that there is a way but not really how.
Maybe u can help.
Thanks
Jassin
The first option need
Apply the And operator on a mask (m, interesting bits set) and your data (d, e). If the result is equal to m, then all the bits set in m are set in the data.
>> d = 4 + 2 + 1
>> e = 4 + 1
>> m = 4 + 2 + 1
>> WScript.Echo 0, CStr(m = (d And m))
>> WScript.Echo 1, CStr(m = (e And m))
>>
0 True
1 False

Using a combination of IF, And in excel VBA

Im trying make a condition in excel VBA where there are two posible condition of variable x and y,
Say for example
Sub Test()
X = 6
Y = 11
If X < 3 Or X > 5 And Y < 10 Then
X = 10
Else
X = 11
End If
MsgBox X
End Sub
For the X term, when X<3 or X>5 seems work well however when i change the Y to any value greater than 10 say 11 then the result is 10 but supposed to be it should be 11. Can you please let me know if i missing something in my code so that when X<3 or X>5 and (y=11)>10 the result must be 11.
Regards,
Kenneth
This is an issue of operator precedence. In VB the order in Logical and Bitwise Operators is as follows:
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor)
The result is that and is executed before or in your condition, leading to the right side of the or being the whole expression X > 5 And Y < 10.
Your condition is executed like:
If X < 3 Or (X > 5 And Y < 10) Then
I believe what you actually want is the following (this should solve your issue):
If (X < 3 Or X > 5) And Y < 10 Then
Sources:
MSDN: Operator Precedence in Visual Basic
Consider:
Sub Test()
X = 6
Y = 11
If (X < 3 Or X > 5) And Y < 10 Then
X = 10
Else
X = 11
End If
MsgBox X
End Sub

Resources