Iterate thru specific controls in a form using a loop

0 votes

If I want to iterate only the txt3, txt6, txt8, and txt9 and multiply their values by 2, I can type it as shown below, but how can I accomplish it using a loop? Let's say I have 10 textboxes on a form labeled txt1 to txt10.

txt3.value = txt3.value * 2
txt6.value = txt6.value * 2
txt8.value = txt8.value * 2
txt9.value = txt9.value * 2
Jan 29, 2023 in Others by Kithuzzz
• 38,000 points
386 views

1 answer to this question.

0 votes

The textbox numbers you want to iterate should first be stored in an array. Then, using a for loop, iterate through that array and carry out your desired operation. Try the sub below.

Private Sub CommandButton1_Click()
Dim arr
Dim i As Long

    arr = Array(3, 6, 8, 9)
    
    For i = LBound(arr) To UBound(arr)
        Me.Controls("TextBox" & arr(i)) = Me.Controls("TextBox" & arr(i)) * 2
    Next i

End Sub
answered Jan 29, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Activating a Specific Cell in Excel Using VBA Results to Error 400

I think you trying to select cells(4, ...READ MORE

answered Dec 27, 2022 in Others by narikkadan
• 63,600 points
568 views
0 votes
1 answer

Excel VBA- How to loop through specific sheets in a workbook and format the same ranges in each sheet

Range(...) instructs VBA to always use the ...READ MORE

answered Mar 21, 2023 in Others by Kithuzzz
• 38,000 points
1,797 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,259 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,696 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
971 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,089 views
0 votes
1 answer

Using standard deviation function in excel for a specific criteria?

Use this: =STDEV(IF((A1:A1000>=-0.5)*(A1:A1000<=0.5),A1:A1000)) Depending on one's version this may ...READ MORE

answered Nov 12, 2022 in Others by narikkadan
• 63,600 points
925 views
0 votes
1 answer

How do delete an specific page in a word document using Excel VBA?

Check the code below. I eliminated the ...READ MORE

answered Nov 21, 2022 in Others by narikkadan
• 63,600 points
1,581 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP