Past Destination & Sum if on sheet

atame

New Member
Joined
May 26, 2015
Messages
31
Hi all,

I am searching multiple sheets for a string, once this string is found it copies it to the MergedData sheet.

In column 1 is a payment number for instance "50". Column 2 is a work order number "11111". Colums "3,4,5,6,7,8" or other information. "9,10,12" are £ values.

I have two problems that i need to rectify.

1) when the data is pasted into the MergedData Sheet, it pastes it in row 2 rather than row 1.

2) when this data is copied over there could be multiple instances on a work order number is column 2, i need to have a sumif to find any duplicates in column 2 and sum up columns "9,10 & 12".

I have managed to get the code for the SUMIF working but i cant seem to get it to work after the data has been copied over.

What I need to happen is, when you click the button it search for the string, and have it copy to the MergedData sheet, and on the same button click to perform the SUMIF function.

Any assistance you can provide is greatly appreciated!

Here is the code that i am using at the moment.

Thanks Aarron

Here is the code to search and copy the raw data.
Code:
Private Sub CommandButton1_Click()Dim FirstAddress As String, WhatFor As String
Dim Cell As Range, Sheet As Worksheet
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .CutCopyMode = False
    End With
    
WhatFor = Sheets("SUB CON PAYMENT FORM").Range("L9")


Worksheets("MergedData").Cells.Clear


If WhatFor = Empty Then Exit Sub


    For Each Sheet In Sheets
      If Sheet.Name <> "SUB CON PAYMENT FORM" And Sheet.Name <> "MergedData" And Sheet.Name <> "Details" Then


    With Sheet.Columns(1)
      Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)


    If Not Cell Is Nothing Then
     FirstAddress = Cell.Address
     Do
        Cell.EntireRow.Copy
        ActiveWorkbook.Sheets("MergedData").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlValues
     Set Cell = .FindNext(Cell)
     Loop Until Cell Is Nothing Or Cell.Address = FirstAddress
    End If
    End With
End If


Next Sheet
Set Cell = Nothing

End Sub


Here is the code i have for the SUMIF function.
Code:
Sub combineduplicates()                 '### starts our macroApplication.ScreenUpdating = False      '### Excel wont update its screen while executing this macro. This is a huge performace boost
Dim SUMcols()                         '### declare a second empty array for our sum columns


SUMcols() = Array(9, 10, 12)         '### the second array stores the columns which should be summed up


'### the next line sets our range for searching dublicates. Starting at cell A2 and ending at the last used cell in column A
Set searchrange = Range([b2], Columns(2).Find(what:="*", after:=[b1], searchdirection:=xlPrevious))
For Each cell In searchrange            '### now we start looping through each cell of our searchrange
       
    Set search = searchrange.Find(cell, after:=cell, lookat:=xlWhole)   '### searches for a dublicate. If no dub exists, it finds only itself
    Do While search.Address <> cell.Address     '### until we find our starting cell again, these rows are all dublicates
        
        For i = 0 To UBound(SUMcols)    '### loop through all columns for calculating the sum
            '### next line sums up the cell in our starting row and its counterpart in its dublicate row
            Cells(cell.Row, SUMcols(i)) = CDbl(Cells(cell.Row, SUMcols(i))) + CDbl(Cells(search.Row, SUMcols(i)))
        Next i                          '### go ahead to the next column
        
       
        search.EntireRow.Delete         '### we are finished with this row. Delete the whole row
        Set search = searchrange.Find(cell, after:=cell)    '### and search the next dublicate after our starting row
    Loop
   
Next                                    '### from here we start over with the next cell of our searchrange
                                        '### Note: This is a NEW unique value since we already deleted all old dublicates
Application.ScreenUpdating = True       '### re-enable our screen updating

End Sub                                 '### ends our macro


 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi - I am trying to understand parts of what you are asking.

You are clearing out the contents of the MergedData sheet every time, so why the need to specify an offset location to paste it into. Can you not just paste it into Cell A1 every time, or if there are multiple rows to copy, then use XlDown, not xlup?

For this part....

2) when this data is copied over there could be multiple instances on a work order number is column 2, i need to have a sumif to find any duplicates in column 2 and sum up columns "9,10 & 12".

...are you saying that there could be multiple rows copied? If so, are you wanting to add rows 9,10, 12 on all rows, or for each individual row that gets copied?
 
Upvote 0
Hi - I am trying to understand parts of what you are asking.

You are clearing out the contents of the MergedData sheet every time, so why the need to specify an offset location to paste it into. Can you not just paste it into Cell A1 every time, or if there are multiple rows to copy, then use XlDown, not xlup?

For this part....



...are you saying that there could be multiple rows copied? If so, are you wanting to add rows 9,10, 12 on all rows, or for each individual row that gets copied?

Hi Loin75,

I am not overly experienced in VBA, so am still learning.

Please find the link below with some attached documents that i am using as test. this has the code within.

If you open the SUMIF file you will be able to see the macro in action, I need it to find all duplicates in column B the sum up each row where the instance is in columns 9,10 & 12.
 
Upvote 0
Hi all,

The first question has been resolved. It is now only getting the SUMIF to work from a button click on one sheet, and perform the function on another.
 
Upvote 0
Hi atame,

If the MergedData sheet is used purely for calculations only, and is wiped clean at the start every time, and only gets populated with the relevant rows, then it should be pretty easy. Just add this to the end of your code:

