filter data based on month or whole data across sheets using dropdown

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
145
Office Version
  1. 2019
Platform
  1. Windows
Hello
I need filtering data by merge duplicates items for column C,D based on ID in column B across sheets with matching part of item using helper column (G) then should show the items based on column G in output sheet .
if cell B1 is empty then should merge for each ID for whole data and insert BALANCE column(E) to subtract column C from D , if I select specific month , then just merge based on month and insert BALANCE column(E) to subtract column C from D . the result should be in OUTPUT based on cell B1 and match part of the item in column G with others sheets in column B and insert TOTAL row to sum the whole columns and subtract from each other.
every time I will add new sheets before OUTPUT sheet. so should update data in OUTPUT sheet as I need it by clear data in OUPUT sheet .


TOTAL (1).xlsm
ABCD
1DATEBATCHIMPORTEXPORT
201/01/2023STR ATM001 RS2,000.00
302/01/2023NO S001 STR12,200.00
403/01/2023TRGG MMNR MSDF2,400.001,200.00
504/01/2023MMDT NTY AS0022,600.001,300.00
605/01/2023MSR2,800.001,400.00
706/01/2023ATM001 RS STR1,500.00
807/01/2023NO STR1 SFGG N1,600.00
908/01/2023STR1,700.00
1009/02/2023NTY AS002 MMDT BN1,800.00
1110/02/2023MSR NBH 1099U NMT663,800.00
1211/02/2023STR4,000.002,000.00
1312/02/2023NO S001 CV678 STR14,200.002,100.00
1413/02/2023MMNR TRGG MSDF4,400.00
1514/02/2023MMDT4,400.00
1615/02/2023NBH 1099U MSR NMT664,400.002,200.00
CA


TOTAL (1).xlsm
ABCD
1DATEBATCHIMPORTEXPORT
211/01/2023ATM001 RS MN99 STR200.00
312/01/2023NO S001 NBH100 STR1200.00
413/01/2023TRGG MNJ800 MMNR MSDF3,000.00100.00
514/01/2023MMDT NTY AS002200.00
615/01/2023MSR1,000.001,400.00
716/01/2023 ATM001 RS BYJ STR600.00
817/01/2023NO STR1 SFGG N1,000.00200.00
918/01/2023STR1,000.00100.00
1019/02/2023 NTY AS002 MMDT BN2,000.00
1120/02/2023MSR NBH 1099U NMT663,800.00
1221/02/2023STR4,000.002,000.00
1322/02/2023NO S001 CV678 STR14,200.002,100.00
1423/02/2023MMNR TRGG MSDF4,400.00
1524/02/2023MMDT4,400.00
1625/02/2023 NBH 1099U MSR NMT664,400.002,200.00
CMN



when B1 is empty
TOTAL (1).xlsm
ABCDEFG
1ITEMS
2BATCHIMPORTEXPORTBALANCESTR
3STR1
4MMNR
5MMDT
6MSR
7
8
9
10
OUTPUT
Cells with Data Validation
CellAllowCriteria
B1List=MONTHS


result
TOTAL (1).xlsm
ABCD
1
2BATCHIMPORTEXPORTBALANCE
3STR11,200.007,800.003,400.00
4STR111,800.00600.0011,200.00
5MMNR14,200.001,300.0012,900.00
6MMDT13,600.001,300.0012,300.00
7MSR20,200.007,200.0013,000.00
8TOTAL71,000.0018,200.0052,800.00
9
OUTPUT
Cell Formulas
RangeFormula
B8:C8B8=SUM(B3:B7)
D8D8=B8-C8
Cells with Data Validation
CellAllowCriteria
B1List=MONTHS


IF I select month in B1 = JAN

TOTAL (1).xlsm
ABCDEFG
1JANITEMS
2BATCHIMPORTEXPORTBALANCESTR
3STR3,200.003,900.00-700.00STR1
4STR13,400.001,800.001,600.00MMNR
5MMNR5,400.001,300.004,100.00MMDT
6MMDT2,800.001,300.001,500.00MSR
7MSR3,800.002,800.001,000.00
8TOTAL18,600.0011,100.007,500.00
9
OUTPUT
Cell Formulas
RangeFormula
B8:C8B8=SUM(B3:B7)
D8D8=B8-C8
Cells with Data Validation
CellAllowCriteria
B1List=MONTHS


