Moving data labels for two consecutive points - vba

I have been trying to find a way to move data labels either above or below points.
Starting at point 1, for every two consecutive points, I'd like to move the data labels above the point, and for the next 2, I'd like to move them below. I'm not sure how to change my for loop or if statement to accommodate this condition. So in the end, the data labels for points 1 and 2 would be positioned above, the data labels for points 3 and 4 would be positioned below, 5 and 6 above...etc
If anyone can help, I'd greatly appreciate it.
Dim FlowIndex As Long
With ActiveChart.SeriesCollection(1)
For FlowIndex = 1 To .Points.Count
With .Points(FlowIndex)
If .HasDataLabel Then
With .DataLabel
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
End If
End With
Next
End With

This is a mathematical question of how to perform your loop. Think of the values 1,2,3,4,5,6,7,8. What mathematical rule would put 1,2,5,6 in the same group, and 3,4,7,8 in another group?
Possible answer: when divided by 4, 1 & 2 round down to 0. 3 & 4 round up to 1. 7&8 round up to 3. So, one group rounds up to an even number, and one group rounds down to an odd number. There are many ways to reflect this principle, but here is one:
IF FlowIndex MOD 4 = 1 OR FlowIndex MOD 4 = 2 then 'There is a remainder of 1 or 2 when divided by 4 - put label above
'Do stuff
Else 'There is a remainder of 3 or 0 when divided by 4 - put label below
'Do other stuff
End If
To clarify on my answer to the last question you had, MOD is a method of dividing which returns the remainder after doing whole number division. So 5 MOD 3 is 2, 9 MOD 3 is 0, etc.

Alternatively:
bLabelAbove = True
For i = 0 to 20 step 2
For j = 1 to 2
If bLabelAbove Then
Series.DataLabels(i + j).Position = xlLabelPositionAbove
Else
Series.DataLabels(i + j).Position = xlLabelPositionBelow
End If
Next
bLabelAbove = Not bLabelAbove
Next

Divide
1, 2, 3, 4, 5, 6, 7, 8
by 2, and round up, to get:
1, 1, 2, 2, 3, 3, 4, 4
Then you can test whether1, 1, 2, 2, 3, 3, 4, 4 is odd or even. If odd, then label above. If even, then label below.

Related

How can I use IF and ELSE IF in looping and display 2 statements in GAMS?

I am a beginner level in this program. I try to improve this loop according to this condition. The details are as follows:
When CUTI(k) = CUTI(k)-4 then,
1)If the result shows this CUTI(k) value greater than 0, then print this CUTI(k) value.
2)If the result shows CUTI(k) value less than 0, then print this CUTI(k) value is added 12 with showing a word "*" after the number in display, e.g. 10*, 9*
I am not sure this loop is correct and enough to add this condition. Look forward to seeing your recoomendation. :)
set k /1*20/;
parameter
CUTI(k)/1 6, 2 2, 3 8, 4 5, 5 1, 6 3, 7 7, 8 8, 9 6, 10 8,11 1, 12 2, 13 4, 14 7,
15 5, 16 2, 17 8, 18 9, 19 2, 20 10/;
loop(k,
if(CUTI(k)-4 > 0,
CUTI(k) = CUTI(k)-4;
else
CUTI(k) = (CUTI(k)-4)+12 ;
)
);
display CUTI;
Your logic looks correct. However, instead of the loop/if/else you could simplify this to one assignment:
CUTI(k) = CUTI(k)-4+12$(CUTI(k)<=4);
However, modifying the display statement by adding a * to some elements is not possible. If you need to distinguish the cases in such a statement, you might assign the values to two different parameters and display them individually.

Explain the difference in this while loop please

Example:
x = 0
while x <= 10:
x += 2
print(x)
Results for this will be 0, 2, 4, 6, 8 , 10
If I switch the postion of print(x) and x += 2. The results will be 2, 4, 6, 8, 10, 12.
Please explain to me the thought process for this.
Thanks
I suppose this is Python. Basically the code will get executed in the order it is written. I suggest you to take a sheet of paper and follow the lines of code, trying to figure out what happens and what is the value of your variable at each step. The output will be the values that your variable had when it got printed with the print function.

Set outcome of formula to working days

