Fill txtbox on userform with value from worksheet

Bandito1

Board Regular
Joined
Oct 18, 2018
Messages
233
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I'm trying to fill the textbox txtStDate with a value from the worksheet;

VBA Code:
Private Sub UserForm_Initialize()
txtStDate = Cells(Target.Row, "A")
End Sub

It doesn't work. The userform won't run.
When i try to look into the error it says; txtStDate = ""

Someone knows what is going wrong?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
In your userform module

VBA Code:
Public MyTarget As Range

Private Sub UserForm_Activate()
    If Not MyTarget Is Nothing Then
       Me.TextBox1 = MyTarget
    Else
        MsgBox "something went wrong with assigning rngTarget variable"
    End If
End Sub

In the worksheet module, assuming you are using worksheet_change event
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    With UserForm1
        Set .MyTarget = Target
        .Show
    End With

End Sub
 
Upvote 0
Thanks for your reply.

I have problems understanding your answer.

The case is like this;

Someone places "x" in column N on my worksheet.
When this is done the userform Returns pops up.

This userform contains the textbox txtStDate.
The value in this textbox should be the value that is in column A of the same row where the "x" is placed in.

When i use

VBA Code:
Private Sub UserForm_Initialize()
txtStDate = "Hi"
End Sub


it works.
The textbox shows "hi" when the userform runs.

But when i use

VBA Code:
Private Sub UserForm_Initialize()
txtStDate = Cells(Target.Row, "A")
End Sub

The userform doesn't run
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,411
Members
449,081
Latest member
JAMES KECULAH

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