Writing Text box value from userform to worksheet

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,415
here is all the code for my user form.

the form initializes just fine and I can type values into all my text boxes.

but when I click the OK Button (CommandButton1) to write the date to row 2 of my worksheet I get the following error.

"Object doesn't support this property or method"

for this line
Cells(2, c) = Controls(“txtBox” & c).Text




Code:
Private Sub CommandButton1_Click()
Dim c As Long
For c = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(2, c) = Controls(“txtBox” & c).Text
Next c
End Sub

Private Sub UserForm_initialize()
Dim ctlLbl As Control
Dim ctlTxt As Control
 
lc = Cells(1, Columns.Count).End(xlToLeft).Column
 
For c = 2 To lc
    Set ctlLbl = Me.Controls.Add("Forms.Label.1") 'Label
    With ctlLbl
    .Name = “lblBox” & c
    .Top = (c - 1) * 25
    .Left = 20
    .Width = 200
    .Caption = Cells(1, c)
    .Font.Bold = True
    .Font.Size = 10
    End With
Next c

 For c = 2 To lc
    Set ctlTxt = Me.Controls.Add("Forms.TextBox.1") 'text
    With ctlTxt
    .Name = “txtBox” & c
    .Top = (c - 1) * 25 - 10
    .Left = 150
    .Height = 25
    .Width = Cells(1, c).Width * 3
    .Font.Bold = True
    .Font.Size = 13
    End With
Next c
  
End Sub

Thanks for looking,

Ross
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There are two types of quotation marks; The slanted type “ ” and the so-called normal stype " ".
In VBA, the normal type is required. You're using a mix of both.

Code:
Private Sub CommandButton1_Click()
Dim c As Long
For c = 2 To Cells(1, Columns.Count).End(xlToLeft).Column
    Cells(2, c) = Controls([COLOR="#FF0000"]“[/COLOR]txtBox[COLOR="#FF0000"]”[/COLOR] & c).Text
Next c
End Sub

Private Sub UserForm_initialize()
Dim ctlLbl As Control
Dim ctlTxt As Control
 
lc = Cells(1, Columns.Count).End(xlToLeft).Column
 
For c = 2 To lc
    Set ctlLbl = Me.Controls.Add("Forms.Label.1") 'Label
    With ctlLbl
    .Name = [COLOR="#FF0000"]“[/COLOR]lblBox[COLOR="#FF0000"]”[/COLOR] & c
    .Top = (c - 1) * 25
    .Left = 20
    .Width = 200
    .Caption = Cells(1, c)
    .Font.Bold = True
    .Font.Size = 10
    End With
Next c

 For c = 2 To lc
    Set ctlTxt = Me.Controls.Add("Forms.TextBox.1") 'text
    With ctlTxt
    .Name = [COLOR="#FF0000"]“[/COLOR]txtBox[COLOR="#FF0000"]”[/COLOR] & c
    .Top = (c - 1) * 25 - 10
    .Left = 150
    .Height = 25
    .Width = Cells(1, c).Width * 3
    .Font.Bold = True
    .Font.Size = 13
    End With
Next c
 
Upvote 0
OMG. Who new, I don't even know how to type in the slanted quote, must have been that way from the source I copy and pasted some of the code from.

Anyway, it works now.

my head still hurts from banging it on the wall trying to figure it out.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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