SUMIF和多张纸

我正在search多个工作表的string,一旦find这个string它将其复制到合并数据表。

第一栏是一个支付号码,例如50 。 第2栏是工单号码11111 。 栏目“3,4,5,6,7,8”或其他信息。 “9,10,12”是£值。

当这个数据被复制时,第2列中的工作订单号可能会有多个实例。我需要有一个SUMIF来查找第2列中的任何重复项,并总结“9,10和12”列。

我已经设法得到SUMIF的代码工作,但我似乎无法得到它的数据已被复制后的工作。

我需要做的是,当你点击button时,它searchstring,并将其复制到MergedData表单中,并在同一个button上单击以完成SUMIF函数。

任何援助,您可以提供非常感谢!

这是我用来从多个工作表中复制数据的代码:

 Private Sub CommandButton1_Click() Dim FirstAddress As String, WhatFor As String Dim Cell As Range, Sheet As Worksheet Dim sSheetsWithData As String, sSheetsWithoutData As String Dim lSheetRowsCopied As Long, lAllRowsCopied As Long Dim bFound As Boolean Dim sOutput As String With Application .ScreenUpdating = False .EnableEvents = False .CutCopyMode = False End With WhatFor = Sheets("SUB CON PAYMENT FORM").Range("L9") Worksheets("MergedData").Cells.ClearContents 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 bFound = False With Sheet.Columns(1) Set Cell = .Find(WhatFor, LookIn:=xlValues, LookAt:=xlWhole) If Not Cell Is Nothing Then bFound = True lSheetRowsCopied = 0 FirstAddress = Cell.Address Do lSheetRowsCopied = lSheetRowsCopied + 1 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 Else bFound = False End If If bFound Then sSheetsWithData = sSheetsWithData & " " & Sheet.Name & " (" & lSheetRowsCopied & ")" & vbLf lAllRowsCopied = lAllRowsCopied + lSheetRowsCopied Else sSheetsWithoutData = sSheetsWithoutData & " " & Sheet.Name & vbLf End If End With End If Next Sheet If sSheetsWithData <> vbNullString Then sOutput = "Sheets with data copied (# of rows)" & vbLf & vbLf & sSheetsWithData & vbLf & _ "Total rows copied = " & lAllRowsCopied & vbLf & vbLf Else sOutput = "No sheeTs contained data to be copied" & vbLf & vbLf End If If sSheetsWithoutData <> vbNullString Then sOutput = sOutput & "Sheets with no rows copied:" & vbLf & vbLf & sSheetsWithoutData Else sOutput = sOutput & "All sheets had data that was copied." End If If sOutput <> vbNullString Then MsgBox sOutput, , "Copy Report" With Worksheets("MergedData") If .Cells(1, 1).Value = vbNullString Then .Rows(1).Delete End With Set Cell = Nothing End Sub 

而这是我用于SUMIF的其他代码:

 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