Looping through variables and controls on a userform

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Code:
Label1.Caption = TextBox1.Value / Var1

Var1 is my variable name. And I have them from 1 to 14 .

And same apply for the label and textboxes.

Instead of writing all 14 lines I want to reduce it.

But I don't know how to handle the variables in the loop.

Can someone pull me out?

Thanks
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Learn how to use arrays to store variables

example
VBA Code:
Private Sub UserForm_Initialize()
    Dim a As Long, var
'store in array
    ReDim var(1 To 5)
    var(1) = "cat"
    var(2) = "lion"
    var(3) = "dog"
    var(4) = "rat"
    var(5) = "cow"
'loop through array and userform controls
    For a = 1 To 5
        Me.Controls("TextBox" & a).Text = var(a)
    Next a
End Sub

This is what the above code does :

User Form result.jpg


Here is a good tutorial
 
Upvote 0
Perhaps

VBA Code:
Dim i As Long

For i = 1 to 14
    Controls("Label" & i).Caption = Val(Controls("TextBox" & i).Text) / Var1
Next i
 
Upvote 0
Perhaps

VBA Code:
Dim i As Long

For i = 1 to 14
    Controls("Label" & i).Caption = Val(Controls("TextBox" & i).Text) / Var1
Next i

The Var1 too must switch just like the controls from 1 to 14
 
Upvote 0
Then, I would agree with Yongle's advice and suggest you learn about arrays. (All though I wouldn't use string variables as the denominator of a fraction. :) )
 
Upvote 0
Then, I would agree with Yongle's advice and suggest you learn about arrays. (All though I wouldn't use string variables as the denominator of a fraction. :) )

That's a valid concern. About the denominator.

Originally I am using a long Integer.

So how will I convert the string back?


Like
Code:
val(Var (a))
?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top