Your script does the job but given my limited understanding of VBA and the complexity of your code I am struggling to adapt the last part, basically calling a function that will sum the value of all the cells in the various offseted ranges and import the data into the Summary sheet.
The problem lies in calling the function (Compile error: ByRef argument type mismatch)
Hi have highlighted the modifications I have done in your script and I have attached the excel file I am working on.
Here is the module with the code
Sub tgr()
Dim wsSummary As Worksheet
Dim wsDatab As Worksheet
Dim SummaryCell As Range
Dim rngFound As Range
Dim rngExchange() As Range
Dim strFirst As String
Dim strUnq As String
Dim rngIndex As Long
Dim i As Long
'--------------------------------------------------------------------
Dim j As Integer
Dim iLastRowSummaryC As Integer
Dim sh1 As Worksheet
'=====================================================================
Set wsSummary = Sheets("Summary")
Set wsDatab = Sheets("Datab")
With wsSummary.Range("C9", wsSummary.Cells(Rows.Count, "C").End(xlUp))
ReDim rngExchange(1 To Evaluate("Sumproduct((" & .Address(External:=True) & "<>"""")/Countif(" & .Address(External:=True) & "," & .Address(External:=True) & "&""""))"))
rngIndex = 0
For Each SummaryCell In .Cells
If InStr(1, "|" & strUnq & "|", "|" & SummaryCell.Text & "|", vbTextCompare) = 0 And Len(SummaryCell.Text) > 0 Then
strUnq = strUnq & "|" & SummaryCell.Text
Set rngFound = wsDatab.Columns("D").Find(SummaryCell.Text, , xlValues, xlWhole)
If Not rngFound Is Nothing Then
rngIndex = rngIndex + 1
strFirst = rngFound.Address
Set rngExchange(rngIndex) = rngFound
Do
Set rngExchange(rngIndex) = Union(rngExchange(rngIndex), rngFound)
Set rngFound = wsDatab.Columns("D").Find(SummaryCell.Text, rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
Set rngFound = Nothing
End If
End If
'-------------------------------------------------------------------
Set sh1 = ActiveWorkbook.Sheets("Summary")
iLastRowSummaryC = sh1.[C9].End(xlDown).Row + 1
Do While j < iLastRowSummaryC
Worksheets("Summary").Cells(j, 3) = SumIPVrngExchange(rngExchange)
j = j + 1
Exit Do
Loop
'=====================================================================
Next SummaryCell
End With
For i = 1 To rngIndex
rngExchange(i).Parent.Select
rngExchange(i).Select
MsgBox "Defined range for cells containing: " & Split(Mid(strUnq, 2), "|")(i - 1) & Chr(10) & _
"rngExchange(" & i & ") = " & "'" & wsDatab.Name & "'!" & rngExchange(i).Address
Next i
Set wsSummary = Nothing
Set wsDatab = Nothing
Erase rngExchange
End Sub
Module with the function:
Function SumIPVrngExchange(rngExchange As Range) As Double
Dim cell As Range
Dim sumTheCells As Double
For Each cell In rngExchange.Offset(0, 1)
sumTheCells = sumTheCells + cell.Value
Next
SumIPVrngExchange = sumTheCells
End Function
Bookmarks