+ Reply to Thread
Results 1 to 5 of 5

TextBox value increase +1 or -1 with Command Buttons

Hybrid View

  1. #1
    Registered User
    Join Date
    12-03-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    50

    TextBox value increase +1 or -1 with Command Buttons

    Hello,

    I recently received assistance from the community on how to code two separate command buttons to give a TextBox a value of +1 or -1. Thank you. The code suggested are:

    Private Sub CommandButton2_Click()
    If Me.TextBox75.Value = 0 Then
        Me.TextBox1.Value = 0
    Else
        Me.TextBox75.Value = Me.TextBox75.Value - 1
    End If
    End Sub
    and
    Private Sub CommandButton3_Click()
    Dim oldvalue As Double
    oldvalue = Me.TextBox75.Value
    Me.TextBox75.Value = oldvalue + 1
    End Sub
    However, as I continue to deign the UserForm1 I have more than a dozen Textboxes that require a set of command buttons to do the same +/- event. Not only does the form start looking cluttered but coding can get tedious. My question is: can someone suggest a code(s) that will allow one set of command buttons - commandbutton (+1) and commandbutton2 (-1) - that will affect more than one textbox1-12 WHEN the textbox "gets focus" from a mouse event (click).

    Thank you - Gal403

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: TextBox value increase +1 or -1 with Command Buttons

    See attached

    Private Sub CommandButton1_Click()
        Dim i As Long
        For i = 1 To 12
            Controls("TextBox" & i).Value = Controls("TextBox" & i).Value + 1
        Next i
    End Sub
    Private Sub CommandButton2_Click()
        Dim i As Long
        For i = 1 To 12
            Controls("TextBox" & i).Value = Controls("TextBox" & i).Value - 1
        Next i
    End Sub
    Private Sub UserForm_Initialize()
        Dim i As Long
        For i = 1 To 12
            Controls("TextBox" & i).Value = 0
        Next i
    End Sub
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    12-03-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: TextBox value increase +1 or -1 with Command Buttons

    Wow! That was fast, Jolivanes. The sample is pretty cool. However, I guess I didn't describe my situation clearly. Using the sample file you created what I desire is to have only one of the textboxes, when selected by a mouse click(), to either get a -1 or +1 when the respective commandbutton is pressed. The way it's coded now it appears that ALL textboxes are affected by the click of the -1 or +1 command button. Is it possible to create a code with only one set of command buttons that will focus on a single textbox?

  4. #4
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: TextBox value increase +1 or -1 with Command Buttons

    Hi,

    Here is another one. Hope this helps.
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  5. #5
    Registered User
    Join Date
    12-03-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    50

    Re: TextBox value increase +1 or -1 with Command Buttons

    This sample is AMAZING! Thank you, 0 xlbiznes. It's exactly what I want the code to do. Give me a few days to analyze this and if I have any more question I'll post it on this thread. Very nice !

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Useform Code - 12 Command Buttons dependant on other command buttons
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2013, 10:58 AM
  2. Master Command Button to call on other Command Buttons located in other Workbooks
    By MrNickRegan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2013, 07:57 PM
  3. Code for a master command button to change the backcolor of multiple command buttons?
    By panttherm5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2012, 10:11 PM
  4. Replies: 3
    Last Post: 05-07-2012, 09:46 PM
  5. Linking command buttons to option buttons
    By sealtite01 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-20-2012, 12:25 PM

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