Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run code from one worksheet to populate another.
I have 2 worksheets in the same workbook called "DATA" and "Scores".
Structure of sheet "Scores" is as follows A B C 1 Player Caps goals 2 Beckham 65 3 Shearer 67 4 Beattie 2 I need to populate column C with number of goals scored by each player (Column A) from worksheet "DATA". The code below runs fine when I select the player (Range A) to become the ActiveCell and then run the code and only if the code and the three column are in the same worksheet, which is "sheet "DATA". I need to add a loop to the code to run it from sheet "scores". Of course the line mScorer = ActiveCell needs to be changed to reflect going through all the players Public Sub SumGoals() Dim mScorer As String Dim mTotal As Integer Dim mGoals As Integer Dim myCell As Range mTotal = 0 mScorer = ActiveCell With Worksheets("Data").Range("AD3:AL500") For Each c In Range("AD2:AL500") If c.Value = mScorer Then firstAddress = c.Address mGoals = c.Offset(0, 1).Value mTotal = mTotal + mGoals End If Next c End With If mTotal 0 Then ActiveCell.Offset(0, 3).Value = mTotal End If End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run code from one worksheet to populate another.
Public Sub SumGoals()
Dim cellRow As String, firstAddress As String Dim mScorer As String Dim mTotal As Integer Dim mGoals As Integer Dim myCell As Range Dim c As Object mTotal = 0 cellRow = ActiveCell.Row mScorer = ActiveSheet.Range("A" & cellRow) With Worksheets("Data").Range("AD3:AL500") For Each c In Range("AD2:AL500") If c.Value = mScorer Then firstAddress = c.Address mGoals = c.Offset(0, 1).Value mTotal = mTotal + mGoals End If Next c End With If mTotal 0 Then ActiveSheet.Range("A" & cellRow).Offset(0, 3).Value = mTotal End If End Sub -----Original Message----- I have 2 worksheets in the same workbook called "DATA" and "Scores". Structure of sheet "Scores" is as follows A B C 1 Player Caps goals 2 Beckham 65 3 Shearer 67 4 Beattie 2 I need to populate column C with number of goals scored by each player (Column A) from worksheet "DATA". The code below runs fine when I select the player (Range A) to become the ActiveCell and then run the code and only if the code and the three column are in the same worksheet, which is "sheet "DATA". I need to add a loop to the code to run it from sheet "scores". Of course the line mScorer = ActiveCell needs to be changed to reflect going through all the players Public Sub SumGoals() Dim mScorer As String Dim mTotal As Integer Dim mGoals As Integer Dim myCell As Range mTotal = 0 mScorer = ActiveCell With Worksheets("Data").Range("AD3:AL500") For Each c In Range("AD2:AL500") If c.Value = mScorer Then firstAddress = c.Address mGoals = c.Offset(0, 1).Value mTotal = mTotal + mGoals End If Next c End With If mTotal 0 Then ActiveCell.Offset(0, 3).Value = mTotal End If End Sub . |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Run code from one worksheet to populate another.
Thanks losmac , but this code does not loop through worksheet "Scores" and
somehow does not run the code to calculate the number of goal from worksheet "Data" "losmac" wrote in message ... Public Sub SumGoals() Dim cellRow As String, firstAddress As String Dim mScorer As String Dim mTotal As Integer Dim mGoals As Integer Dim myCell As Range Dim c As Object mTotal = 0 cellRow = ActiveCell.Row mScorer = ActiveSheet.Range("A" & cellRow) With Worksheets("Data").Range("AD3:AL500") For Each c In Range("AD2:AL500") If c.Value = mScorer Then firstAddress = c.Address mGoals = c.Offset(0, 1).Value mTotal = mTotal + mGoals End If Next c End With If mTotal 0 Then ActiveSheet.Range("A" & cellRow).Offset(0, 3).Value = mTotal End If End Sub -----Original Message----- I have 2 worksheets in the same workbook called "DATA" and "Scores". Structure of sheet "Scores" is as follows A B C 1 Player Caps goals 2 Beckham 65 3 Shearer 67 4 Beattie 2 I need to populate column C with number of goals scored by each player (Column A) from worksheet "DATA". The code below runs fine when I select the player (Range A) to become the ActiveCell and then run the code and only if the code and the three column are in the same worksheet, which is "sheet "DATA". I need to add a loop to the code to run it from sheet "scores". Of course the line mScorer = ActiveCell needs to be changed to reflect going through all the players Public Sub SumGoals() Dim mScorer As String Dim mTotal As Integer Dim mGoals As Integer Dim myCell As Range mTotal = 0 mScorer = ActiveCell With Worksheets("Data").Range("AD3:AL500") For Each c In Range("AD2:AL500") If c.Value = mScorer Then firstAddress = c.Address mGoals = c.Offset(0, 1).Value mTotal = mTotal + mGoals End If Next c End With If mTotal 0 Then ActiveCell.Offset(0, 3).Value = mTotal End If End Sub . |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Help on VBA Code to populate multiple sheets | Excel Discussion (Misc queries) | |||
populate cell on worksheet 2 if text is red on worksheet 1 | Excel Worksheet Functions | |||
Use a code to populate a cell with a specific value | Excel Worksheet Functions | |||
VBA code to populate 1000's of cells? | Excel Discussion (Misc queries) | |||
HOW ?Excel chart auto insert /populate a code based on date | New Users to Excel |