Show message box on userform after sum or subtraction

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
175
Office Version
  1. 2019
Platform
  1. Windows
Hello
I got this code by Dante Amore
the code will subtract from column D in sheet and sum to column D in sheet after matching ID in textbox1 with column C based on selected optionbutton
now I want when click commandbutton1 then will show message box and show the available QTY in title message box based inside sheet for column D and show me inside message box how QTY become inside the sheet for column D after sum or subtract inside sheet , but if I write QTY in textbox2 for the ID in textbox1 and bigger than available QTY inside sheet then should show the message by warning and show me available QTY in title message box and show me how remaining QTY after subtract based on optionbutton2,4 and exit sub without subtract from sheet .
QQ.xlsx
ABCD
1ITEMDATEIDQTY
2145102SDFR-100020
3245103SDFR-100120
4345072SDFR-1002200
5445073SDFR-1003201
ASDD

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
 
  'Validations
  With TextBox1
    If .Value = "" Then
      MsgBox "Enter ID"
      .SetFocus
      Exit Sub
    End If
    Set f = Range("C:C").Find(.Value, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      MsgBox "ID does not exists"
      .SetFocus
      Exit Sub
    End If
  End With
  With TextBox2
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter QTY"
      .SetFocus
      Exit Sub
    End If
  End With
 
  'Sum or subtract
  With f.Offset(, 1)
    Select Case True
      Case OptionButton1 Or OptionButton3
        .Value = .Value + Val(TextBox2.Value)
      Case OptionButton2 Or OptionButton4
        .Value = .Value - Val(TextBox2.Value)
      Case Else
        MsgBox "No option was selected"
    End Select
  End With
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
 
  'Validations
  With TextBox1
    If .Value = "" Then
      MsgBox "Enter ID"
      .SetFocus
      Exit Sub
    End If
    Set f = Range("C:C").Find(.Value, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      MsgBox "ID does not exists"
      .SetFocus
      Exit Sub
    End If
  End With
  With TextBox2
    If .Value = "" Or Not IsNumeric(.Value) Then
      MsgBox "Enter QTY"
      .SetFocus
      Exit Sub
    End If
  End With
 
  'Sum or subtract
  With f.Offset(, 1)
  
    Select Case True
      Case OptionButton1 Or OptionButton3
        MsgBox "Available QTY : " & .Value & vbCr & _
               "How QTY become : " & .Value + Val(TextBox2.Value)
        .Value = .Value + Val(TextBox2.Value)
      Case OptionButton2 Or OptionButton4
        If .Value < Val(TextBox2.Value) Then
          MsgBox "Value bigger than available QTY" & vbCr & _
          "Remaining :" & .Value - Val(TextBox2.Value), vbCritical, "Available QTY : " & .Value
          Exit Sub
        Else
          MsgBox "Available QTY : " & .Value & vbCr & _
               "How QTY become : " & .Value - Val(TextBox2.Value)
        End If
        .Value = .Value - Val(TextBox2.Value)
      Case Else
        MsgBox "No option was selected"
    End Select
  End With
End Sub

🤗
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top