VBA Excel If Statement

mulholm

New Member
Joined
Jul 2, 2018
Messages
49
I need a bit of help writing some code.

I have a spreadsheet to track stats of a teams performance however the system that is used to generate the stats does not produce a team total so everyday i have to go in and manually add the formula.

I was going to just do a basic =sum formula to total up the data however the "Team" stats will appear in a different row each day depending on how many people have been in that day.

What i want to achieve is something along the lines of a "If cell = Team Total then sum of the above data"

Hope that makes sense.

cheers
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hiya mulholm,

Assuming that your table has the following structure:

Column A || Column B
a || 1
b || 2
c || 3
d || 4
e || 5
f || 6
g || 7
Team Total

Then you can use the following code:

Code:
Option Explicit


Sub TeamTotal()
    Dim lngRow              As Long
    
    With Sheet1
        'find "Team Total" position
        lngRow = [COLOR=#ff0000]Application.WorksheetFunction.Match("Team Total", .Range("A:A"), 0)[/COLOR]
        'apply the sum formula
       [COLOR=#0000ff] .Range("B" & lngRow).Formula = "=SUM[/COLOR]([COLOR=#ff8c00]B1:B" & lngRow - 1 & ")"[/COLOR]
    End With


End Sub

It will find the position of "Team Total" in column A (in our example, the result is row 8) and create SUM formula in cell B8 which sums all values between B1 and B7.

Let me know if that helps.

Best regards,
Justyna
 
Upvote 0
Team member 1
6:45:00
Team member 2
5:24:00
Team member 3
3:00:00
Team member 4
4:00:00
Team member 5
6:32:00
Team member 6
6:00:00
Team member 7
6:15:00
Team member 8
6:31:00
Team Total

<colgroup><col width="157" style="width: 118pt; mso-width-source: userset; mso-width-alt: 5741;"> <col width="140" style="width: 105pt; mso-width-source: userset; mso-width-alt: 5120;"> <tbody> </tbody>

Above is how the table currently appears.

What i want it to perform a sum of team total.
The team members names and team total will always appear in column A and the time signed in will always appear in column B.
The issue i have is that "Team total" changes row number on a daily basis but always stays in column A
 
Upvote 0
The team members names and team total will always appear in column A and the time signed in will always appear in column B.
The issue i have is that "Team total" changes row number on a daily basis but always stays in column A
 
Upvote 0
Good, thanks for confirming. If that's the structure then my VBA code should work.
If you run the code then the current position of "Team total" row number should be automatically identified and a SUM function will be created right next to it. Give it a try, then change the position of "Team total", run it again, and let me know if that works for you.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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