COGNOS - Days Between Excluding Weekend - cognos

I Would Like To Count The Number Of Days Between Start Day [Start] and End Date [Finish];
My Current Code Works Fine, But Includes Weekends,
_days_between (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),cast([Start] as date)) + 1
Edit (Based On Alexey Baturin Answer):
1 + 5 * (_days_between (cast([Finish] as date);cast([Start] as date))
-_day_of_week (cast([Finish] as date);1)+_day_of_week (cast([Start] as date);1))/7
- if (_day_of_week (cast([Start] as date);1) > 5) then (6) else (_day_of_week (cast([Start] as date);1))
+ if (_day_of_week (cast([Finish] as date);1) > 5) then (5) else (_day_of_week (cast([Finish] as date);1))
I Now Have This Code But This Doesn't Include If [Finish] Is Missing.

Try
1 + (_week_of_year (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date))) - _week_of_year (cast([Start] as date))) * 5
- if (_day_of_week (cast([Start] as date),1) > 5) then (6) else (_day_of_week (cast([Start] as date),1))
+ if (_day_of_week (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),1) > 5) then (5) else (_day_of_week (IF ([Finish] is missing) THEN (current_date) ELSE (cast([Finish] as date)),1))
Or simpler, to make the idea of calculation clear.
1 + (_week_of_year ([Finish]) - _week_of_year ([Start])) * 5
- if (_day_of_week ([Start];1) > 5) then (6) else (_day_of_week ([Start];1))
+ if (_day_of_week ([Finish];1) > 5) then (5) else (_day_of_week ([Finish];1))
End of year bug fixed
1 + 5 * (_days_between ([Finish];[Start])
-_day_of_week ([Finish];1)+_day_of_week ([Start];1))/7
- if (_day_of_week ([Start];1) > 5) then (6) else (_day_of_week ([Start];1))
+ if (_day_of_week ([Finish];1) > 5) then (5) else (_day_of_week ([Finish];1))

Related

Animating movement of a worksheet Label

I'm trying to make a label move from one cell to another. I figured out the position offsets and created 3 "tweens" as animators call them. But when I run the code, the Label doesn't display until the sub has ended. If I comment out the last position the label is in the right spot. How do I make Excel update the screen to show the label before I move it again?
xo and yo are integers either -1, 0, or 1. playerLocation is a range, might as well be ActiveCell.
Sub moveAnimation()
Dim moveAnim As OLEObject
Set moveAnim = Worksheets(currentSheet).OLEObjects(currentSheet & "player")
moveAnim.Left = playerLocation.Left - 0.5 + yo * (playerLocation.Width / 4)
moveAnim.Top = playerLocation.Top + 0.25 + xo * (playerLocation.Width / 4)
Sleep (25)
moveAnim.Left = playerLocation.Left - 0.5 + yo * (playerLocation.Width / 2)
moveAnim.Top = playerLocation.Top + 0.25 + xo * (playerLocation.Width / 2)
Sleep (25)
moveAnim.Left = playerLocation.Left - 0.5 + yo * 3 * (playerLocation.Width / 4)
moveAnim.Top = playerLocation.Top + 0.25 + xo * 3 * (playerLocation.Width / 4)
Sleep (25)
moveAnim.Left = 0
moveAnim.Top = 0
I've figured it out. ActiveSheet.Calculate will force an update so i put it between each position

How to update an old vba code to work on excel?

