Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a userform containing 10 texboxes. Want to save all the text boxes data in sheet with seperate rows of Excelsheet for each textbox data.

I have used for loop to enter data but I am getting problem in code like not able to assign dynamic controlname to cell...

control name are like txtName1, txtName2..... txtName10

see code...

Dim i As Integer
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sales")
For i = 1 To 10
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 1) = txtName+i+.Text -- Here is the problem
Posted
Updated 4-Nov-14 23:01pm
v2

1 solution

Hi,

As you mentioned in code, you can't write like txtName + i + .Text.

But this is one of the possible way, Create an array with the names of text boxes you want...

Dim ControlNames(1 To 8) As Variant

VB
ControlNames(1) = "TextBox1"
ControlNames(2) = "TextBox2"
ControlNames(3) = "TextBox3"
ControlNames(4) = "TextBox4"
ControlNames(5) = "TextBox5"
ControlNames(6) = "TextBox6"
ControlNames(7) = "TextBox7"
ControlNames(8) = "TextBox8"


after this, you can access as shown below,

VB
 For i = 1 To 8  '10
    sht.Cells(i, 1).Value = UserForm1.Controls(ControlNames(i)).Text
Next i



Hope this is helpful to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900