+ Reply to Thread
Results 1 to 12 of 12

Choose from different balances in VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Choose from different balances in VBA

    The result should be as follows:

    textbox1 shows the remaining hours of box1 [i.e. 18]
    textbox2 shows the remaining hours of box2 [i.e. 32]
    textbox3 shows the remaining hours of box3 [i.e. 8,5]

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Choose from different balances in VBA

    Hi Jonathan

    Hold off on that...needs a small correction!!!

    Add these few lines of code
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim LastRow As Long
        Dim x As Double
        Dim y As Double
        Dim z As Double
        Dim zz As Double
    
        '-------------------------------------
        ' Add these lines of code
        If Me.TextBox4 <= Me.TextBox1 Then
            Me.TextBox1 = Me.TextBox1 - Me.TextBox4
            Exit Sub
        End If
        '-------------------------------------
         
        x = Me.TextBox4 - Me.TextBox1
    
        If x >= 0 Then
            y = x
            Me.TextBox1 = 0
        Else
            Me.TextBox1 = Me.TextBox1 - x
        End If
    
        If y >= 0 And y >= Me.TextBox2 Then
            z = Me.TextBox2 - y
            zz = z
            Me.TextBox2 = 0
        Else
            Me.TextBox2 = Me.TextBox2 - y
        End If
    
        If zz <= 0 Then
            Me.TextBox3 = Me.TextBox3 + zz
        End If
    
        LastRow = Sheets("Sheet1").Range("K65536").End(xlUp).Row + 1
    
        Sheets("Sheet1").Range("K" & LastRow).Value = Me.TextBox1.Value
        Sheets("Sheet1").Range("K" & LastRow + 1).Value = Me.TextBox2.Value
        Sheets("Sheet1").Range("K" & LastRow + 2).Value = Me.TextBox3.Value
    
    End Sub
    Last edited by jaslake; 05-01-2012 at 11:22 AM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Choose from different balances in VBA

    Hi Jonathan

    Try this instead
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim LastRow As Long
        Dim x As Double
        Dim y As Double
        Dim z As Double
        Dim zz As Double
    
        '-------------------------------------
        ' Add these lines of code
        If Val(Me.TextBox4) <= Val(Me.TextBox1) Then
            Me.TextBox1 = Me.TextBox1 - Me.TextBox4
            GoTo EarlyExit
        End If
        '-------------------------------------
    
        x = Me.TextBox4 - Me.TextBox1
    
        If x >= 0 Then
            y = x
            Me.TextBox1 = 0
        Else
            Me.TextBox1 = Me.TextBox1 - x
        End If
    
        If y >= 0 And y >= Me.TextBox2 Then
            z = Me.TextBox2 - y
            zz = z
            Me.TextBox2 = 0
        Else
            Me.TextBox2 = Me.TextBox2 - y
        End If
    
        If zz <= 0 Then
            Me.TextBox3 = Me.TextBox3 + zz
        End If
    
        '-------------------------------------
        ' Add these lines of code
    EarlyExit:
        '-------------------------------------
    
        LastRow = Sheets("Sheet1").Range("K65536").End(xlUp).Row + 1
    
        Sheets("Sheet1").Range("K" & LastRow).Value = Me.TextBox1.Value
        Sheets("Sheet1").Range("K" & LastRow + 1).Value = Me.TextBox2.Value
        Sheets("Sheet1").Range("K" & LastRow + 2).Value = Me.TextBox3.Value
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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