View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave D-C[_3_] Dave D-C[_3_] is offline
external usenet poster
 
Posts: 176
Default Delete Row and Auto Subtract

This has a lot of extra goodies (baggage?), but it
shows one approach -- using a popup menu.
(Assumes InvNum and Qty are columns A & B)
(It needs error checking to be added) Dave D-C
In Module1:
Option Explicit
Public gSwCancel As Boolean

Sub MakePopup(pCBName$)
gSwCancel = True
Dim CB1 As CommandBar, CBC1 As CommandBarControl
On Error Resume Next
Application.CommandBars(pCBName).Delete
On Error GoTo 0 ' restore error processing
Set CB1 = Application.CommandBars.Add(pCBName, msoBarPopup, False,
True)
End Sub

Sub MakePUButton(pCBName$, pCaption$, pOnAction$)
Dim CB1 As CommandBar, CBC1 As CommandBarControl
Set CB1 = Application.CommandBars(pCBName)
If pCaption = "" Then
Set CBC1 = CB1.Controls(CB1.Controls.Count)
CBC1.BeginGroup = True
Else
Set CBC1 = CB1.Controls.Add(msoControlButton)
CBC1.Style = msoButtonCaption
CBC1.Caption = pCaption
CBC1.OnAction = pOnAction
End If
End Sub

In Sheet2:
Option Explicit
Dim gRow&

Private Sub Worksheet_BeforeRightClick( _
ByVal Target As Excel.Range, Cancel As Boolean)
If Target.Rows.Count < 1 Then Exit Sub
gRow = Target.Row
Call MakePopup("DC1") ' sets gSwCancel = True
Call MakePUButton("DC1", "DelRow", "Sheet2.DelRow")
Call MakePUButton("DC1", "True", "Sheet2.MenuTrue")
Call MakePUButton("DC1", "", "") ' begingroup
Call MakePUButton("DC1", "False", "Sheet2.MenuFalse")
Application.CommandBars("DC1").ShowPopup
Cancel = gSwCancel
End Sub

Sub DelRow() ' ** This is the meat of it **
Const ShName = "Sheet1" ' other sheet
Dim iRow&, InvNum$, Qty&
iRow = Sheets(ShName).Range("A:A").Find(Cells(gRow, 1)).Row
InvNum = Cells(gRow, 1)
Qty = Sheets(ShName).Cells(iRow, 2) - Cells(gRow, 2)
Sheets(ShName).Cells(iRow, 2) = Qty
Rows(gRow).Delete
MsgBox "New Qty for " & InvNum & " is " & Qty
End Sub

Sub MenuTrue()
Beep
' gSwCancel = True ' already True
End Sub

Sub MenuFalse()
Beep
gSwCancel = False
End Sub

hon123456 wrote:
Dear All,
I have two worksheet with the following data,

Sheet 1
Invoice Number Qty
Invoice-001 6

Sheet 2
Invoice Number Qty
Invoice-001 1

Now what I want is if I delete the Invoice-001 row in Sheet2,
the Qty of Invoice-001 in Sheet1
will be subtracted by the Qty of Invoice-001 of Sheet2. How Can I do
that?