For Loop Counter in VBA increments by one extra - excel

I have a simple loop counter which I expected to msg me 1,2,3...10. Instead I get 1,3,5,7,9and I can't figure out where this extra increment is coming from. I'm a bit rusty after layoffs so bear with me if this seems simple.
here is my code:
Dim x As Integer
For x = 1 To 10
MsgBox x
x = x + 1
Next x

It is typical that a for loop in most programming languages will automatically increment or iterate through something. In most languages, it's also common to provide a way to specify the increment amount. This is the case in VBA—see VBA For...Next documentation. For example:
For i = 1 to 20 Step 2
'do something
next i
Your code is incrementing x, and For is also incrementing x. There are times when you want to increment the counter within the loop if some special condition is met, but not on every loop.
If you want to manually increment, then you should use a while loop instead.

The Next x line, when referring to an integer (x) in a For loop, will increment x by 1
If you would like to increment by more than one, or increment in a negative direction you could use step at the end of the For line, which provides direction to the Next portion. Example:
For x = 10 to 0 step -1
msgbox x
next x
will result in 11 consecutive msgbox's displaying:
10
9
8
7
6
5
4
3
2
1
0

Depending on how you would like to control X you have different options:
Option 1:
Let the for-loop control everything and use "step".
In this case x will raise by y at each iteration. And you should not alter x within the loop.
Dim y As Integer
y = 1
For x = 1 To 10 step y
MsgBox x
Next x
Option 2:
If X depends on something happening within the loop and you don't want the for loop to alter x you could set step to 0.
For x = 1 To 10 step 0
MsgBox x
If x Mod 2 = 0 Then
x = x + 3
Else
x = x - 1
End If
Next x
Alternativ option
Having a for loop as in option 2 not altering the x would be bad practice. It is not wrong as the code is working just fine but a while loop would be a better solution.
Dim x As Integer
x = 1
Do While x < 10
MsgBox x
If x Mod 2 = 0 Then
x = x + 3
Else
x = x - 1
End If
Loop

Related

array from continuous data?

I have a quite difficult problem that i cant wrap my head around.. hope you can help me!
Lets say my data is in A1:G1 for example:
A1 B1 C1 D1 E1 F1 G1
X X 0 X X X 0
or
Y X X X X Z X
The thing i would need to come up with is how to get array from this data according to the X, BUT if like in example 1 there is 2 times X in the beginning and 3 x next to each other so the array should come out like {2;2;0;3;3;3;0} so i want the array to be 7 long and the array should show the x as number how many are next to each other.
example 2 should come out like {0;4;4;4;4;0;1}
if you can figure this out would really help me alot!
Edit:
Trying to give out better, more bigger picture of what i mean..
if data is :
A B C
1 X X
2 X X
3 X
it should come out as
A B C
1: 2 4 0
2: 0 4 2
3: 1 0 0
or in array {2\4\0;0\4\2;1\0\0}
on B1 and B2 there should be 4 because the formula should count horizontal but also vertical continuum. I tried to use usmanhaqs formula but i was not able to modify it so the count resets on every line.
Real size of the table is 7 times 7 cells.
I will use the array with another array (scoreboard which is also 7 times 7 cells, and has numbers 1, 2 or 3 on every cell) using sumproduct and it will give out the points of that player.
I appreciate your efforts on helping out a newbie learner on vba :)
For a formula solution, I can only come up with one for the special case where you have just X's and zeroes (example 1) so far:
=SUM(IF(A1:G1<>"X",0,INDEX(FREQUENCY(IF(A1:G1="X",COLUMN(A1:G1)),IF(A1:G1<>"X",COLUMN(A1:G1))),N(IF({1},SUBTOTAL(2,OFFSET(A1,0,0,1,COLUMN(A1:G1)))))+1,1)))
entered as an array formula using CtrlShiftEnter
I have wrapped it in a SUM function to demonstrate that it generates an array which can be passed to another function (result: 13) or it can be array-entered across several cells:
You can test this code
Function get_array(r As Range, match_chr As String)
Dim check_val
Dim array_value
array_value = "{"
For i = 1 To r.Count
check_value = r.Item(i)
If (check_value = match_chr) Then
j = i + 1
Do While (j <= r.Count) And (check_value = r.Item(j))
j = j + 1
Loop
array_value = array_value & WorksheetFunction.Rept(j - i & ", ", j - i)
i = j - 1
Else
array_value = array_value & "0, "
End If
Next
array_value = Left(array_value, Len(array_value) - 2) & "}"
get_array = array_value
End Function
You can use it as below
EDIT
find below another function to return an array of values that can be used in the formulae
Function get_number_array(r As Range, match_chr As String)
Dim check_val
Dim array_value
Dim number_array(1 To 50) As Long
For i = 1 To r.Count
check_value = r.Item(i)
If (check_value = match_chr) Then
j = i + 1
Do While (j <= r.Count) And (check_value = r.Item(j))
j = j + 1
Loop
For k = 1 To j - i
number_array(i + k - 1) = j - i
Next k
i = j - 1
Else
number_array(i) = 0
End If
Next
get_number_array = number_array
End Function
You have to use it same as the previous one, but it will return excel array.

Table with 4 variable columns

I'm sure this is simple for all of you, but I'm new here. How do I create a formula or code that can output all of the potential scenarios for this type of array below? Basically, max is 60, min is 0, but I'm unsure how to make Excel spit out a table that represents this.
Solution:
Doing it with Excel formulas alone, while theoretically possible, is incredibly computationally demanding and crashed Excel when I was trying to do so. You can do this fairly easy with VBA though.
Create a VBA module, drop these two snippets in, press play, and wait for a few seconds to a minute while the code runs. The code is not the most efficient, but it is probably the simplest algorithm to understand.
Public Sub comb4()
Dim a, b, c, d, n, r, x As Integer
x = 60
a = x
Do While a >= n
b = x - a
Do While b >= n
c = x - a - b
Do While c >= n
d = x - a - b - c
Do While d >= n
If sumToZero(-x, a, b, c, d) Then
r = r + 1
Cells(r, 1).Value = a
Cells(r, 2).Value = b
Cells(r, 3).Value = c
Cells(r, 4).Value = d
End If
d = d - 1
Loop
c = c - 1
Loop
b = b - 1
Loop
a = a - 1
Loop
End Sub
Public Function sumToZero(ParamArray intNums())
For x = LBound(intNums) To UBound(intNums)
y = y + intNums(x)
Next x
If y = 0 Then sumToZero = True
End Function
Code explanation:
x is the max that you defined, while n is the min you defined.
r is a counter which helps us track what row to print to.
Since we always want to count down to 0 for each column's sub-permutations, each of our Do While loops will count from the theoretical maximum value down to 0.
The loop structure is nested so that each time we hit -1 in a column N we instead leave the loop, go into the loop one level higher, and decrease the value in column M by 1. The value in N is reset when we start the next instance of the loop for M.
Between each value change we need to check to see if the result is a valid solution. We pass the (negative) max and all 4 variables we are looping to a function which sums the values and returns true if the variables are equal to the max. When the function is true, we go to the next row in Excel and print the four values.

Simple python and need explanation

m = 0
for x in range (4,6):
for y in range (2,4):
m = m + x + y
print (m)
ANSWER: 28
not sure how this is? Excluding the last number in the range, I thought it should be 14. I add it up on paper and cannot understand what I am doing wrong.
That loop is equivalent to:
m = 4+2 + 4+3 + 5+2 + 5+3
And, that sum is 28.
(In the outer loop, x takes on the values 4 and 5. In the inner loop, y takes on values 2 and 3.)

Finding Max of 3 Inputs VBA

I am trying to find the maximum of 3 inputs. The problem is not in the algorithm, as when I made the same script in python it worked just fine. The problem is that it does not work as expected. I will write some scenarios and what the outcome was:
8 5 12 - Max: 12
5 8 12 - Max: 12
12 5 8 - Max: 8
12 8 5 - Max: 8
5 12 8 - Max: 8
8 12 5 - Max: 8
100 22 33 - Max: 33
22 3 100 - Max: 100
100 22 3 - Max: 22
It seems that it works for quite some combination, but not for each and every one. I haven't managed to find a pattern yet, and I can't figure out what is going wrong.
I am attaching the code:
Sub Maxthree()
'Calculates the maximum of three numbers'
Dim x, y, z As Single
x = InputBox("Enter the first number!")
y = InputBox("Enter the second number!")
z = InputBox("Enter the third number!")
MsgBox ("X: " & x & " Y: " & y & " Z: " & z)
If x > y Then
If x > z Then
MsgBox ("the maximum is : " & x)
Else
MsgBox ("the maximum is : " & z)
End If
Else
If y > z Then
MsgBox ("the maximum is : " & y)
Else
MsgBox ("the maximum is : " & z)
End If
End If
End Sub
Because they are input using an InputBox, it's comparing text values. So, for example "8" is greater than "12". Instead try converting to Longs like:
x = CLng(InputBox("Enter the first number!"))
You can also simplify your code to:
MsgBox WorksheetFunction.Max(x, y, z)
Here is the pattern you were looking for.
Since X and Y are Variant while Z is Single, this is how VBA will perform the comparisons:
X vs Y: string vs string (this is what is causing all the trouble)
X vs Z: numeric (X will be converted automatically)
Y vs Z: numeric (Y will be converted automatically)
Re-evaluate all 9 of your scenarios, with X and Y being compared as strings and (X or Y) being compared to Z as numbers. The results you observed, while unexpected, are correct.
Just feel fortunate that you aren't programming in PHP, where this is all much worse!
Microsoft are to blame for allowing Variant to be the default data type if no other type is specified. They support "Option Explicit" to force variables to be declared. They should go a step further, and have an option to require data types in all declarations.
Here is a function that returns the Largest element of any number of them:
Function Largest(ParamArray a() As Variant) As Variant
'returns the largest element of list
'List is supposed to be consistent: all nummbers or all strings
'e.g: largest(2,6,-9,7,3) -> 7
' largest("d", "z", "c", "x") -> "z"
'by Patrick Honorez --- www.idevlop.com
Dim result As Variant
Dim i As Integer
result = Null
For i = LBound(a) To UBound(a)
If result > a(i) Then
'nothing to do. This construct will properly handle null values
Else
result = a(i)
End If
Next i
Largest = result
End Function

number of executions in an algorithm

For this algorithm:
i = 1
while(i<=2n){
x = x + 1
i = i + 2
}
can someone tell me how to find the formula for the number of times x = x + 1 is executed?
i goes from 1 to 2n (inclusive), so first thought is 2n.
But we see that i increments by two at a time instead of one, so it's half of that: n.
With n<1 number of times executed=0.
With n>=1 number of times executed=n.

Resources