subtraction items between two sheets when show data in listbox on userform

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
240
Office Version
  1. 2019
  2. 2010
Hi experts
I hope some body help by adding some lines to achieve my project.
based on the orginal code will populate data in listbox based on sheet PURCHASE and when search item in textbox1 will poulate data based on column D (column(4) in listbox)
now I add another sheet name is SALES when show the data in listbox should subtract the values for columns H,J between sheets(PURCHASE,SALES) based on the item in column D
also when search item in textbox1 based on column D should subtract between two sheets.
search on userform (2) (1).xlsm
ABCDEFGHIJ
1DATE CLIENT NOINVOICE NOID-CCBB-RTT-YOR-GGQTYPRICETOTAL
21/5/2021CC2-TRB-1INV-TRGG-1IDTR-100FFDOOD-1PACK-2TTR60.00120.007,200.00
31/6/2021CC2-TRB-1INV-TRGG-1IDTR-101FFDOOD-2PACK-3BBR10.00130.001,300.00
41/7/2021CC2-TRB-1INV-TRGG-1IDTR-102FFDOOD-3PACK-4LLR5.00100.00500.00
51/8/2021CC2-TRB-1INV-TRGG-1IDTR-103FFDOOD-4PACK-5MMR10.00289.002,890.00
61/9/2021CC2-TRB-2INV-TRGG-2IDTR-104FFDOOD-5PACK-6ASDR5.00140.00700.00
71/12/2021CC2-TRB-2INV-TRGG-2IDTR-107FFDOOD-6PACK-7FFG12.00200.002,400.00
81/13/2021CC2-TRB-2INV-TRGG-2IDTR-108FFDOOD-7PACK-8GGF13.00100.001,300.00
PURCHASE
Cell Formulas
RangeFormula
J2:J8J2=H2*I2


search on userform (2).xlsm
CDEFGHIJ
1INVOICE NOID-CCBB-RTT-YOR-GGQTYPRICETOTAL
2INV-TRGG-1IDTR-100FFDOOD-1PACK-2TTR12.00122.001,464.00
3INV-TRGG-1IDTR-101FFDOOD-2PACK-3BBR5.00135.00675.00
4INV-TRGG-1IDTR-102FFDOOD-3PACK-4LLR2.00110.00220.00
SALES
Cell Formulas
RangeFormula
J2:J4J2=H2*I2


what I expect in listbox on userform
case1 when run the userform
1.PNG


see the selected items how changes the values in columns 9,10 in listbox , but I don't want to show column price in listbox

when put item in textbox1 based on column D ,also I don't want to show column price in listbox
2.PNG

code
VBA Code:
Private Sub TextBox1_Change()
  Call FilterData
End Sub

Private Sub TextBox2_Change()
  
  Call FilterData
 
End Sub

Private Sub TextBox3_Change()

  Call FilterData
  

End Sub

Sub FilterData()
    Dim i As Long, ii As Long, n As Long
    Me.ListBox1.List = a
    If Me.TextBox1 = "" Then Exit Sub
    With Me.ListBox1
        .Clear
        For i = 0 To UBound(a, 1)
            If UCase$(a(i, 3)) Like UCase$(Me.TextBox1) & "*" Then
                .AddItem
                .List(n, 0) = n + 1
                For ii = 1 To UBound(a, 2)
                    .List(n, ii) = a(i, ii)
                Next
                n = n + 1
            End If
        Next
    End With
    Dim r As Long
       Dim MySum, MySum1  As Double

        MySum = 0
        MySum1 = 0
        With ListBox1
            For r = 0 To .ListCount - 1
                MySum = MySum + .List(r, 7)
                MySum1 = MySum1 + .List(r, 9)
            Next r
        End With

        TextBox2.Value = Format(MySum, "#,##0.00")
        TextBox3.Value = Format(MySum1, "#,##0.00")
        
End Sub

Private Sub UserForm_Activate()
      
      

End Sub
Private Sub UserForm_Initialize()
    Dim lindex&
    Dim rngDB As Range, rng As Range
    Dim i, myFormat(1) As String
    Dim sWidth As String
    Dim vR() As Variant
    Dim n As Integer
    Dim myMax As Single
    Set rngDB = Range("A2:J20")
    For Each rng In rngDB
        n = n + 1
        ReDim Preserve vR(1 To n)
        vR(n) = rng.EntireColumn.Width
    Next rng
    myMax = WorksheetFunction.Max(vR)
    For i = 1 To n
        vR(i) = myMax
    Next i
    With Sheets("purchase").Cells(1).CurrentRegion
        myFormat(0) = .Cells(2, 8).NumberFormatLocal
        myFormat(1) = .Cells(2, 9).NumberFormatLocal
        Set rng = .Offset(1).Resize(.Rows.Count - 1)
        a = .Cells(1).CurrentRegion.Value
    End With

    sWidth = Join(vR, ";")
    Debug.Print sWidth
    With ListBox1
        .ColumnCount = 10
        .ColumnWidths = sWidth '<~~ 63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63
        .List = rng.Value
        .BorderStyle = fmBorderStyleSingle
        For lindex = 0 To .ListCount - 1
            '.List(lindex, 0) = (Format((.List(lindex, 0)), "dd/mm/yyyy"))   ' BL = dates
                        .List(lindex, 0) = lindex + 1

            .List(lindex, 7) = Format$(.List(lindex, 7), myFormat(0))
            .List(lindex, 8) = Format$(.List(lindex, 8), myFormat(1))
            .List(lindex, 9) = Format$(.List(lindex, 9), myFormat(1))
        Next
       
        a = .List
        '<--- this line
    End With
End Sub

thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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