Lookup and summing comparison
Graham,
Try this: assumes data starts column 2 on both sheets
Sub HiLow()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastrow As Long, r As Long
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
With ws1
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
r = 2 '<=== start row of data
Do
n = Application.CountIf(Range("A:A"), .Cells(r, "A"))
Sum1 = Application.SumIf(Range("A:A"), .Cells(r, "A"), Range("B:B"))
sum2 = Application.VLookup(.Cells(r, "A"), ws2.Range("A:B"), 2, 0)
If IsError(sum2) Then
.Cells(r + n - 1, "C") = "No match"
Else
If Sum1 sum2 Then
.Cells(r + n - 1, "C") = "Too high"
Else
If Sum1 < sum2 Then
.Cells(r + n - 1, "C") = "Too Low"
End If
End If
End If
r = r + n
Loop Until r lastrow
End With
End Sub
HTH
"Graham Haughs" wrote:
I am struggling with finding a simple solution to this and I am not sure
if it is a function option or a programming option. In one worksheet
(Sheet A) as an example there could be two columns as below
Column A Column B
Field 1 12
Field 1 55
Field 2 24
Field 2 43
Field 2 18
Field 3 16
Field 4 21
Field 4 41
In another worksheet (Sheet B) there are also two columns as below
Column A Column B
Field 1 28
Field 2 65
Field 3 67
Field 4 54
There are obviously more columns etc but the concept is the same.
Ideally what I would like to do is in Sheet A, in column C, at the point
where there is the last entry of a Field in column A, a warning flag of
any kind to appear if the totals of Column B are greater than the value
in Sheet B of Column B for the same field. eg in Sheet A, for Field 1,
the total is 77, while the respective figure in Sheet B is 28 for the
same field. Thus the flag of "Too High!" or some equivalent should
appear in cell C2 of Sheet A.
It is further complicated by varying number of entries in Sheet A for
the Field 1, 2 etc.
I can do it using VLookup and Sumif etc but it is long and convoluted, I
just feel there is a simpler solution. I have put it in the Functios
group but if you feel a programming solution is more effective then I
would value any pointers.
Thanks for taking the time to read through this garbled explanation.
Kind Regards
Graham Haughs
Turriff, Scotland
|