Sum non adjacent cells with criteria

sooshil

Board Regular
Joined
Feb 21, 2013
Messages
104
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello

I have data to analyse game result as follows.
Every team has to play twice with the same opponent yielding Score1 and Score2.
I have to add total score of each player on a table 2 shown below.
The main problem is the number of team will be changing. If a team is added then every team will also play with the new team.
So, I want to dynamically add those team scores no matter how many times they played.
I have list of team in another sheet and this first data is dynamically generated based upon the teams listed on the other sheet.
Since the player can be added or removed, the number of scores we need to take will also be changed. That means the position(cell address) of a player in the sheet changes as a player is added or removed.
May be I am explaining more than required. If required more information, I am ready here.


[TABLE="class: grid, width: 1260"]
<colgroup><col span="2"><col><col><col span="2"><col span="7"><col><col span="6"></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]L[/TD]
[TD]M[/TD]
[TD]N[/TD]
[TD]O[/TD]
[TD]P[/TD]
[TD]Q[/TD]
[TD]R[/TD]
[TD]S[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD="colspan: 2"]John Vs Jane[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]John Vs Joseph[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]John Vs Regina[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]John[/TD]
[TD]25[/TD]
[TD]36[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]John[/TD]
[TD]35[/TD]
[TD]29[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]John[/TD]
[TD]38[/TD]
[TD]42[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Jane[/TD]
[TD]28[/TD]
[TD]29[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Joseph[/TD]
[TD]46[/TD]
[TD]32[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Regina[/TD]
[TD]42[/TD]
[TD]33[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD="colspan: 2"]Jane Vs Joseph[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]Jane Vs Regina[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 3"]Joseph Vs Regina[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD]Teams[/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[TD]Tie?[/TD]
[TD]N/R?[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Jane[/TD]
[TD]25[/TD]
[TD]52[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Jane[/TD]
[TD]40[/TD]
[TD]36[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Joseph[/TD]
[TD]42[/TD]
[TD]36[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Joseph[/TD]
[TD]36[/TD]
[TD]41[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Regina[/TD]
[TD]32[/TD]
[TD]51[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Regina[/TD]
[TD]44[/TD]
[TD]41[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]Score1[/TD]
[TD]Score2[/TD]
[/TR]
[TR]
[TD]John[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Jane[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Joseph[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Regina[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Just put your equipment on sheet2 as shown below.

Sheet2
<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:76.04px;" /><col style="width:76.04px;" /><col style="width:76.04px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td><td >C</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td > </td><td >Score1</td><td >Score2</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >John</td><td style="text-align:right; ">98</td><td style="text-align:right; ">107</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >Jane</td><td style="text-align:right; ">93</td><td style="text-align:right; ">117</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >Joseph</td><td style="text-align:right; ">124</td><td style="text-align:right; ">109</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td >Regina</td><td style="text-align:right; ">118</td><td style="text-align:right; ">125</td></tr></table>

Run this macro

Note: Change Sheet1 and Sheet2 by the name of your sheets.
Code:
Sub [B][COLOR=#0000ff]Scores[/COLOR][/B]()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim r As Range, f As Range, cell As String, c As Range
  
  Set sh1 = Sheets("[COLOR=#ff0000]Sheet1[/COLOR]")
  Set sh2 = Sheets("[COLOR=#ff0000]Sheet2[/COLOR]")
  Set r = sh1.UsedRange
  
  For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))
    Set f = r.Find(c.Value, , xlValues, xlWhole)
    If Not f Is Nothing Then
        cell = f.Address
        Do
            c.Offset(, 1).Value = c.Offset(, 1).Value + f.Offset(, 1).Value
            c.Offset(, 2).Value = c.Offset(, 2).Value + f.Offset(, 2).Value
            Set f = r.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
  MsgBox "End  "
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (Scores) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Hi Dante

Thanks for the response. I really appreciative your effort. That adds up the scores but there is a problem.
For the same set of players, no matter how many we run the macro, the total scores should remain same. But here, If we run the macro more than once, the total keeps increasing. Which is not what I want.

Thank you again.
 
Upvote 0
Try this please:

Code:
Sub Scores()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim r As Range, f As Range, cell As String, c As Range
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  Set r = sh1.UsedRange
Sh2.range(sh2.cells(2,2), sh2.cells(rows.count, columns.count)).clearcontents
  
  For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))
    Set f = r.Find(c.Value, , xlValues, xlWhole)
    If Not f Is Nothing Then
        cell = f.Address
        Do
            c.Offset(, 1).Value = c.Offset(, 1).Value + f.Offset(, 1).Value
            c.Offset(, 2).Value = c.Offset(, 2).Value + f.Offset(, 2).Value
            Set f = r.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
  MsgBox "End  "
End Sub
 
Upvote 0
Hi Dante

Thank you so much. :)
It worked.
I am not good at VBA. Can you let me know, if I want to change the resulting table anywhere I want (in Sheet2), then where do I need to change the VBA?
 
Last edited:
Upvote 0
For Each c In sh2.Range("A2", sh2.Range("A" & Rows.Count).End(xlUp))

Change "A" by the new column
Change 2 of "A2" by the initial row
 
Last edited:
Upvote 0
Hi Dante

Thank you again for your support so far. Now, my requirement for the same problem is changed. Actually I couldn't realize before that I need 'For' and 'Against' column too.
I need to get data in the second table as follows.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD][/TD]
[TD]Score1[/TD]
[TD][/TD]
[TD]Score2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: center"]2[/TD]
[TD][/TD]
[TD]For[/TD]
[TD]Against[/TD]
[TD]For[/TD]
[TD]Against[/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD]John[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]Jane[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]Joseph[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]Regina[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
</tbody>[/TABLE]


'For' for a player, lets say for John is sum of all the score that John has scored
'Against' for him is the sum of all the scores that his opponents scored against him.

Thank you so much Dante.
 
Last edited:
Upvote 0
Try this


To change the resulting table anywhere update this line: Set r2 = sh2.Range("A3", sh2.Range("A" & Rows.Count).End(xlUp))

Code:
Sub Scores()
  Dim sh1 As Worksheet, sh2 As Worksheet, r As Range, r2 As Range
  Dim f As Range, cell As String, c As Range, n As Long
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  Set r = sh1.UsedRange
[COLOR=#0000ff]  Set r2 = sh2.Range("A3", sh2.Range("A" & Rows.Count).End(xlUp))[/COLOR]
  
  r2.Offset(0, 1).Resize(r2.Rows.Count, Columns.Count - r2.Cells(1, 1).Column).ClearContents
  For Each c In r2
    Set f = r.Find(c.Value, , xlValues, xlWhole)
    If Not f Is Nothing Then
        cell = f.Address
        Do
            c.Offset(, 1).Value = c.Offset(, 1).Value + f.Offset(, 1).Value
            c.Offset(, 3).Value = c.Offset(, 3).Value + f.Offset(, 2).Value
            If f.Offset(1, 1).Value = "" Then n = -1 Else n = 1
            c.Offset(, 2).Value = c.Offset(, 2).Value + f.Offset(n, 1).Value
            c.Offset(, 4).Value = c.Offset(, 4).Value + f.Offset(n, 2).Value
            Set f = r.FindNext(f)
        Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
  MsgBox "End  "
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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