Consulting

Results 1 to 8 of 8

Thread: User Form

  1. #1
    VBAX Regular
    Joined
    Feb 2011
    Posts
    17
    Location

    User Form

    Hi all

    I'm writing a code for a user form in VBA excel.

    I have two columns of text boxes, and what I would like is to enter dates in the left column, and then that date minus two dates, should appear automatically in the right column but I want to be able to amend that manually.

    I tried

    If IsDate(TextBox8.Text) Then

    TextBox8.Text = CDate(TextBox28.Text) - 2

    End If

    but that doesn't seem to work. any ideas?

    thanks

  2. #2
    VBAX Regular
    Joined
    Dec 2010
    Posts
    14
    Location
    What does the "-2" mean? minus 2 days?

    you can define something as "date"

    dim mydate as date

    and then add or remove days by function

    mydate - days(2) or some other sort

  3. #3
    What about (note I changed the textbox numbers too!):

    If IsDate(TextBox8.Text) Then

    TextBox28.Text = Format(CDate(TextBox8.Text) - 2,"dd/mm/yyyy")

    End If
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  4. #4
    VBAX Regular
    Joined
    Feb 2011
    Posts
    17
    Location
    Hi
    i tried to do it as you said

    I put the function in a new Sub right? Or do I have to write it in the code of the userform itself??

    so I have:


    Private Sub TextBox8()

    If IsDate(TextBox8.Text) Then

    TextBox28.Text = Format(CDate(TextBox8.Text) - 2, "dd/mm/yyyy")

    End If

    End Sub


    Unfortunately this still doesn't work for me. When I enter the date, nothing appears in the other text box. Do I have to call the function or something?
    And I don't have to put anything in for the dd/mm/yyyy right? because this will always depend on what's written in the first column.
    thanks!

  5. #5
    You have not created the proper change event routine for the textbox.
    Doubleclick on the textbox in the userform in the VBA editor. The editor should open a new event stub for the default Change event of the textbox. Put the code there. Remove the routine you posted as it will not do anything.

    Once in the code window behind a form you can use the two dropdowns at the top of the window. The left one has all objects in the form and after selecting one of them, the one on the right shows the available events that object has to offer. Selecting an object usually makes the VBA editor insert the default event for that control automatically, remove it if you don't need it.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  6. #6
    VBAX Regular
    Joined
    Feb 2011
    Posts
    17
    Location
    Hey

    thanks for your answer.
    I entered it in the code for of the textbox in which the date should appear. it still doesnt work though, when I enter a date in the first column.

    the dropdown menu I left on "Change". is that correct??

    thank you!

  7. #7
    Your code should look like this:

    Private Sub TextBox8_Change()
        If IsDate(TextBox8.Text) Then
            TextBox28.Text = Format(CDate(TextBox8.Text) - 2, "dd/mm/yyyy")
        End If
    End Sub
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  8. #8
    VBAX Regular
    Joined
    Feb 2011
    Posts
    17
    Location
    Hi!

    thanks a lot! it works now!!

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •