VBA Code to gte the values by multiple filtering

Wafee

Board Regular
Joined
May 27, 2020
Messages
104
Office Version
  1. 2013
Platform
  1. Windows
Hi, Can someone help me with VBA code that does the below.

I have two sheets where one is source and another is output. I will update source sheet weekly and VBA should update output.
Source sheet looks as below. it will have both current week's and past weeks data.
Column HColumn JColumn UColumn V
WEEK_END_DATE​
Sales​
Senior Manager​
Manager​
10/17/2020​
40​
Hari​
Max​
10/17/2020​
30​
Sam​
James​
10/17/2020​
60​
Hari​
Max​
10/17/2020​
10​
Hari​
Colin​
10/17/2020​
20​
Hari​
Jade​
10/17/2020​
40​
Sam​
Varun​
10/17/2020​
50​
Sam​
James​
10/17/2020​
70​
Sam​
Murat​
10/10/2020​
70​
Sam​
Murat​

my output sheet looks as below. below steps needs to be done.

1. Find the Report date from the cell Y2 (10/17/20) and needs to find out where is that date in first row (In this case it is "D" row. this is where values to be entered)
2. now pick a name from column A from output sheet and look for the same in either column U or V in source sheet and then filter the report date in column H of source sheet and sum of respective sales amount to be published in cells of "D" row.
Column AColumn BColumn CColumn DColumn Y
Senior Manager/Manager​
10/3/2020​
10/10/2020​
10/17/2020​
Report date​
Hari​
80​
60​
130​
10/17/2020​
Max​
40​
40​
100​
Colin​
40​
20​
10​
Jade​
20​
Sam​
90​
80​
190​
Varun​
20​
30​
40​
James​
20​
20​
80​
Murat​
30​
20​
70​
Jade​
20​
10​

I have posted a similar query before but it was bit complicated and haven't recived any responses. this time made my requirement simpler. Thnak you in adavance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
For some reason, when I copy/paste the data you posted, each value contains an invisible question mark at the end. Is your data manually entered into your workbook? It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Hi Mumps, Sorry for all the trouble. below are the input and output sheets(Chnaged few things to make it simpler). Below are the steps that needs to be done. Output sheet will basically be amended with new weekdata without altering any existing data.

1. take the date from Report date column (G) in the output sheet and filter the same in weekending column (A) in inputsheet.

2. Take the list of Senior Managers and their respective Managers from the above filtered data and needs to cross check with the output sheet list (Column A and Column B) and if there is any changes that needs to be added.(In this case Jade was under Sam and for the week 10/17/20 he moved under Hari so new line to be added in "B" column of output sheet adding Hari. no need to remove Jade from Sam as he got previous data from previous week )

3. Once Manager related Changes are done we have find out in which column data needs to be entered by looking for Reporting date(10/17/20) in 1 st row of output sheet. in this case it is column "E".

4. Finally, Sales amount needs to be pulled from input sheet. For senior Managers filter name and report date value needs to be pulled. for Managers - name, report date and respective senior manager name also needs be filtered and respective sum of sales amount needs to be pulled.

Please consider to skip step 2 if it is not possible mate. Thank you in advance.




Input sheet.

Samplexl.xlsx
ABCD
1WEEK_END_DATESalesSenior ManagerManager
210/17/202040HariMax
310/17/202030SamJames
410/17/202060HariMax
510/17/202010HariColin
610/17/202020HariJade
710/17/202040SamVarun
810/17/202050SamJames
910/17/202070SamMurat
1010/10/202070SamMurat
1110/10/202020HariJade
Sheet1


output sheet.

Samplexl.xlsx
ABCDEFG
1Senior ManagerManager10/3/202010/10/202010/17/2020Report date
2Hari806013010/17/2020
3Max4040100
4Colin402010
5Jade20
6Sam9080190
7Varun203040
8James202080
9Murat302070
10Jade2010
Sheet2
 
Upvote 0
For some reason, when I copy/paste the data you posted, each value contains an invisible question mark at the end. Is your data manually entered into your workbook? It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
Hi Mumps, Could you please help me with the request.
 
Upvote 0
This macro is based on the data you posted in Post #3:
VBA Code:
Sub CopyValues()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lRow As Long, srcWS As Worksheet, desWS As Worksheet
    Dim fnd As Range, man As Range, srMan As Range, i As Long, y As Long, x As Long: x = 1
    Dim fVisRow As Long, fndDate As Range, rng As Range, total As Long, val As String
    Set srcWS = Sheets("Source")
    Set desWS = Sheets("Output")
    lRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS
        Set fndDate = .Rows(1).Find(.Range("G2"))
        If Not fndDate Is Nothing Then
            With srcWS
                .Cells(1, 1).CurrentRegion.AutoFilter 1, CStr(fndDate)
                fVisRow = .Rows("2:" & lRow).SpecialCells(xlCellTypeVisible).Row
                lvisrow = .Cells(.Rows.Count, "A").End(xlUp).Row
                With desWS.Range("B3:B" & LastRow).SpecialCells(xlCellTypeConstants).Areas
                    For i = 1 To .Count
                        fr = .Item(i).Row
                        val = desWS.Cells(fr - 1, 1)
                        cnt = .Item(i).Cells.Count
                        For Each man In desWS.Range("B" & fr).Resize(cnt)
                            Set fnd = srcWS.Range("D" & fVisRow & ":D" & lvisrow).Find(man, LookIn:=xlValues, lookat:=xlWhole)
                            If Not fnd Is Nothing Then
                                If fnd.Offset(, -1) <> val Then
                                    Set srMan = desWS.Range("A:A").Find(fnd.Offset(, -1), LookIn:=xlValues, lookat:=xlWhole)
                                    If Not srMan Is Nothing Then
                                        y = desWS.Range("B" & srMan.Row + 1 & ":B" & LastRow).Cells.SpecialCells(xlCellTypeBlanks).Row
                                        desWS.Rows(y).EntireRow.Insert
                                        desWS.Cells(y, 2) = man
                                    End If
                                End If
                            End If
                        Next man
                    Next i
                End With
            End With
        End If
        srcWS.Range("A1").AutoFilter
        For Each srMan In .Range("A2:A" & LastRow)
            If srMan <> "" Then
                Set fndDate = .Rows(1).Find(.Range("G2"))
                If Not fndDate Is Nothing Then
                    Set fnd = srcWS.Range("C:C").Find(srMan, LookIn:=xlValues, lookat:=xlWhole)
                    If Not fnd Is Nothing Then
                        With srcWS
                            .Cells(1, 1).CurrentRegion.AutoFilter 3, fnd
                            For Each rng In .Range("B2:B" & lRow).SpecialCells(xlCellTypeVisible)
                                total = total + rng.Value
                            Next rng
                            .Range("A1").AutoFilter
                        End With
                        .Cells(srMan.Row, fndDate.Column) = total
                        total = 0
                    End If
                End If
            End If
        Next srMan
    End With
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This macro is based on the data you posted in Post #3:
VBA Code:
Sub CopyValues()
    Application.ScreenUpdating = False
    Dim LastRow As Long, lRow As Long, srcWS As Worksheet, desWS As Worksheet
    Dim fnd As Range, man As Range, srMan As Range, i As Long, y As Long, x As Long: x = 1
    Dim fVisRow As Long, fndDate As Range, rng As Range, total As Long, val As String
    Set srcWS = Sheets("Source")
    Set desWS = Sheets("Output")
    lRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With desWS
        Set fndDate = .Rows(1).Find(.Range("G2"))
        If Not fndDate Is Nothing Then
            With srcWS
                .Cells(1, 1).CurrentRegion.AutoFilter 1, CStr(fndDate)
                fVisRow = .Rows("2:" & lRow).SpecialCells(xlCellTypeVisible).Row
                lvisrow = .Cells(.Rows.Count, "A").End(xlUp).Row
                With desWS.Range("B3:B" & LastRow).SpecialCells(xlCellTypeConstants).Areas
                    For i = 1 To .Count
                        fr = .Item(i).Row
                        val = desWS.Cells(fr - 1, 1)
                        cnt = .Item(i).Cells.Count
                        For Each man In desWS.Range("B" & fr).Resize(cnt)
                            Set fnd = srcWS.Range("D" & fVisRow & ":D" & lvisrow).Find(man, LookIn:=xlValues, lookat:=xlWhole)
                            If Not fnd Is Nothing Then
                                If fnd.Offset(, -1) <> val Then
                                    Set srMan = desWS.Range("A:A").Find(fnd.Offset(, -1), LookIn:=xlValues, lookat:=xlWhole)
                                    If Not srMan Is Nothing Then
                                        y = desWS.Range("B" & srMan.Row + 1 & ":B" & LastRow).Cells.SpecialCells(xlCellTypeBlanks).Row
                                        desWS.Rows(y).EntireRow.Insert
                                        desWS.Cells(y, 2) = man
                                    End If
                                End If
                            End If
                        Next man
                    Next i
                End With
            End With
        End If
        srcWS.Range("A1").AutoFilter
        For Each srMan In .Range("A2:A" & LastRow)
            If srMan <> "" Then
                Set fndDate = .Rows(1).Find(.Range("G2"))
                If Not fndDate Is Nothing Then
                    Set fnd = srcWS.Range("C:C").Find(srMan, LookIn:=xlValues, lookat:=xlWhole)
                    If Not fnd Is Nothing Then
                        With srcWS
                            .Cells(1, 1).CurrentRegion.AutoFilter 3, fnd
                            For Each rng In .Range("B2:B" & lRow).SpecialCells(xlCellTypeVisible)
                                total = total + rng.Value
                            Next rng
                            .Range("A1").AutoFilter
                        End With
                        .Cells(srMan.Row, fndDate.Column) = total
                        total = 0
                    End If
                End If
            End If
        Next srMan
    End With
    srcWS.Range("A1").AutoFilter
    Application.ScreenUpdating = True
End Sub
Thank you for the response Mumps. Will run the code and get back to you.
 
Upvote 0
When you get the error, hover the mouse over "total". What do you see? Hover the mouse over "rng.value". What do you see?
 
Upvote 0
Sorry the above mentioned error was due to the invisible question mark (my bad), I have corrected it and code ran successfully. but i have only got the the sales amount for Senior Manager and for manager respective cells are empty.
When you get the error, hover the mouse over "total". What do you see? Hover the mouse over "rng.valueSorry that error was due to
 
Upvote 0
Sorry the above mentioned error was due to the invisible question mark (my bad), I have corrected it and code ran successfully. but i have only got the the sales amount for Senior Manager and for manager respective cells are empty.
Also If i add Jade under Hari and run the code it is still adding additional line with for Jade which is a duplicate.
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,208
Members
448,874
Latest member
Lancelots

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