Forum Discussion

officeamuz's avatar
officeamuz
Copper Contributor
Feb 09, 2023

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:

AB
IDName
1Product 1
2Product 2
3Product 3

 

this is invoice sheet:

ABCDEFG
RecFactorProductNumQtyPriceDescDate
11112250000 1401/10/16
121215000000 1401/10/16
21215250000 1401/11/17
222225000000 1401/11/17
31315250000 1401/11/25
323235000000 1401/11/25
414215500000 1401/11/30
42412250000 1401/11/30
51514250000 1401/12/05
616215000000 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

No RepliesBe the first to reply

Resources