Code:
Sheets("Mergeddata").Range("M1") = "=SUM(I1,J1,L1)"Dim lastRow As Long
lastRow = Sheets("Mergeddata").Range("L" & Rows.Count).End(xlUp).Row
Sheets("Mergeddata").Range("M1").AutoFill Destination:=Sheets("Mergeddata").Range("M1:M" & lastRow)

ie. M1 should always be free on the mergeddata sheet. Paste the SUM formula in there, and then copy down to the last row.

Is this what you are after?
 
Upvote 0
Hi atame,

If the MergedData sheet is used purely for calculations only, and is wiped clean at the start every time, and only gets populated with the relevant rows, then it should be pretty easy. Just add this to the end of your code:

Code:
Sheets("Mergeddata").Range("M1") = "=SUM(I1,J1,L1)"Dim lastRow As Long
lastRow = Sheets("Mergeddata").Range("L" & Rows.Count).End(xlUp).Row
Sheets("Mergeddata").Range("M1").AutoFill Destination:=Sheets("Mergeddata").Range("M1:M" & lastRow)

ie. M1 should always be free on the mergeddata sheet. Paste the SUM formula in there, and then copy down to the last row.

Is this what you are after?
Hi Lion75,

Not quite what i'm after. I need it to search column B for any duplicates, then if found, sums up the values in Columns I, J & L. then delete the duplicates leaving only the 1 instance with the total for all.

As mention above, the code that i have to do this task, works very well, but it only works on the activesheet, i need a button on sheet1 that perfoms the action on the MergedData sheet.

Example of raw data.

5027801108721032 Fake House, Fake Road, FK1 1FK103221102110
5027801108721033 Fake House, Fake Road, FK1 1FK103321092109
5027801108721034 Fake House, Fake Road, FK1 1FK103421082108
5027801108721035 Fake House, Fake Road, FK1 1FK103521072107
5027801108721036 Fake House, Fake Road, FK1 1FK103621062106
5027801108721037 Fake House, Fake Road, FK1 1FK103721052105
5027801108721038 Fake House, Fake Road, FK1 1FK103821042104
50278711787212 Fake House, Fake Road, FK1 1FK65984.61984.61
50278711787213 Fake House, Fake Road, FK1 1FK65984.62984.62
5027861168725 Fake House, Fake Road, FK1 1FK91843.95843.95
5027901109723 Fake House, Fake Road, FK1 1FK45.5754.31754.31
5027901109723 Fake House, Fake Road, FK1 1FK45.5754.31754.31

<tbody>
</tbody>

What it should look like after.
5027801108721032 Fake House, Fake Road, FK1 1FK72451474914749
50278711787212 Fake House, Fake Road, FK1 1FK1301969.231969.23
5027861168725 Fake House, Fake Road, FK1 1FK91843.95843.95
5027901109723 Fake House, Fake Road, FK1 1FK911508.621508.62

<tbody>
</tbody>
 
Upvote 0
Crude, but does the job..

Code:
Application.CutCopyMode = False


With Sheets("Mergeddata")
    .Range("M1") = "=SUM(I1,J1,L1)"
    .Range("N1") = "=SUMIFS(M:M,B:B,B1)"
End With
    
Dim lastRow As Long


lastRow = Sheets("Mergeddata").Range("L" & Rows.Count).End(xlUp).Row
Sheets("Mergeddata").Range("M1:N1").AutoFill Destination:=Sheets("Mergeddata").Range("M1:N" & lastRow)


With Sheets("Mergeddata")
    .Columns("N:N").Copy
    .Columns("N:N").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Columns("M:M").Delete Shift:=xlToLeft
    .Range("$A$1:$N$2233").RemoveDuplicates Columns:=2, Header:=xlNo
End With
 
Upvote 0
Crude, but does the job..

Code:
Application.CutCopyMode = False


With Sheets("Mergeddata")
    .Range("M1") = "=SUM(I1,J1,L1)"
    .Range("N1") = "=SUMIFS(M:M,B:B,B1)"
End With
    
Dim lastRow As Long


lastRow = Sheets("Mergeddata").Range("L" & Rows.Count).End(xlUp).Row
Sheets("Mergeddata").Range("M1:N1").AutoFill Destination:=Sheets("Mergeddata").Range("M1:N" & lastRow)


With Sheets("Mergeddata")
    .Columns("N:N").Copy
    .Columns("N:N").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .Columns("M:M").Delete Shift:=xlToLeft
    .Range("$A$1:$N$2233").RemoveDuplicates Columns:=2, Header:=xlNo
End With
Hi Leon75,

That did not sum up the value correctly! The code below works, but only on the active sheet, i need to to work on MergedData.

Thanks for all the help!

Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False      
Dim SumCols()                         
Dim ws As Worksheet
SumCols() = Array(9, 10, 12)          

Set searchrange = Range(Range("B1"), Columns(2).Find(what:="*", after:=[b1], searchdirection:=xlPrevious))
For Each cell In searchrange          
    Set Search = searchrange.Find(cell, after:=cell, lookat:=xlWhole)   
    Do While Search.Address <> cell.Address     


        For I = 0 To UBound(SumCols)    

            Cells(cell.Row, SumCols(I)) = CDbl(Cells(cell.Row, SumCols(I))) + CDbl(Cells(Search.Row, SumCols(I)))
        Next I                         
               
        Search.EntireRow.Delete         
        Set Search = searchrange.Find(cell, after:=cell)   
    Loop
 
Next                                 

Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,206,970
Messages
6,075,921
Members
446,170
Latest member
zzzz02

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