Subtracting using VBA Userform

Strugglin Exceller

New Member
Joined
Mar 9, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
So I have two userforms. one to add a quantity to inventory and one to subtract. In the userform, when you look up the part number. it will fill in a description of the part and the amount in inventory. If you enter a quantity in the add Field. it will take the number in inventory and add the quantity you are adding to the existing amount. My code works great for adding.

So i used the same code for removing inventory and it does a whacky calculation. if there are 200 in inventory and I want to remove a hundred. it changes the new total to -100


This is my code to add
VBA Code:
Private Sub SubmitAdd_Click()
    Dim Part_No As Long
    Part_No = PartNo.Text  
    lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row    
    For I = 2 To lastrow
       If Sheet1.Cells(I, 1).Value = Part_No Then
          Sheet1.Cells(I, 32).Value = QtyAdd.Text + Sheet1.Cells(I, 32).Value          
    MsgBox "Your inventory quantity has been updated to " & Sheet1.Cells(I, 32).Value        
    End If
Next
Unload AddQtyToInv
End Sub


This is my code to remove
VBA Code:
Private Sub SubmitRemove_Click()
    Dim Part_No As Long
    Part_No = PartNo.Text    
    lastrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row    
    For I = 2 To lastrow
       If Sheet1.Cells(I, 1).Value = Part_No Then
           Sheet1.Cells(I, 32).Value = QtyRemoved.Text - Sheet1.Cells(I, 32).Value          
    MsgBox "Your inventory quantity has been updated to " & Sheet1.Cells(I, 32).Value
        End If
Next
Unload RemoveQtyFromInv
End Sub

You can see in the uploaded image what it's doing. I'm guessing I'm just overlooking something really simple
Thanks in advance for your help
 

Attachments

  • 2023-07-12 21_19_35-Master Database.xlsb - Excel.jpg
    2023-07-12 21_19_35-Master Database.xlsb - Excel.jpg
    73.4 KB · Views: 11

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
If this Sheet1.Cells(I, 32).Value is 200
and QtyRemoved.Text is 100 then you're saying
Sheet1.Cells(I, 32).Value = 100 - 200 which of course, is -100
You want to subtract the removed from the current count, not subtract the current count from the removed.
 
Upvote 0
If this Sheet1.Cells(I, 32).Value is 200
and QtyRemoved.Text is 100 then you're saying
Sheet1.Cells(I, 32).Value = 100 - 200 which of course, is -100
You want to subtract the removed from the current count, not subtract the current count from the removed.
Micron...I agree that is the case. And I forgot to add that bit of info. I tried to reverse the order. it throws an error.

Sheet1.Cells(I, 32).Value - Sheet1.Cells(I, 32).Value=Qtyremoved.text

When I add this line. it puts the Minus right against the sheet name.
 
Upvote 0
The "something equals" must come first. Try
Sheet1.Cells(I, 32)=Sheet1.Cells(I, 32)-QtyRemoved.Text
Otherwise if the stock count was 500 and the removed is 100 you're saying
500-500=100
 
Last edited:
Upvote 0
Solution
The "something equals" must come first. Try
Sheet1.Cells(I, 32)=Sheet1.Cells(I, 32)-QtyRemoved.Text
Otherwise if the stock count was 500 and the removed is 100 you're saying
500-500=100
Thanks, Micron. I'll try this. Unfortunately when I opened my sheet this morning. it said it was corrupt and that it lost the active X control. I check all the backup versions and appear to have been corrupted a couple of days ago. this is a brand-new sheet. So not sure what happened. But now I have to recreate everything from scratch. this may take me a bit before I can test your suggestion.
 
Upvote 0
Sorry to learn of your troubles. Good luck!
 
Upvote 0
Glad I could help. I usually say thanks for the recognition as well, but you marked your post as the solution.;)
 
Upvote 0
Back up and running. Your suggestion worked! thank you for your help!!!!!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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