excel vba, assigning values to variables with similar names using for next loop - vba

Brand new to stackoverflow and am also quite new to programming. I am very grateful for this incredible resource. Thank you!
I have 30 TextBoxes on my Userform. TextBox1 TextBox2......TextBox30. I would like to assign values to all of them quickly using a For Next Loop. Just not too sure how.
Example:
For X=1 to 30
TextBox & X.Value ="Hello"
Next X
Thank you in advance for any help.

This should do it.
Private Sub UserForm_Initialize()
For x = 1 To 30
Controls("Textbox" & x).Value = "Hello " & x
Next x
End Sub

How about this:
For x = 1 To 30
Sheets(1).Shapes("textbox " & x).TextFrame.Characters.Text = "Hello"
Next x

Related

using an input box to set up how many times a loop will run

I have a code that puts spaces in a column of numbers, so that each group of 6 numbers have 2 spaces between them, starting from the top of the sheet. After the groups of 6 there can be groups of 5, these need to have 3 spaces between them.
The groups of 6 always appear above the groups of 5.
I would like to have a code that asks how many groups of 6, then asks for how many groups of 5, then puts in the relevant spacing.
Sub MacroMan()
Dim x As Integer
x = 8
For i = 1 To CInt(InputBox("Run this many times:"))
'Range("H2").Paste
Application.CutCopyMode = False
Range("H&x:H&x+1").Insert Shift:=xlDown
'Range("H2:H110").Copy
x = x + 8
Next
End Sub
I've got the code to run now, but I get the following error.
Run-time error '1004'
Method 'Range of object'_'Global'failed
You can ask the user for their input and store the answer in a string. Then convert the string to an integer and use it to determine the number of loops.
sub AskUser
dim Answer as string
dim i as integer
dim Number as integer
Answer = Inputbox("Your text here")
Number = CInt(Answer) 'Note this will throw an error if you enter something that's not a numbeer
for i = 1 to Number
'code
next i
End sub
This should do the same thing, for the amount of times that you specify.
Important: Always run untested code on a copy of your data as executed code cannot be undone!
Sub SO()
Dim x As Integer, i As Integer
x = 8
Application.CutCopyMode = False
For i = 1 To CInt(InputBox("Run this many times:"))
Range("H" & x & ":H" & x + 1).Insert Shift:=xlDown
x = x + 8
Next
End Sub

for each loop using a datagridview

I trying to perform a calculation on some values within a DGV that has been imported from data within an xls file. but I cant get the loop to work. what I need it to do is read the value of the cells in column 2 and column 3 of each row, put them in to a text box and then output the result of the calculation to column 4 of the DVG. I would post the code but frankly I haven't got anything real to go on. am I correct in thinking I need to be using a For each loop???
Any help or guidance is greatly appreciated.
Thanks in advance
I find it Little hard understanding your request... But how about this?
For Each Row As DataGridViewRow In DataGridView1.Rows
Dim CellValues As New List(Of String)
Dim Failed As Boolean = False
For x = 1 To 2 'This is right. It's NOT supposed to be 2 To 3.
If Row.Cells(x).Value <> Nothing Then
CellValues.Add(Row.Cells(x).Value.ToString())
ElseIf Row.Cells(x).Value = Nothing Then
Failed = True
End If
Next
If Failed = True Then
Continue For
End If
TextBox1.AppendText(CellValues(0) & " + " & CellValues(1) & Environment.NewLine)
Row.Cells(3).Value = CInt(CellValues(0)) + CInt(CellValues(1))
Next

VBA Excel Transform to loop

Here i have my hundred line of code please enlighten me about how do i put line code into loop
here is my try but i wont work out still trying
If sp.Name Like "Rounded Rectangle*" Or sp.Name Like "Oval*" Then
For i = 11 To 100
x = i - 9
Sheet2.Shapes.Range(Array("Rounded Rectangle " + i)).TextFrame.Characters.Text = Sheet1.Range("A" + x)
Next i
End If
and repeat until X = 110
in this case how can i change it in to correct loop please advice
thank you
This is a general approach to making a loop to cover a string variable....say we want to loop over Shape("Rectangle 1")....Shape("Rectangle 2")....Shape("Rectangle 3)..... , etc.
Dim str As String, i As Long
For i = 11 To 100
str = "Rectangle " & CStr(i)
Sheets2.Shapes(str)................
Next i
and use a similar approach to make "A2"..."A3".........
simple math:
For i = 11 To 100
change to
For i = 11 To 119

Double For Loop in VB.NET

Dim ssi(11) As String
For i = 0 To 10
If ssi(i) = "" Then ssi(i) = "0"
For j = 0 To Val(ssi(11)) + i
ssi(i) = xuh(Val(ssi(i)))
Next
Next
If ssi(11) = "2" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10)))
ElseIf ssi(11) = "3" Then
L_zz.Caption = Val(Left(ssi(0) & ssi(1) & ssi(2) & ssi(3) & ssi(4) & ssi(5) & ssi(6) & ssi(7), ssi(10))) * (-1)
End If
I am new here and new to VB as well.
I am trying to understand this double loop in vb code.
ssi(i) is defined as a String variable. and each element is assigned to a specific number in a String. Hope I told it clearly.
My problem with this loop is below.
Since i ranges from 0 to 10, what does this j mean? Does j mean the new ssi(1-10) or another whatever number?
I think the best way to answer your question about understanding a double loop is to try looking at something simpler.
The first program I always write in each new version of BASIC that comes along is a 12 times table.
I've modified it a bit below to be a 12 x 10 table for the purpose of illustrating for you how a double loop works ... hope it helps:
For x As Integer = 1 To 12
For y As Integer = 1 To 10
Console.Write(x * y)
Console.Write(vbTab)
Next
Console.WriteLine()
Next

Visual Basic Fill Array with simple number set

I need to fill an array with numbers 1-50, and I currently have the code:
Dim numberSet(49)
For x = 1 To 50
numberSet(x - 1) = x
Next x
The challenge is to do it in the least amount of lines possible. This part is bugging me because it seems like i shouldn't be using 4 lines for something so basic.
Any thoughts from you guys? I want to avoid doing something like = {1,2,3,4,5...50} if I can. Thanks!
In one line:
Dim numberSet(49): For x = 1 To 50: numberSet(x - 1) = x: Next x
One line (but it creates a 1-based array...)
Sub TT()
Dim arr
arr = Application.Transpose([=ROW(A1:A50)])
Debug.Print UBound(arr)
Debug.Print arr(1)
Debug.Print arr(13)
End Sub
...and if you turn off Option Explicit you can skip the declaration. But don't do that ;-)