I would like to change the outcome of a SQL statement formula to 1, 2, 3, 4 or 5 (these are working days).
Example 1: when I have day 1, minus 2 days the outcome should be 4.
Example 2: when I have day 4, plus 2 days the outcome should be 1.
Example 3: when I have day 5, minus 20 days, the outcome should be 5
At the moment I'm using a table as shown below (I have the input and days-back and the output is what i want to see):
Input, days-back, output:
1 0 1
Input, days-back, output:
1 1 5
Input, days-back, output:
1 2 4
Input, days-back, output:
2 4 3
P.s. I do not have a date, only day numbers.
I hope you understand what I'm looking for :)
If you want to have "days-back" greater than 5 you need to use the following formula:
((Input + ((5*days-back)-1) - days-back) % 5) + 1
How this works - If you look at the prior formula you can see I'm adding 5 to input to make sure we are always positive before I subtract one and the days back. I then mod by 5 and add the one back in so that we go from 1 to 5 instead of 0 to 4
Since I don't know how large days-back is going to be I need something larger but I also need to have it not effect the mod 5 calculation so I just multiply it by 5. I then subtract one (so I can add it later and offset 0 to 4 to 1 to 5) and we are done.
prior answer below
I note I missed the 5 case -- here is the formula that works for that:
((Input + 4 - days-back) % 5) + 1
original answer
You need to use use modulus math. The formula you want is
(Input + 5 - days-back) % 5
Where % means modulus. In SQL Server you can use % in Oracle it is MOD, etc -- it depends on the platform.
For those that care here is my DB2 test code:
WITH TEST_TABLE(input, days_back) AS
(
VALUES
(1,0),
(1,1),
(1,2),
(2,4)
)
SELECT TEST_TABLE.*
MOD(INPUT+4-DAYS_BACK,5)+1
FROM TEST_TABLE

Multiple for loop variable in vba?

I have the following code in VBA:
For n = 1 To 15
Cells(n, 8) = Application.Combin(2 * n, n)
next n
I want the n in the cells(n,8) to have an incerement 2, so the code skips a line after each entry.
Is it possible to have an other increment variable in this same loop that jumps 2 at once?
Thanks in advance!
EDIT: after reading the comment: I think what is needed is a counter to count, 1,2,3,4,5,6...15, and another one to count 1,3,5,7...15
For that, here is what is need to be done:
basically, you want the first iterator to be a normal counter,
and the second iterator to be odd numbers only.
So here is a simply input output table
input output
----- -----
1 1
2 3
3 5
4 7
5 9
6 11
From the above, we can deduce the formula needed to convert the input into the desired output: output = (input x 2) -1
And so, we can re-write our for loop to be like so:
For n=1 to 15
Cells(n,8) = Application.Combin(2*n-1,n)
Next
============= End of Edit =========================
Simply, use the keyword STEP in the for loop
For n = 1 To 15 STEP 2 'STEP can also be negative
'but you have to reverse the starting, and endin
'value of the iterator
The values for n will be: 1, 3, 5, 7, 9, 11 , 13, 15
Alternatively, use a local variable inside the for loop for that purpose (in-case you want the loop to execute 15 times)
For n=1 to 15
i = n + 1
Cells(i,8) = Application.Combine(2*n,n)
Next

BlackJack Project - Can't make aces change from 11 to 1's

I'm currently making a blackjack game for my project in school in Visual Basic.
In blackjack, when you have aces (value initially 11) their value turns to 1 when the total value of the cards is > 21. In code, this would just take away 10 for every ace
I'm stuck on this.
This is the code I have (that doesn't work):
Do While PlayerValue > 21 And counter <= noAcesPlayer
counter += 1
PlayerValue -= 10
Loop
In a senario, I have a: 2, 8, A, 8 (=29)
But since there is an Ace, and the total value is > 21, the value should have 10 subtracted from it (=19) - the above code does not do this.
Another scenario would be 10, 8, A, A (=40)
Again, the two Aces should turn into 1's, since the total value > 21, giving 20.
Any help would be greatly appreciated. :)
Here is an approach
Public Enum CardFace
None
Ace
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
End Enum
This code should produce a value of twenty
Dim cards As New List(Of CardFace) From {CardFace.Ten, CardFace.Eight, CardFace.Ace, CardFace.Ace}
Dim total As Integer = 0
Dim numofAces As Integer = 0
For Each c As CardFace In cards
Debug.WriteLine(c.ToString)
If c = CardFace.Ace Then
numofAces += 1
Else
total += c
End If
Next
If numofAces > 0 Then
If total + 11 + (numofAces - 1) > 21 Then
total += numofAces
Else
total += 11 + (numofAces - 1)
End If
End If
Debug.WriteLine(total)
The correct way to build a blackjack hand is the following (in pseudo-code):
Variables: total = 0, soft-flag = false
For each card in hand:
Add card value to total. Faces are 10, aces are 1.
If the card you added was an ace, set soft-flag = true
If total < 12 and soft-flag:
Add 10 to total
Else:
set soft-flag = false
That's it. Only one loop over the cards, no extraneous variables, and you're left with the total value and a flag indicating if the total is soft.