Announcement

Collapse
No announcement yet.

Pass Cell Value To UserForm TextBox As Text

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pass Cell Value To UserForm TextBox As Text

    Hi,
    I am new to this forum and not really sure how this question will be routed.

    I am trying to learn how to write a macro and create a UserForm. While doing so, I ran into a problem trying to capture an existing data in a cell ("C5") from a sheet called details. I just wanted to display the content of cell "C5" into the textbox that I created in my UserForm.

    I appreciate any help and thanks to the technology like this.

    Thanks and Regards,

  • #2
    Re: Capture Data From Sheet To Show In Userform

    There are two techniques that you can use.
    Putting this in a normal module
    Code:
    UserForm1.TextBox1.Text = CStr(Range("c5").Value)
    UserForm1.Show
    Will put C5's contents into the textbox before the userform is shown. Replacing CStr(Range("c5").Value) with a variable gives you control over which value goes into the text box.


    If you know that C5's value will always be the intial value of the text box, this code can be put in the UserForm's code module. The range is fully qualified to insure that which sheet is active doesn't affect the right value being put in the text box. This consideration also applies to the other technique, but since the range reference is outside of the userform, its less of a suprise.
    Code:
    Private Sub UserForm_Initialize()
    Me.TextBox1.Text = CStr(ThisWorkbook.Sheets("Sheet1").Range("C5").Value)
    End Sub
    In both cases
    Code:
    UnLoad Userform1
    will erase the contents of the text box. The textboxe's value must be put in a cell or variable before that line is executed

    Comment


    • #3
      Re: Capture Data From Sheet To Show In Userform

      It works, thank you very much.

      Comment


      • #4
        Re: Capture Data From Sheet To Show In Userform

        Glad to have helped.

        Comment

        Working...
        X