Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Show Total row in listbox based on search separated range

0

Hello

I  search  for  data for  separated range  by combox1: combobox3 ,  then  will show  the  data in  listbox ,  but  the  problem  doesn't  show  the  TOTAL row  for  relating  with separated range , how  can  I  make TOTAL  row show  in listbox when  search  for  data?

Answer
Discuss

Discussion

Hi Malkal.

Sorry but once again I'm struggling with the wording of your question.

If I enter these values in the ComboBoxes:
CSS-100   INV-A123   ITTT-100/AS-2
The ListBox shows filtered results like this:
1/12/2022     CSS-100  INV-A123     ITTT-100/AS-2 300
 1/2/2023     CSS-100  INV-A123     ITTT-100/AS-2 100
5/1/2023     CSS-100  INV-A123     ITTT-100/AS-2 100

Where do you want the Total value to appear (and why, against individiual line items)?

Please edit your question to explain and I'll try to answer tomorrow..
John_Ru (rep: 6142) Jan 16, '23 at 1:03 pm
Hi john 
as  show  in sheet  ,also should  show  in listbox   in first  column   into  listbox shows for  the  lastrow  should  show TOTAL  word  and  the  value  in  last  column in listbox for  the  lastrow   =200 as  your  example .
actually  I  try  to  understand  this  
why, against individiual line items?
I don't  understand  it  to  answer  you , sorry!
Malkal (rep: 22) Jan 16, '23 at 2:07 pm
I still don't understand, sorry. For example I don't know where the 200 total comes from (given row quantities of 300, 100 and 100)!

Please add a sheet to your file to show how the totals should appear (using my ComboBox entries above) .
John_Ru (rep: 6142) Jan 16, '23 at 2:53 pm
 I don't know where the 200 total comes from (given row quantities of 300, 100 and 100)!
sould sum fro third  row  for  each  range  then  based  on  your  example (100+100=200)   not (300+100+100=400)   as  you  think  , strange case  righ ?  yes  it's  because  I  have  complicated  case .
I  attach  picture  in third  sheet  with  example  first  range , third  range .
Malkal (rep: 22) Jan 17, '23 at 12:45 am
Malkal. You say "not (300+100+100=400)" but conventional maths adds this way (300+100+100=500)!

From your examples, it seems:

1. If ComboBox3 produces a Listbox with one row, the Total is that row
2. If ComboBox3 produces a Listbox with more than one row, the Total is the sum of rows EXCLUDING the first row.

Is this what you want? (If so, this is possible but makes no sense to me and probably no sense to anyone who works routinely with numbers- I think you are making it complicatedto be honest!). 
John_Ru (rep: 6142) Jan 17, '23 at 6:55 am
Also, I asked you to show the result by replicating the ComboBox selections I made (but you didn't).
John_Ru (rep: 6142) Jan 17, '23 at 6:57 am
but conventional maths adds this way (300+100+100=500)!
sorry  about  calculation mistake , yes that's right

Is this what you want? 
yes
(If so, this is possible but makes no sense to me and probably no sense to anyone who works routinely with numbers- I think you are making it complicatedto be honest!). 

I  know  this  is  not  usual way  to  sum  with  for  this  way  but  I  need this  way  despite  of  this  is  complicated  way

Also, I asked you to show the result by replicating the ComboBox selections I made (but you didn't).
ok I updated  the  file
Malkal (rep: 22) Jan 17, '23 at 7:53 am
Add to Discussion

Answers

0
Selected Answer

Malkal

You've asked for a strange "total" in your ListBox where:

  1. If ComboBox3 produces a Listbox with one row, the "total" is that row
  2. If ComboBox3 produces a Listbox with more than one row, the "total" is the sum of rows EXCLUDING the first row.

In the attached file, I've modified the sub FilterData (as shown in bold below) so that:

  1. A new variable FTotal collects that "total" where data matches
  2. If all Comboxes are completed (and there's filtered data), a Total is displayed as the last row of the ListBox (with a note, if row 1 is omitted)
  3. (As an extra) the date column is formatted in dd mmm yyyy for less-ambiguous reading.
Sub FilterData()
  Dim txt1 As String, txt2 As String, txt3 As String
  Dim i As Long, j As Long, k As Long
  Dim FTotal As Double

  FTotal = 0
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))

  For i = 1 To UBound(a)
    If ComboBox1 = "" Then txt1 = a(i, 2) Else txt1 = ComboBox1
    If ComboBox2 = "" Then txt2 = a(i, 3) Else txt2 = ComboBox2
    If ComboBox3 = "" Then txt3 = a(i, 4) Else txt3 = ComboBox3
    If LCase(a(i, 2)) Like LCase(txt1) & "*" And _
       LCase(a(i, 3)) Like LCase(txt2) & "*" And _
       LCase(a(i, 4)) Like LCase(txt3) & "*" Then

      k = k + 1
      'For j = 1 To 5
      ' change loop to make first entry a date format
      b(k, 1) = Format(a(i, 1), "dd mmm yyyy")
      ' then collect other values
      For j = 2 To 5
        b(k, j) = a(i, j)
      Next j

        ' update FTotal
        If k <= 2 Then
            If IsNumeric(a(i, 5)) Then FTotal = a(i, 5)
            Else
            If IsNumeric(a(i, 5)) Then FTotal = FTotal + a(i, 5)
        End If

    End If


  Next i
    If k > 0 Then ListBox1.List = b

    ' only if all boxes are complete...
    If ComboBox1 <> "" And ComboBox2 <> "" And ComboBox3 <> "" And k > 0 Then
      ' ...add the total row
      ListBox1.addItem
      If k = 1 Then
        ListBox1.List(k, 0) = "Total"
        Else
        ListBox1.List(k, 0) = "Total (less first row)"
        End If
      ListBox1.List(k, 4) = FTotal
    End If

End Sub

I notice that your entries in worksheet DETAILS do not match all combinations in DATA (but leave you to correct that / check other combinations work).

Hope this works well for you.

Discuss

Discussion

Glad that worked for you, Malkal. Thanks for selecting my Answer.
John_Ru (rep: 6142) Jan 17, '23 at 10:11 am
Hi john 
well done !
sorry about  strange requiremnt  unusually.
to  clarification :
first  row  for  each  range  will brings  from  DATA sheet  I just  give  you three ranges  to  understand  my  requirement  and  after  first  row  will enter data  for  each  range by  another  userform (isnot  relating  DATA  sheet). so  the  data entering  under  frist row(from second row) represent  the  out quntity for  each  invoice until finish QTY  with different dates  then  will  sum  the  quantity for   data entering  under  frist row(from second row). what  shows in  TOTAL   row  should  be  the  same  first  row  ,then the  data enteing for  QTY  are  correct , if   the  value in  TOTAL   row bigger than  first  row then  there is  error for Exceeding the amount allowed for the invoice as in the first row (this  will  help  me  to  find  error)  if   the  value in  TOTAL   row smaller than  first  row  then  there   A residual value is still allowed to be deducted when compared to the first row I  no  know  if  you  convinced  with  this 
thanks  very  much  for  your  help .
Malkal (rep: 22) Jan 17, '23 at 10:24 am
Malkal. I think I nearly underand your logic but the conventional way is to use a single column with stock as positive values and "out" (or sold) as negative values and a normal sum. That or put stock and "out" in separate columns, subratcing one from another. 

So long as it works for you, the solution is good for you.
John_Ru (rep: 6142) Jan 17, '23 at 10:34 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login