the data could be 2500 rows for each sheet , the sheets could be 20 sheets at least
thanks .
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
@Maklil, try this:
Note: I haven't applied the Total, just see if this works first:
VBA Code:
Sub Maklil_1()
Dim regEx As Object
Dim va, vb
Dim i As Long, j As Long, M As Long

Sheets("OUTPUT").Activate
Range("A3:D100000").Clear
va = Range("G2", Cells(Rows.Count, "G").End(xlUp)).Resize(, 4)
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
            .Global = True
            .MultiLine = False
            .IgnoreCase = False
        End With

    For Each wsn In Worksheets
        If wsn.Name <> "OUTPUT" Then
            For i = 1 To UBound(va, 1)
                regEx.Pattern = "\b" & va(i, 1) & "\b"
                
                If Range("B1") = "" Then
                    With wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        For j = 1 To UBound(vb, 1)
                            If regEx.test(vb(j, 2)) Then
                                va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                va(i, 4) = va(i, 2) - va(i, 3)  'balance
'                            If va(i, 1) = "STR" Then Debug.Print vb(j, 2) & " : " & va(i, 2) & " : " & va(i, 3)
                            End If
                            
                        Next
                    End With
                    
                Else
                    With wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        M = Month(DateValue("1 " & Range("B1") & " 2020"))
                        For j = 1 To UBound(vb, 1)
                            If Month(vb(j, 1)) = M Then
                                If regEx.test(vb(j, 2)) Then
                                    va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                    va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                    va(i, 4) = va(i, 2) - va(i, 3)  'balance
                                End If
                            End If
                        Next
                    End With
               End If
            Next
        End If
    Next

Range("A3").Resize(UBound(va, 1), 4) = va
        
End Sub

But I got different result from yours when B1 is blank:
Maklil #1.xlsm
ABCD
1
2BATCHIMPORTEXPORTBALANCE
3STR1120079003300
4STR11180060005800
5MMNR14200130012900
6MMDT13600310010500
7MSR20200720013000
8
OUTPUT
 
Upvote 0
But I got different result from yours when B1 is blank:
my apologies about merging errors!🙏🙏🙏🙏
your code works greatly , but I need add TOTAL row as in OP .
thank you .
 
Upvote 0
my apologies about merging errors!🙏🙏🙏🙏
your code works greatly , but I need add TOTAL row as in OP .
thank you .
Try this:
VBA Code:
Sub Maklil_2()
Dim regEx As Object
Dim va, vb
Dim i As Long, j As Long, M As Long, n As Long
Dim Wsn As Worksheet

Sheets("OUTPUT").Activate
Range("A3:D100000").Clear
va = Range("G2", Cells(Rows.Count, "G").End(xlUp)).Resize(, 4)
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
            .Global = True
            .MultiLine = False
            .IgnoreCase = False
        End With

    For Each Wsn In Worksheets
        If Wsn.Name <> "OUTPUT" Then
            For i = 1 To UBound(va, 1)
                regEx.Pattern = "\b" & va(i, 1) & "\b"
                
                If Range("B1") = "" Then
                    With Wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        For j = 1 To UBound(vb, 1)
                            If regEx.test(vb(j, 2)) Then
                                va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                va(i, 4) = va(i, 2) - va(i, 3)  'balance
'                            If va(i, 1) = "STR" Then Debug.Print vb(j, 2) & " : " & va(i, 2) & " : " & va(i, 3)
                            End If
                            
                        Next
                    End With
                    
                Else
                    With Wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        M = Month(DateValue("1 " & Range("B1") & " 2020"))
                        For j = 1 To UBound(vb, 1)
                            If Month(vb(j, 1)) = M Then
                                If regEx.test(vb(j, 2)) Then
                                    va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                    va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                    va(i, 4) = va(i, 2) - va(i, 3)  'balance
                                End If
                            End If
                        Next
                    End With
               End If
            Next
        End If
    Next

Range("A3").Resize(UBound(va, 1), 4) = va
n = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & n) = "TOTAL"
Range("B" & n) = "=SUM(B3:B" & n - 1 & ")"
Range("C" & n) = "=SUM(C3:C" & n - 1 & ")"
Range("D" & n) = "=B" & n & "-C" & n
Range("A" & n & ":D" & n).Interior.Color = 49407
Range("A" & n & ":D" & n).Font.Bold = True

End Sub
 
Upvote 0
it will copy data twice and will delete borders which made it manually !

