Checking if data exist in range, if it exist add up the number

daphnedaphy

New Member
Joined
Feb 17, 2014
Messages
7
Hello Everyone,

I'm pretty new to VBA and hope you can advise me in some coding which I'm currently stuck for days.

I have 1 sheet (Sheet 2) which have a list of name, designation, department and hours. In another sheet (Sheet 2) I would like to run a summary of the total number hours in each department has. I'm using VBA nested loop to check if data exist in "Sheet 2". If it's exist it will calculated the number of hours in each department.

The issue i'm facing is, the loop only run once. I tried writing the pseudo code, the logic work but when I run it. The code run once.

Sheet 1 data

NameRoleDepartmentHours Spent
AAADesignerCreative2
BBBDeveloperDevelopment4
CCCSenior DeveloperDevelopment6
DDDAccount ManagerAccount8
EEEAccount AssistantAccount10

<tbody>
</tbody>

Sheet 2 data (output)
DepartmentTotal Hours
Account18
Creative
Development

<tbody>
</tbody>

My Code as follow:
Code:
Sub CountResourceHours()
    Dim rTimeHr, rDashboardTime As Range
    
    Set rTimeHr = Worksheets("Sheet2").Range("C2")
    Set rDashboardTime = Worksheets("Sheet3").Range("A2")
    
    Do While rDashboardTime.Text <> ""
        
        Do While rTimeHr.Text <> ""
            If rTimeHr = rDashboardTime Then
                rDashboardTime.Offset(columnoffset:=1) = rDashboardTime.Offset(columnoffset:=1) + rTimeHr.Offset(columnoffset:=1)
            End If
            
            Set rTimeHr = rTimeHr.Offset(rowoffset:=1)
            
        Loop
        
    Set rDashboardTime = rDashboardTime.Offset(rowoffset:=1)
        
    Loop
End Sub

Can someone shine me some light?
Million Thanks!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Welcome to the Board!

Why do you need VBA at all? It seems you should be able to do it with the SUMIF formula.
See Excel's help files on the SUMIF function for details and examples.
 
Upvote 0
Thank you for welcoming me to the board and thanks for the proposed solutions! Yes, unfortunately I have to use VBA as this is part of a dashboard I'm working on. The item list shown in Sheet 2 will be undefined, depending on how much data the user would like to input (e.g. it might be just 10 rows, 20 rows or 50 rows). Depending on the data I have collected I would need to calculate the total number of hours per department and from there, I will be generating a piechart (done) which will be showing in the dashboard for presentation purposes.

Can you guys shine more light to help me please?
Millions Thanks!
 
Upvote 0
SUMIF in code to do the same is shown below:

Sub SumRangeOnCriteria()
Dim Sumact1 As Long, Sumact2 As Long, Sumact3 As Long
Sumact1 = Application.SumIf(Sheets("Sheet1").Range("C2:C10"), "Creative", Range("D2:D10"))
Sheets("Sheet3").Range("B2").Value = Sumact1
Sumact2 = Application.SumIf(Sheets("Sheet1").Range("C2:C10"), "Development", Range("D2:D10"))
Sheets("Sheet3").Range("B3").Value = Sumact2
Sumact3 = Application.SumIf(Sheets("Sheet1").Range("C2:C10"), "Account", Range("D2:D10"))
Sheets("Sheet3").Range("B4").Value = Sumact3
End Sub
 
Upvote 0
Thanks for the propose solution! This work too, but the downside is where if the user had 100 rows of data I would need to change the code manually. Do you think if I use a switch case will work too?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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