+ Reply to Thread
Results 1 to 16 of 16

TextBox Change Event HELP

  1. #1
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    TextBox Change Event HELP

    Hello Excel VBA experts,

    I have created a textbox (activex control) that I would like to work with my calculation. Notice that, the code below, I want users to enter only numerical numbers (including decimals...e.g. 25.50) and if it's really numerical, the textbox will show it in percentage format with 2 decimal places (e.g. 25.50%). However, whenever I try to type it more than 2 digits, it always goes to the 2nd IF statement where the msgbox is. The reason too why I want it to be a 'change' event so that I could see instantly of the calculation when the user input whatever numbers.

    Your help is greatly appreciated!

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by dluhut; 02-06-2013 at 03:10 PM. Reason: Attached file

  2. #2
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: TextBox Change Event HELP

    you type procedure in "change" event so when You type something procedure starting because its value is change.
    Then in procedure in IF's you change value of TextBox so it is change again and again

    try to use other event (maybe "AfterUpdate") or change your idea for prevent starting change sub all the time when any value will be typed
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  3. #3
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: TextBox Change Event HELP

    Can you attach the workbook that you currently work on?

  4. #4
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    @ JieJenn, I have attached the file that I'm working (in the first post after I've edit). Forgive me if you see the code in mess, since I'm not a programmer.

    @ MaczaQ, How would you do this then, if you were me. Basically, trying to accomplish these few things.
    1. Instantaneously change the amount when user input.
    2. User able to input with decimals (i.e. 25.2)
    3. Show '%' sign after user have finish input the data in the textbox.

  5. #5
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    If the above 3 requirements are not able to met, then I'm willing to take out the '%' sign. Which means point 1 and 2 has to be met

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    Public oldvalue As Double

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

    temp = TextBox1.Value
    If Not IsNumeric(temp) Then temp = oldvalue
    oldvalue = temp

    TextBox1.Value = Format(oldvalue / 100, "0.00%")

    End Sub

    Private Sub UserForm_Activate()
    If IsNumeric(TextBox1.Value) Then
    oldvalue = TextBox1.Value
    Else
    oldvalue = 0: TextBox1.Value = Format(0, "0.00%")
    End If
    End Sub

  7. #7
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    @Mehmetcik. Thank for the help. Although I'm not an expert with VBA, however, from the 2nd code, it seems like you assume I'm using a form. But I don't. I'm using the ActiveX control TextBox. I have attached the spreadsheet for reference

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    No Spreadsheet Seen.

  9. #9
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    The following seems to work.

    I created the textbox using "Developer - Insert - Textbox"

    I opened the revelent macro module by clicking on Design Mode then right clicking on the text box and selecting view code.
    Paste the text below into that window.

    Deselect Design mode.

    ENJOY.



    Public oldvalue As Double
    Private Sub TextBox1_Change()
    temp = TextBox1.Value
    If Right(temp, 1) = "%" Then temp = Left(temp, Len(temp) - 1)
    If Not IsNumeric(temp) Then temp = oldvalue
    oldvalue = temp

    TextBox1.Value = Format(oldvalue / 100, "0.00%")
    End Sub

  10. #10
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    Hi Mehmetcik,

    Have tried copying the latest code and still doesn't work.

    Did you really not see the attached file? It was on the very first post of this thread.

  11. #11
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    Ok I've modified your code for textbox1.

    As follows:-

    Private Sub TextBox1_Change()

    If flag = True Then GoTo 200

    temp = TextBox1.Value
    If Right(temp, 1) = "%" Then temp = Left(temp, Len(temp) - 1)


    If Len(Me.TextBox1) >= 1 And Not IsNumeric(temp) Then

    MsgBox "Sorry, only numbers allowed"

    TextBox1.Value = vbNullString
    ActiveSheet.Label1.Caption = ""
    ActiveSheet.Label12.Caption = ""
    temp = 36

    Else: flag = Not flag
    If flag = True Then TextBox1.Value = Format(temp / 100, "0.00%")


    20 End If

    If Range("C16").Value = 0 Then
    ActiveSheet.Label49.Caption = ""
    ElseIf Range("C16").Value >= 50 Then
    With ActiveSheet.Label49
    .Caption = Format(Range("E16").Value, "$#,#.00")
    .Font.Size = 48
    End With
    Else
    With ActiveSheet.Label49
    .Caption = "Min 50 Phones."
    .Font.Size = 35
    End With
    End If

    GoTo 300

    200 flag = False

    ActiveSheet.Label1.Caption = Range("D3")
    ActiveSheet.Label12.Caption = Format(Range("E3").Value, "$#,#.00")
    300 ' exit

    End Sub

  12. #12
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    Hi Mehmetcik,

    Sorry for not being clear...instead of TextBox1, is it possible to look at "TextBox_MCDDisc"? It is on that box, where users will be inputting the numerical (with decimals included) and then transform it to "%"...I've tried to change your code from TextBox1 to TextBox_MCDDisc and it doesn't work.

  13. #13
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    Ok

    Try This
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    Hi mehmetcik,

    Really appreciate your help.

    Tried it, but give me the following errors.
    1. Tried to type 2 digits (i.e. 30), result error.
    2. Tried to type 4 digits with decimal (i.e. 25.25), result error.

  15. #15
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: TextBox Change Event HELP

    Sorry About That.

    I have modified the macro,so it only runs when you exit the text box. This simplifies the code and it seems to work.
    The code is now in the Lostfocus event
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: TextBox Change Event HELP

    Hi Mehmetcik,

    Although I would like to have it in 'Change' instead of LostFocus, but I would consider this thread to be solved. Thanks for all your help. Please check your reputation as I've increased it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1