Iterations In From Loop - vba

is there a way to say in vba something like:
from x = 1 to 100, by 10
so that the x's are 1, 10, 20, 30, etc. to 100?

You can use STEP:
for x = 0 to 100 step 10
next x
This will take you through 0, 10, 20... 100
Since you want to start at 1 and go 1, 10, 20... 100, here is a slight modification
for x = 0 to 100 step 10
if x = 0 then
y = 1
else
y = x
end if
'// use y in all calculations downstream instead of x
next x

For y = 0 To 10
If y = 0 Then x = 1 Else x = 10 * y
' do stuff with x
Next y

Related

Excel VBA Loop x = x + 1

I'm new to Excel VBA and i'm trying to make a loop that sums X = X + 1 but when the loop ends it continues with the last X and doesn't starts again.
This is what I have:
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
Debug.Print K
Next K
Next I
This is what i get: 1 2 3 4 1 2 3 4 1 2 3 4 .
What i would like to get is: 1 2 3 4 5 6 7 8 9 10 11 12 .
Thanks for the help provided. I thought this would solve my problem but it's a bit more complicated. I need this because i'm adding coordinates in X, Y, Z format with this code:
For I = 1 To 6
X = 0
J = 10
RobApp.Project.Structure.Nodes.Create X = X + 1, 0, 0, J * (I - 1)
RobApp.Project.Structure.Nodes.Create X = X + 1, Range("N34") * 0.15, 0, J *
(I - 1)
Next I
"X = X+1" is the node number. I want it to be sequencial, 1,2,3,4 and so on while J is increasing in the Z coordinate. For example for the first line of code:
Node 1 = 0,0,0
Node 2 = 0,0,10
Node 3 = 0,0,20
and so on!
Or rather, use the extra variable X as you originally planned:
X = 0
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
X = X + 1
Debug.Print X
Next K
Next I

Project Euler 3

I have been trying to solve question 3 on project euler with the following vb code but I do not under stand why it is not working. Can someone please point me in the right direction?
Sub Main()
Dim p As Int64 = 600851475143
Dim y As Integer
For i As Int64 = p / 2 To 1 Step -1
If p Mod i = 0 Then
y = 0
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
y = y + 1
End If
Next
If y = 0 Then
Console.WriteLine(i)
Console.ReadLine()
End If
End If
Next
End Sub
The question is "The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
You start with n=1. Every number divides evenly by 1.
(so y = y+1 every time, and If y = 0 Then can never happen).

Trying to make a For Loop that adds something in Visual Basic; not getting right answer

I need to make a console app for a class, and it has to compute the following using a For Next loop: 4 + 8 + 12 + 16 + 20 .... + 208
Here's what I have:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + 4
Console.WriteLine(z)
Next
I have no idea what I'm doing wrong.
On each iteration, you are adding 4 to z, so you are actually computing 4 + 4 + 4 + ... + 4. What you really want to do is to add x to z:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + x
Console.WriteLine(z)
Next

what to do with fraction as index number

given these inputs x = 4, S = [1 2 3 4 5 6 7 8 9 10], and n = 10
search (x,S,n) {
i = 1
j = n
while i < j {
m = [(i+j)/2]
if x > Sm then i=n+1
else j = m
end
if x = Si then location = i
else location = 0
This code is not from any particular language its just from my discrete math hw, but I'm confused as to what Sm would equal on the first iteration because m would be 11/2. If i use a fraction as the index do I round down? Is there a general rule for this? Am I making any sense? Help pls

VB.NET - Grouping statements on one line

I want to put several statements on one line, including conditional statements that need to be grouped together.
I can do this:
x += 20 : y += 10 : If x > 400 Then x = 0
I want to have more than one statement under 'if'. It balks when I try to do this:
x += 20 : y += 10 : (If x > 400 Then x = 0 : y = 0)
or this:
x += 20 : y += 10 : If x > 400 Then (x = 0 : y = 0)
Is there a way?
x += 20 : y += 10 : If x > 400 Then x = 0 : y = 0
But I wouldn't recommend putting it on one line. It's a bit nasty to readability.