TOTAL (1).xlsm
ABCDEFG
1ITEMS
2BATCHIMPORTEXPORTBALANCESTR
3STR1120079003300STR1
4STR11180060005800MMNR
5MMNR14200130012900MMDT
6MMDT13600310010500MSR
7MSR20200720013000
8710002550045500
9710002550045500ITEMS
10ITEMSSTR
11STR1120079003300STR1
12STR11180060005800MMNR
13MMNR14200130012900MMDT
14MMDT13600310010500MSR
15MSR20200720013000
16TOTAL284000102000182000
OUTPUT
Cell Formulas
RangeFormula
B16:C16B16=SUM(B3:B15)
D16D16=B16-C16
Cells with Data Validation
CellAllowCriteria
B1List=MONTHS
 
Upvote 0
it will copy data twice and will delete borders which made it manually !
I can't reproduce the error, here's what I got by running the code once or twice:
Maklil #1.xlsm
ABCDEFG
1ITEMS
2BATCHIMPORTEXPORTBALANCESTR
3STR1120079003300STR1
4STR11180060005800MMNR
5MMNR14200130012900MMDT
6MMDT13600310010500MSR
7MSR20200720013000
8TOTAL710002550045500
9
OUTPUT
Cell Formulas
RangeFormula
B8:C8B8=SUM(B3:B7)
D8D8=B8-C8


In the beginning, the code will clear Range("A3:D100000") in Sheets("OUTPUT"):
VBA Code:
Sheets("OUTPUT").Activate
Range("A3:D100000").Clear
Isn't that what you want? as you said:
so should update data in OUTPUT sheet as I need it by clear data in OUPUT sheet .
 
Upvote 0
I will check again maybe the problem is from the file .
 
Upvote 0
ok but how I can fix problem deletion borders and format number.

the code delete it #,##0.00 , borders !
 
Upvote 0
ok but how I can fix problem deletion borders and format number.

the code delete it #,##0.00 , borders !
OK, I'm assuming the OUTPUT sheet already has the format. The code just keeps the format intact.
Try:
VBA Code:
Sub Maklil_3()
Dim regEx As Object
Dim va, vb
Dim i As Long, j As Long, M As Long, n As Long
Dim Wsn As Worksheet
Dim c As Range

Sheets("OUTPUT").Activate
Set c = Range("A3:D3")
Range("A4:D100000").Clear

va = Range("G2", Cells(Rows.Count, "G").End(xlUp)).Resize(, 4)
        Set regEx = CreateObject("VBScript.RegExp")
        With regEx
            .Global = True
            .MultiLine = False
            .IgnoreCase = False
        End With

    For Each Wsn In Worksheets
        If Wsn.Name <> "OUTPUT" Then
            
            For i = 1 To UBound(va, 1)
                regEx.Pattern = "\b" & va(i, 1) & "\b"
                
                If Range("B1") = "" Then
                    With Wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        For j = 1 To UBound(vb, 1)
                            If regEx.test(vb(j, 2)) Then
                                va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                va(i, 4) = va(i, 2) - va(i, 3)  'balance
'                            If va(i, 1) = "STR" Then Debug.Print vb(j, 2) & " : " & va(i, 2) & " : " & va(i, 3)
                            End If
                            
                        Next
                    End With
                    
                Else
                    With Wsn
                        vb = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 4)
                        M = Month(DateValue("1 " & Range("B1") & " 2020"))
                        For j = 1 To UBound(vb, 1)
                            If Month(vb(j, 1)) = M Then
                                If regEx.test(vb(j, 2)) Then
                                    va(i, 2) = va(i, 2) + vb(j, 3)  'import
                                    va(i, 3) = va(i, 3) + vb(j, 4)  'export
                                    va(i, 4) = va(i, 2) - va(i, 3)  'balance
                                End If
                            End If
                        Next
                    End With
               End If
            Next
        End If
    Next

Range("A3").Resize(UBound(va, 1), 4) = va
n = Range("A" & Rows.Count).End(xlUp).Row + 1
Range("A" & n) = "TOTAL"
Range("B" & n) = "=SUM(B3:B" & n - 1 & ")"
Range("C" & n) = "=SUM(C3:C" & n - 1 & ")"
Range("D" & n) = "=B" & n & "-C" & n

c.Copy
Range("A" & 3 & ":D" & n).PasteSpecial xlPasteFormats

Range("A" & n & ":D" & n).Interior.Color = 49407
Range("A" & n & ":D" & n).Font.Bold = True
End Sub
 
Upvote 1
Solution
sorry about error in OP ,no one waste time for this bad thread , but you did it.
I grateful for your assistance and spend your time to help me.
every thing is great.
thank you so much .;)
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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