Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Userform Textbox linked to Cell value. 1

Status
Not open for further replies.

bsurfn99

Technical User
Mar 23, 2005
34
US
I am looking to have a userform textbox value linked to a cell. I would like to act as default, being able to be changed by the user should they desire. I will then take the data and put it to a database.

There are a lot of threads on this, but I have been unsuccessful in applying them to my application so far.

When the userform is loaded, I would like the textbox to have the value loaded.

here is where I'm at....

sub load_test_form()
load test_form
end sub

private sub Userform_Initialize()

Dim txt_test as string
dim celval as string
celval = sheets("Sheet1").range("M17".value
txt_test.text = celval

End sub

I get an invalid qualifier error and I'm sure where I went wrong. Any ideas? seems simple... I can get it to work for labels, just not textboxs.
 

HI,
Code:
private sub Userform_Initialize()
'txt_test should be an existing textbox and should not be declared

txt_test.text = sheets("Sheet1").range("M17").value
End sub

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Perfect. Thanks for explaining the error as well.
 
If you want a two-way link where if the cell contents change, the textbox contents change, and vice versa, the following example may be of interest. It takes advantage of the user form being able to hook into the worksheet's events. To take full advantage of being able to go from the worksheet to the form and back, you'll need to set the user form's ShowModal property to False.
Code:
Private WithEvents wsLinked As Worksheet
Private txt_testAddress As String

Private Sub txt_test_Change()
    wsLinked.Range(txt_testAddress).Value = txt_test.Text
End Sub

Private Sub wsLinked_Change(ByVal Target As Range)
    If (Target.Address = txt_testAddress) Then
        txt_test.Text = wsLinked.Range(txt_testAddress).Value
    End If
End Sub

Private Sub UserForm_Initialize()
    Set wsLinked = Sheet1
    txt_testAddress = "$M$17"
    wsLinked_Change wsLinked.Range(txt_testAddress)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top