I have this code from an old book that does not work in my Excel. It is supposed to calculate the slope and intercept of a Deming regression from a range of numbers, x and y
Function Deming(XValues, YValues)
Dim MeanX(), MeanY()
Ncells = XValues.Count
ReDim MeanX(Ncells / 2), MeanY(Ncells / 2)
For x = 2 To Ncells Step 2
MeanX(x / 2) = (XValues(x - 1) + XValues(x)) / 2
MeanY(x / 2) = (YValues(x - 1) + YValues(x)) / 2
SumX = SumX + MeanX(x / 2): SumY = SumY + MeanY(x / 2)
SumX2 = SumX2 + (MeanX(x / 2)) ^ 2
SumY2 = SumY2 + (MeanY(x / 2)) ^ 2
SumXY = SumXY + MeanX(x / 2) * MeanY(x / 2)
SumDeltaX2 = SumDeltaX2 + (XValues(x - 1) - XValues(x)) ^ 2
SumDeltaY2 = SumDeltaY2 + (YValues(x - 1) - YValues(x)) ^ 2
Next
XBar = SumX / N: YBar = SumY / N
Sx2 = (N * SumX2 - SumX ^ 2) / (N * (N - 1))
Sy2 = (N * SumY2 - SumY ^ 2) / (N * (N - 1))
Sdx2 = SumDeltaX2 / (2 * N)
Sdy2 = SumDeltaY2 / (2 * N)
rPearson = (N * SumXY - SumX * SumY) / Sqr((N * SumX2 - SumX ^ 2) * (N *
SumY2 - SumY ^ 2))
lambda = Sdx2 / Sdy2
U = (Sy2 - Sx2 / lambda) / (2 * rPearson * Sqr(Sx2) * Sqr(Sy2))
Slope = U + Sqr(U ^ 2 + 1 / lambda)
Intercept = YBar - Slope * XBar
Deming = Array(Slope, Intercept)
End Function
Does this have a bad syntax or not?
First this is not old code, this is simply bad code.
Anything in VBA, which does not compile, when someone writes Option Explicit on the top of the Module/Worksheet is a bad syntax. This is a rule of a thumb. And in the case of the code, if this one is pasted to the editor the following line is red:
rPearson = (N * SumXY - SumX * SumY) / Sqr((N * SumX2 - SumX ^ 2) * (N *
SumY2 - SumY ^ 2))
This is because it should be in 1 line, and not in 2.
So, concerning the question - how to update it - as a first step, make sure the code compiles with Option Explicit on top (Option Explicit statement). So, write Option Explicit and then go to Debug>Compile on the VBEditor's ribbon. VBeditor will highlight the problem. The first one is that Ncell is not defined:
Then find a way to define it, e.g. write Dim Ncells as Variant or as anything else you may consider useful on the top of the highligted line. It could be that just declaring a variable is not enough, as there is a calculation XBar = SumX / N in the code. There, N should be declared and assigned to a value. If it is only declared, it will be 0, and then a division by 0 will be an error. Thus, probably something like this should be written, depending on the logic: Dim N as Double: N = 1

What does a second equals = within vba variable assignment do?

Perplexed by the function of using a second = sign in vba. eg. s = Int(xVal) + (xVal = n + 1)
I had been deciphering some code and came across the following line which is perplexing me somewhat and despite some extensive research and debugging I seem to be struggling to find the answer:
s = Int(xVal) + (xVal = n + 1)
and
p(i, 3) = A(i)(s + 3 + (s = n)) + (s = n) * (p(i, 1) - p(i, 2))
My question is what is the function of the comparisons within the parentheses after the first assignment = sign?
TIA
(s = n)
If both s and n have the same value then this evaluates to True, which can be coerced to its underlying value of -1 by other arithmetic operations.
Eg:
? True * 1 '>> -1
? False * 1 '>> 0
So this:
s = Int(xVal) + (xVal = n + 1)
is like writing:
If xVal = n + 1 Then
s = Int(xVal) + -1
else
s = Int(xVal) + 0
end if
or:
s = Int(xVal) + IIf(xVal = n + 1, -1, 0)

Absolute Value of the Differences In Two (2) Ranges - PART II

