Forum Discussion
Problem wit setting two different ranges on visible rows, with different filters.
Hi,
I m trying to create a vba file in excel to manage sales
I have a table to save Products and one for Invoice
To calculate total price for all sales in my report userform, I Use worksheetfunction.sumproduct.... and it works well.
Then I want to get the Total value in special period of time. So I filter my Invoice sheet, and set a range on visible cells, and use worksheetfunction.sumproduct.... in my Range and it works well again...
But Now I want to add new condition, for product cod. again, I filter my Invoice sheet, and set another range on visible cells, and use worksheetfunction.sumproduct.... this time the second range, just get one record. But the filtered sheet has more records visible.
hope I could explain the problem, correctly.
Thank you for your help.
More information:
This is Product Sheet:
A | B |
ID | Name |
1 | Product 1 |
2 | Product 2 |
3 | Product 3 |
this is invoice sheet:
A | B | C | D | E | F | G |
Rec | Factor | ProductNum | Qty | Price | Desc | Date |
11 | 1 | 1 | 2 | 250000 | 1401/10/16 | |
12 | 1 | 2 | 1 | 5000000 | 1401/10/16 | |
21 | 2 | 1 | 5 | 250000 | 1401/11/17 | |
22 | 2 | 2 | 2 | 5000000 | 1401/11/17 | |
31 | 3 | 1 | 5 | 250000 | 1401/11/25 | |
32 | 3 | 2 | 3 | 5000000 | 1401/11/25 | |
41 | 4 | 2 | 1 | 5500000 | 1401/11/30 | |
42 | 4 | 1 | 2 | 250000 | 1401/11/30 | |
51 | 5 | 1 | 4 | 250000 | 1401/12/05 | |
61 | 6 | 2 | 1 | 5000000 | 1401/12/10 |
(Dates are in number format ("0000\/00\/00") because excel date functions doesn't work for Persian dates, and I use some modules to manage dates. no problem with finding and filtering on date column)
This is the Code that runs when the product is selected in combobox 1:
Private Sub ComboBox1_Change()
If SD = Empty Then SD = "14010101"
If FD = Empty Then FD = "14011229"
EOR = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
Sheet1.Range("A1:G" & EOR).AutoFilter 7, "<=" & FD, xlAnd, ">=" & SD
Set Rng = Sheet1.Range("A2:G" & EOR).SpecialCells(xlCellTypeVisible)
TextBox1.Text = Format(WorksheetFunction.SumProduct(Rng.Columns(4), Rng.Columns(5)), "#,##0")
TextBox2.Text = Format(WorksheetFunction.Sum(Rng.Columns(4)), "#,##0")
If ComboBox1.ListIndex = -1 Then
TextBox3.Text = ""
TextBox4.Text = ""
Sheet1.AutoFilterMode = False
Exit Sub
End If
Rng.AutoFilter 3, Val(ComboBox1.Value)
Set NewRng = Sheet1.Range("A2:G" & EOR).SpecialCells(xlCellTypeVisible)
TextBox3.Text = Format(WorksheetFunction.SumProduct(NewRng.Columns(4), NewRng.Columns(5)), "#,##0")
TextBox4.Text = Format(WorksheetFunction.Sum(NewRng.Columns(4)), "#,##0")
Sheet1.AutoFilterMode = False
End Sub
Thank you so so Much