adapting code to merge amount for duplicates item on usrform

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi
I would adapt @DanteAmor's code
VBA Code:
Option Explicit
Dim a As Variant

Private Sub TextBox1_Change()
  Call FilterData
End Sub



Sub FilterData()
  Dim txt1 As String, txt2 As String, txt3 As String
  Dim i As Long, j As Long, k As Long
 
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    If TextBox1 = "" Then txt1 = a(i, 4) Else txt1 = TextBox1
     Then
      k = k + 1
      For j = 1 To 7
        b(k, j) = a(i, j)
      Next
    End If
  Next
  If k > 0 Then ListBox1.List = b
End Sub

Private Sub UserForm_Activate()
  a = Sheets("purchase").Range("A2:G" & Sheets("purchase").Range("D" & Rows.Count).End(3).Row).Value
End Sub
I would when I write item in txtbox1 then should merge amount in column G for duplicates item.
and change the date column A to auto srial 1,2,3 when merging. but when clear then will return the original data as the original code does it.
thanks
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
here is data
ABDO1.xlsm
ABCDE
1DATE CLIENT NOINVOICE NOBRANDBALANCE
201/05/2021CUS-BS-1INV-BS-11200R20 G580 JAP95.00
301/06/2021CUS-BS-1INV-BS-11200R20 G580 THI10.00
401/07/2021CUS-BS-1INV-BS-11200R20 G580 JAP5.00
501/08/2021CUS-BS-1INV-BS-11200R20 R187 THI10.00
601/09/2021CUS-BS-2INV-BS-21200R24 G580 JAP5.00
701/10/2021CUS-BS-2INV-BS-21200R20 G580 JAP30.00
801/11/2021CUS-BS-2INV-BS-21200R20 G580 THI50.00
901/12/2021CUS-BS-2INV-BS-21400R20 VSJ JAP12.00
1002/12/2021CUS-BS-2INV-BS-21400R20 R180 JAP13.00
purchase


and what I want when search in textbox based on column D
1.JPG
 
Upvote 0
would when I write item in txtbox1 then should merge amount in column G for duplicates item.
Based on your example, it should be column "E".

And when is it duplicated? when the value in column D is repeated or when the values in columns B, C, and D are repeated.
Because if it is when the value of column D is repeated, there is no point in adding the data of columns B and C, because they are different, for example:

1721834241825.png


In the previous example (key only column D), the mark is the same, the sum is 130, but the customer and invoice data are different, but if it is accumulated in a single record, only one customer and one invoice will appear:

1721834396937.png



If you use the key from columns 2, 3 and 4:
1721834531595.png



Try:
VBA Code:
Option Explicit
Dim a As Variant

Private Sub TextBox1_Change()
  Call FilterData
End Sub

Sub FilterData()
  Dim txt1 As String, txt2 As String, txt3 As String
  Dim i As Long, j As Long, k As Long, y As Long
  Dim dic As Object
  Dim ky As Variant
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    If TextBox1 = "" Then txt1 = a(i, 4) Else txt1 = TextBox1
    
    If UCase(a(i, 4)) Like "*" & UCase(txt1) & "*" Then
      ky = a(i, 4)
     
      If Not dic.exists(ky) Then
        k = k + 1
        y = y + 1
        dic(ky) = y
      End If
      
      y = dic(ky)
      
      b(y, 1) = y
      b(y, 2) = a(i, 2)
      b(y, 3) = a(i, 3)
      b(y, 4) = a(i, 4)
      b(y, 5) = b(y, 5) + a(i, 5)
    End If
  Next
  If k > 0 Then ListBox1.List = b
End Sub

Private Sub UserForm_Activate()
  a = Sheets("purchase").Range("A2:G" & Sheets("purchase").Range("D" & Rows.Count).End(3).Row).Value
End Sub

If you want the extended key, change this line:
ky = a(i, 4)

By this:
ky = a(i, 2) & "|" & a(i, 3) & "|" & a(i, 4)


Regards
Dante Amor
 
Upvote 0
Solution
Hi Dante again ,
sorry about confusion and post some errors in details🙏🙏
both two suggestions works perfectly .
many thanks buddy.;)
 
Upvote 1

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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