Display tables between two numbers - vbscript

I have been trying to create a VB script using the "Do While" loop in the VBScript. I want to generate tables between two numbers accepted from the user I don't know what's wrong with this:
option explicit
Dim i, a, j, product, table
a = inputbox("enter a number")
For i=1 to a
j=1
Do while(j<=10)
product=i*j
a=i&"*"&j&"="&product
table=table&vbnewline&a
loop
msgbox table
next
The above results in a blank window.
Can anyone help me resolve the problem?
P.S.
I managed to get it to work using the "For" loop.

Option 1: Use a For loop:
Dim i
For i = 1 To 10
' This will run 10 times
Next
Option 2: Use a Do While loop:
Dim j
j = 1
Do While j <= 10
' This will also run 10 times
j = j + 1
Loop
Edit:
If you need to loop on both i and j, you can create a nested For loop:
Dim i, j
For i = 1 To 10
For j = 1 To 10
' This will run 10 times for each value of i (so 100 total)
Next
Next

Related

Microsoft Access - Label Printing - each 10 labels to print vertically

Good day, I am having an issue, I need to print 3000 lables every 7 days and they are cut in to individual lables, at the moment they are printing in sequence 1,2,3,4,5,6...etc, so person wastes time to pick them up and put them in correct slots
Now what I need is that record Number 2 is printed on 2nd page, 3rd record on page 3 and like this 10 pages, my 11 record under is on 1st page just below record number 1, record 12 is on page 2 under record number 2.....
So idea is that person can grab a stack of 10 record instead of taking them one by one, but I am not sure how can I do it=\ any Ideas?
' Macro to assign numbers to data source so that it can be sorted to cause labels to print down columns
Dim Message, Title, Default, labelrows, labelcolumns,
i As Integer, j As Integer,
k As Integer
Message = "Enter the number of labels in a row" ' Set prompt.
Title = "Labels per Row" ' Set title.
Default = "3" ' Set default.
' Display message, title, and default value.
labelcolumns = InputBox(Message, Title, Default)
Message = "Enter the number of labels in a column" ' Set prompt.
Title = "Labels per column" ' Set title.
Default = "5" ' Set default.
labelrows = InputBox(Message, Title, Default)
ActiveDocument.Tables(1).Columns.Add
BeforeColumn:=ActiveDocument.Tables(1).Columns(1)
ActiveDocument.Tables(1).Rows(1).Range.Cut
k = 1
For i = 1 To ActiveDocument.Tables(1).Rows.Count - labelcolumns
For j = 1 To labelrows
ActiveDocument.Tables(1).Cell(i, 1).Range.InsertBefore k + (j - 1) *
labelcolumns
i = i + 1
Next j
k = k + 1
i = i - 1
If k Mod labelcolumns = 1 Then k = k - labelcolumns + labelcolumns *
labelrows
Next i
ActiveDocument.Tables(1).Sort FieldNumber:="Column 1"
ActiveDocument.Tables(1).Rows(1).Select
Selection.Paste
ActiveDocument.Tables(1).Columns(1).Delete
this one I had was for word, unfortunately it didn't work exactly the way I need

For Loop in Visual Basic results wrong number of loops

I don't understand why I get x with a value of 6 while I think it should be 5.
Sub Main()
Dim x = 0
For x = 1 To 5
Next
Console.WriteLine(x)
Console.ReadLine()
End Sub
Result: 6
The reason that x equals 6 is because of the nature of a loop. You put no code inside the body of the loop. If you printed your code there you would see
1
2
3
4
5
Each time Next is reached, x is incremented. The fifth time you go through the loop, x is incremented to 6. In most cases it's best to not use loop variables outside of their loop. Using a C style loop what I mean is a bit more clear
for (int i=0; i<=5; i++){}
The loop runs until the condition i <= 5 is not true. Since each time through the loop i is increased by 1 this occurs first when i equals 6. I used the variable i here because i is a much more common loop variable name to see than x.

Multiplying 2 matrix using VBS

I would just like to know why this returns an "Expected end of statement" whenever I call the function multiply_matrix(matrixA, matrixB) and feed it with matrixA (3x3 matrix), and matrixB(3x3 matrix).
The error is always at "Next k".
This is the code of the function.
Function multiply_matrix(matrixA, matrixB)
dim answer_matrix(3,3)
for i=0 to UBound(matrixA,1)
for j=0 to UBound(matrixB,2)
sum = 0
for k=0 to UBound(matrixB,1)
sum = sum + ( matrixA(i,k) * matrixB(k,j) )
next k
answer_matrix(i,j) = sum
next j
next i
multiply_matrix = answer_matrix
End Function
Other Basic dialects allow variable names after the Next, VBScript doesn't.

Generate first 10 numbers of a sequence

I have been trying to create this sequence:
2,7,17,37,67,...
I have to print first 10 numbers of the series.
To do this, I have created the following:
option explicit
Dim m,n,i,str,d
m=2
d=10
n=7
For i=0 to 10
n=n+d
d=d+10
str=str&n&vbcrlf
msgbox str
next
I am unable to print first two numbers, 2 and 7, as they are declared before the for loop. Even if I store them in a variable called str, they get printed after every execution. Is there a way to add these two and print them only once.
You could add your initial value of m to your string before you start running your sequence. Then, append the value of n to your string at the start of your loop instead of the end so that you capture n's initial value. For example:
m=2: d=10: n=7
str = m & vbCrLf ' Capture initial value of m
For i = 1 to 9
str = str & n & vbCrLf ' Capture initial value of n
n = n + d
d = d + 10
Next
MsgBox str
Note that you're only looping 9 times now, since you've already captured the first number in your sequence (m) prior to your loop.
I also moved MsgBox outside your loop so that it only appears once, after the full 10-number sequence has been generated.

How to check if an item is already in a listbox in vb6

I'm working with vb6 and I want to generate multiple randum numbers (the range from to is detirmend by user and also the number of generated answers) and send them to a listbox
But I don't want to duplicate generated numbers So..
I want before sending the generated number to the listbox to check if it already exists in the lisbox. if it already exists then generate another number if it does't then send it the the listbox
here is what I have till now
max and min are the range to chose numbers between
answers is the number of generated numbers
Randomize
For i = 1 To answers Step 1
generated = CInt(Int((max - min + 1) * Rnd() + min))
For n = 0 To List1.ListCount
If List1.List(n) <> gen Then
List1.AddItem (gen)
Else
If List1.List = gen Then
'I don't know what to do from here
'(how to go back to generate another number)
Next n
Next i
Thank you in advance
keep in minde I need to keep things simple
Thank you soo much
Use a boolean value to keep the result if same value generated is in list.
Private Sub AddRandomNumbers()
Dim blnIfFound As Boolean
Dim max As Integer
Dim min As Integer
Dim answers As Integer
max = 10
min = 1
answers = 5
Randomize
Do While List1.ListCount < answers
generated = CInt(Int((max - min + 1) * Rnd() + min))
blnIfFound = False
For n = 0 To List1.ListCount
If List1.List(n) = generated Then
blnIfFound = True
Exit For
End If
Next n
If blnIfFound = False Then List1.AddItem (generated)
Loop
End Sub

Resources