VBA : Transfer Data from VBA Form to Excel worksheet

KMGIL

New Member
Joined
Dec 16, 2014
Messages
26
Hi
I have created a VBA excel form which pre populates a number of text boxes based on an excel file . As well as the Textboxes there are a number of comboboxes. The data that is generated from the excel file is based on a user selecting an email address so the number of rows changes each time.

The form works for this .

What I do not know is how I can transfer this data from the form to an excel worksheet so that the various number of rows are always captured.

Please see below code to date for form generation


Thanks

Kind Regards

ken



Code:
Private Sub CommandButton1_Click()
Dim i As Long
For i = 1 To Sheet2.Range("A1000000").End(xlUp).Row






'Line 1
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 15
.Height = 15
.Top = i * 22
.Width = 75
.Font.Size = 8
.Locked = True
.Value = Sheet2.Cells(i, 3)
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With








'Line 2
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 100
.Height = 15
.Top = i * 22
.Width = 40
.Font.Size = 8
.Locked = True
.Value = Sheet2.Cells(i, 4)
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With




'Line 3
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 160
.Height = 15
.Top = i * 22
.Width = 250
.Font.Size = 8
.Locked = True
.Value = Sheet2.Cells(i, 7)
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With




'Line 4
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 430
.Height = 15
.Top = i * 22
.Width = 300
.Font.Size = 8
.Locked = True
.Value = Sheet2.Cells(i, 6)
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With




'Line 5
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 750
.Height = 15
.Top = i * 22
.Width = 50
.Font.Size = 8
.Locked = True
.Value = Format(Sheet2.Cells(i, 10), "#####.00")
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With


'Line 6
Set tbox = Controls.Add("Forms.ComboBox.1")
With tbox
.List = Array("Yes", "No")
.Name = i
.Left = 820
.Height = 15
.Top = i * 22
.Width = 40
.Font.Size = 8
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF








End With




'Line 7
Set tbox = Controls.Add("Forms.ComboBox.1")
With tbox
.List = Array("< 1 Month", "1-2 Months", "2-3 Months", "3 Months +")
.Name = i
.Left = 880
.Height = 15
.Top = i * 22
.Width = 70
.Font.Size = 8
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF




End With


'Line 8
Set tbox = Controls.Add("Forms.ComboBox.1")
With tbox
.List = Array("Yes", "No")
.Name = i
.Left = 970
.Height = 15
.Top = i * 22
.Width = 40
.Font.Size = 8
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF






End With




'Line 9
Set tbox = Controls.Add("Forms.textbox.1")
With tbox
.Name = i
.Left = 1030
.Height = 15
.Top = i * 22
.Width = 250
.Font.Size = 8
.Locked = False
.Value = Sheet2.Cells(i, 15)
.BackColor = &H80&
.BorderColor = &H80&
.ForeColor = &HFFFFFF


End With






' Userform height Setting
If i <= 2 Then
Me.Height = i * 80
ElseIf i <= 10 Then
Me.Height = i * 40
Else
Me.Height = i * 25
End If


Next i




End Sub
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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