Hopefully this is easier to read than yesterday. Trying to find a way to vary the number of periods "N" that measure "VOLATILITY" The code for the complete function as suggested yesterday is below and fixes "N" at 10. Function works just fine for KAMA with the default value for "N" (N0Addr and N1Addr are not needed in this default version of the KAMA function but are steps to get to a variable "N")
This formula works in Excel:
=SUMPRODUCT((ABS(I26:I36-I25:I35)))
I can also obtain the correct sum of differences in the two ranges but not the absolute value. This VBA Code does that with the named ranges "N0Addr" and "N1Addr":
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
Function nTEST(Price, nPer, mPer, N)
'Variables
Fast = 2 / (nPer + 1)
Slow = 2 / (mPer + 1)
'One(1) Prior Period Calculation
nTEST1 = Application.Caller.Offset(-1)
N0Addr = Application.WorksheetFunction.Concat(Price.Offset(-N, 0).Address & ":" & Price.Address)
N1Addr = Application.WorksheetFunction.Concat(Price.Offset(-(N + 1), 0).Address & ":" & (Price.Offset(-1, 0).Address))
'Change Formula (Y - Yn)
E = Abs(Price - Price.Offset(-N, 0))
'Volatility Formula { =SUM(ABS(Y:Yn)-(Y1:Yn1))) }
'VOLATILITY (N = 10)
'1-10
R = Abs(Price - Price.Offset(-1, 0)) + Abs(Price.Offset(-1, 0) - Price.Offset(-2, 0)) _
+ Abs(Price.Offset(-2, 0) - Price.Offset(-3, 0)) _
+ Abs(Price.Offset(-3, 0) - Price.Offset(-4, 0)) + Abs(Price.Offset(-4, 0) - Price.Offset(-5, 0)) _
+ Abs(Price.Offset(-5, 0) - Price.Offset(-6, 0)) + Abs(Price.Offset(-6, 0) - Price.Offset(-7, 0)) _
+ Abs(Price.Offset(-7, 0) - Price.Offset(-8, 0)) + Abs(Price.Offset(-8, 0) - Price.Offset(-9, 0)) _
+ Abs(Price.Offset(-9, 0) - Price.Offset(-10, 0))
'EFFICIENCY RATIO
ER = E / R
Smooth = (ER * (Fast - Slow) + Slow) ^ 2
'Formula Calculation
nKAMA = Smooth * Price + (1 - Smooth) * nKAMA1
End Function
Looking for an VBA formula or method to input a working formula for "volatility" that can vary over N periods. I can get the sum of differences but not the sum of absolute differences.
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
I can also enter one formula in Excel that accomplishes provides the sum of absolute differences.
=SUMPRODUCT((ABS(I26:I36-I25:I35)))

WorksheetFunction.Countifs 13 Type Mismatch

I am working on a complex Excel VBA application and trying to use Countifs function. The problem is it is giving me a Type mismatch error I am unable to point a finger to. It would be too complex to paste the entire code here but I am pasting what I think are the relevant lines of the code
Public NumSecTypes As Integer
Public NumSecurities As Integer
.
.
.
Dim i as Integer
Dim AvgPoSSizeCountif As Double
Dim SecType As String
.
.
.
Sheets("Output").Select
AvgPoSSizeCountif = WorksheetFunction.CountIfs(Range(Cells(i + 1, (17 + 2 * NumSecTypes)), Cells(i + 1, (16 + 2 * NumSecTypes + NumSecurities))), Range(Cells(6, (17 + 2 * NumSecTypes)), Cells(6, (16 + 2 * NumSecTypes + NumSecurities))), SecType, Range(Cells(i + 1, (21 + 2 * NumSecTypes + 2 * NumSecurities)), Cells(i + 1, (20 + 2 * NumSecTypes + 3 * NumSecurities))), ">0")
Can anyone tell me what is wrong with this? The error is in the "AvgPoSSizeCountif = ..." line
Many thanks in advance. Any help would be appreciated. Kindly let me know if you need any other information
The syntax for the CountIfs function should be
CountIfs( criteria_range1, criteria1, [criteria_range2, criteria2, ...])
It looks like you are missing the criteria that matches Parameter 1. This is what your function looks like below:
WorksheetFunction.CountIfs(
// Parameter 1
Range(Cells(i + 1, (17 + 2 * NumSecTypes)), Cells(i + 1, (16 + 2 * NumSecTypes + NumSecurities))),
// Parameter 2
Range(Cells(6, (17 + 2 * NumSecTypes)), Cells(6, (16 + 2 * NumSecTypes + NumSecurities))),
// Parameter 3
SecType,
// Parameter 4
Range(Cells(i + 1, (21 + 2 * NumSecTypes + 2 * NumSecurities)), Cells(i + 1, (20 + 2 * NumSecTypes + 3 * NumSecurities))),
// Parameter 5
">0"
)

Resources