Sum Column B based on Column A using Excel VBA Macro

Rakesh Kamani

New Member
Joined
Feb 25, 2020
Messages
33
Office Version
  1. 2013
Platform
  1. Windows
There are 4 worksheets in the workbook. Command button in sheet 1, project name and actual hours in sheet 2, project name and planned hours based on month and year in sheet 3, final report display on sheet 4. (Name of project, actual hours and planned hours).

When I click on the Command button on Sheet 1, Report Display on Sheet 4 based on Sheet 2 and Sheet 3.

How to write a VBA in this case?

Thank you in advance.
 
Hi @Rakesh Kamani.

I put here a first code considering the data as your images. Adding the columns from March to June.

Dante Amor
ABCDEFGHIJKLMN
1Project NameproductDivisionHoursPhase
2M312101KeyTD-Pu41
3M309401DoorsTDP62
4M309201MetalTDS33
5M309401IronMR44
6M312101MetalEX25
Actual_Hours


Dante Amor
ABCDEFGHIJKLMNOPQR
1Project2020/012020/022020/032020/042020/052020/06
2M327905-A M SYC SBW138.5125.62152140125.32362.14
3Total:
4M309201-A M SYC SBW25658532.5145.6385
5Total:
6M309401-A MM SYMC SBW145.3236.1423.5612536512
7Total:
8M327905-A M SYC SBW25.2336545.1221.425.3621.3
9Total:
10M312101- SYMC SBW54.325.632412.53314.23
11Total:
12
13M327905-A MM_225.323.125.3645.8578.0665
Planned_Hours


Dante Amor
ABC
1ProjectHoursPlanned
2M3121016260.76
3M30940110525.56
4M3092013348.13
AH_Vs_PH_Report


VBA Code:
Sub Report()
  Dim sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim a As Variant, ky As Variant, nSum As Double
  Dim dic As Object, i As Long, lr As Long
  Dim c As Range, r As Range, f As Range, cell As String
  
  Set sh2 = Sheets("Actual_Hours")
  Set sh3 = Sheets("Planned_Hours")
  Set sh4 = Sheets("AH_Vs_PH_Report")
  sh4.Rows("2:" & Rows.Count).ClearContents
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = sh2.Range("D2:N" & sh2.Range("D" & Rows.Count).End(3).Row).Value2
  
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + a(i, 10)
  Next
  Set r = sh3.Range("D:E")
  
  For Each ky In dic.keys
    nSum = 0
    Set f = r.Find(ky, , xlValues, xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        nSum = nSum + WorksheetFunction.Sum(sh3.Range(sh3.Cells(f.Row, "K"), sh3.Cells(f.Row, "R")))
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      lr = sh4.Range("A" & Rows.Count).End(3).Row + 1
      sh4.Range("A" & lr).Value = ky
      sh4.Range("B" & lr).Value = dic(ky)
      sh4.Range("C" & lr).Value = nSum
    End If
  Next
End Sub

I understand that several things are missing, but I hope you can try this and move forward with whatever you need.
 
Upvote 0
Solution

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
thank you Amor,

The above image you can consider 1 project, because project ID is M327905.
you can consider and calculate Automotive/Private separately .
 
Upvote 0
Hi @Rakesh Kamani.

I put here a first code considering the data as your images. Adding the columns from March to June.

Dante Amor
ABCDEFGHIJKLMN
1Project NameproductDivisionHoursPhase
2M312101KeyTD-Pu41
3M309401DoorsTDP62
4M309201MetalTDS33
5M309401IronMR44
6M312101MetalEX25
Actual_Hours


Dante Amor
ABCDEFGHIJKLMNOPQR
1Project2020/012020/022020/032020/042020/052020/06
2M327905-A M SYC SBW138.5125.62152140125.32362.14
3Total:
4M309201-A M SYC SBW25658532.5145.6385
5Total:
6M309401-A MM SYMC SBW145.3236.1423.5612536512
7Total:
8M327905-A M SYC SBW25.2336545.1221.425.3621.3
9Total:
10M312101- SYMC SBW54.325.632412.53314.23
11Total:
12
13M327905-A MM_225.323.125.3645.8578.0665
Planned_Hours


Dante Amor
ABC
1ProjectHoursPlanned
2M3121016260.76
3M30940110525.56
4M3092013348.13
AH_Vs_PH_Report


VBA Code:
Sub Report()
  Dim sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim a As Variant, ky As Variant, nSum As Double
  Dim dic As Object, i As Long, lr As Long
  Dim c As Range, r As Range, f As Range, cell As String
 
  Set sh2 = Sheets("Actual_Hours")
  Set sh3 = Sheets("Planned_Hours")
  Set sh4 = Sheets("AH_Vs_PH_Report")
  sh4.Rows("2:" & Rows.Count).ClearContents
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = sh2.Range("D2:N" & sh2.Range("D" & Rows.Count).End(3).Row).Value2
 
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + a(i, 10)
  Next
  Set r = sh3.Range("D:E")
 
  For Each ky In dic.keys
    nSum = 0
    Set f = r.Find(ky, , xlValues, xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        nSum = nSum + WorksheetFunction.Sum(sh3.Range(sh3.Cells(f.Row, "K"), sh3.Cells(f.Row, "R")))
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      lr = sh4.Range("A" & Rows.Count).End(3).Row + 1
      sh4.Range("A" & lr).Value = ky
      sh4.Range("B" & lr).Value = dic(ky)
      sh4.Range("C" & lr).Value = nSum
    End If
  Next
End Sub

I understand that several things are missing, but I hope you can try this and move forward with whatever you need.
Thank you DanteAmor
 
Upvote 0
Hi @Rakesh Kamani.

I put here a first code considering the data as your images. Adding the columns from March to June.

Dante Amor
ABCDEFGHIJKLMN
1Project NameproductDivisionHoursPhase
2M312101KeyTD-Pu41
3M309401DoorsTDP62
4M309201MetalTDS33
5M309401IronMR44
6M312101MetalEX25
Actual_Hours


Dante Amor
ABCDEFGHIJKLMNOPQR
1Project2020/012020/022020/032020/042020/052020/06
2M327905-A M SYC SBW138.5125.62152140125.32362.14
3Total:
4M309201-A M SYC SBW25658532.5145.6385
5Total:
6M309401-A MM SYMC SBW145.3236.1423.5612536512
7Total:
8M327905-A M SYC SBW25.2336545.1221.425.3621.3
9Total:
10M312101- SYMC SBW54.325.632412.53314.23
11Total:
12
13M327905-A MM_225.323.125.3645.8578.0665
Planned_Hours


Dante Amor
ABC
1ProjectHoursPlanned
2M3121016260.76
3M30940110525.56
4M3092013348.13
AH_Vs_PH_Report


VBA Code:
Sub Report()
  Dim sh2 As Worksheet, sh3 As Worksheet, sh4 As Worksheet
  Dim a As Variant, ky As Variant, nSum As Double
  Dim dic As Object, i As Long, lr As Long
  Dim c As Range, r As Range, f As Range, cell As String
 
  Set sh2 = Sheets("Actual_Hours")
  Set sh3 = Sheets("Planned_Hours")
  Set sh4 = Sheets("AH_Vs_PH_Report")
  sh4.Rows("2:" & Rows.Count).ClearContents
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = sh2.Range("D2:N" & sh2.Range("D" & Rows.Count).End(3).Row).Value2
 
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + a(i, 10)
  Next
  Set r = sh3.Range("D:E")
 
  For Each ky In dic.keys
    nSum = 0
    Set f = r.Find(ky, , xlValues, xlPart)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        nSum = nSum + WorksheetFunction.Sum(sh3.Range(sh3.Cells(f.Row, "K"), sh3.Cells(f.Row, "R")))
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
      lr = sh4.Range("A" & Rows.Count).End(3).Row + 1
      sh4.Range("A" & lr).Value = ky
      sh4.Range("B" & lr).Value = dic(ky)
      sh4.Range("C" & lr).Value = nSum
    End If
  Next
End Sub

I understand that several things are missing, but I hope you can try this and move forward with whatever you need.
Good Morning Dear Dante Amor,

This coding you provided is very useful, but it is the only projected result are that has a common project in Sheet 2 and Sheet 3..

Can you please provide which is available project id's in sheet2 and sheet3..
previous code is perfectly working but the thing is common project only displayed. in this case I need all projects Id's .

Thank you for this in advance.

Thanks,
Rakesh Kamani
 
Upvote 0
Can you please provide which is available project id's in sheet2 and sheet3..
previous code is perfectly working but the thing is common project only displayed. in this case I need all projects Id's .

Which ones do you want:
Projects available on sheet2 that are not on sheet3?
Projects available on sheet3 that are not on sheet2?
And how to identify which records on sheet3 are projects?
 
Upvote 0
Which ones do you want:
Projects available on sheet2 that are not on sheet3?
Projects available on sheet3 that are not on sheet2?
And how to identify which records on sheet3 are projects?
Projects available on sheet2 that are not on sheet3? - Yes (example: D325897, M845214,)
Projects available on sheet3 that are not on sheet2? - Yes (example: D548521 A MM SYN MAC, M796548 D M SY MODE)
And how to identify which records on sheet3 are projects? (you can consider single project :- M327905 M DELL MOTHER, M327905 E DELL KEY, M327905)

Thank you Amor :)
 
Upvote 0
1583248315500.png


I mean, in the column you have these texts:
- Total
- M33801
- Automotive / private

How to know, of those texts what is project?
 
Upvote 0
View attachment 8140

I mean, in the column you have these texts:
- Total
- M33801
- Automotive / private

How to know, of those texts what is project?


We currently consider the M333801 WABCO Electrical Project ID. Please ignore the automotive / private and Total.

Put a comment for automotive / private if that is possible. There is no need for automotive / private values right now. if you can arrange this value please consider like project ID and put it comment, in feature its useful for me.

For your help, I thank you for Advance.
 
Upvote 0
visually I can know what a project is. but I mean if there is a pattern to recognize which texts are projects.
 
Upvote 0
visually I can know what a project is. but I mean if there is a pattern to recognize which texts are projects.
The project pattern is
sheet2 project ID like below
9000226
M312101
M309401
9150016
D001876
AV17045
P313720
S_V1910

Sheet3 project ID like below
M315904 PEP CMC
M327701 Tata industries wireless
M332801 M SY SYN Marc
M332801 D CMS port
M332701 KIA Dashboard
M332701 B KIA Rearwheel
P313720 D MM SYN Break
M312101 Mark Der

Sheet4 project ID display like below
9000226
M312101
M309401
9150016
D001876
AV17045
P313720
S_V1910
M315904
M327701
M332801
M332701

Can you set the value of output hours in an integer, the previous code planned value is displayed (3.4 + 2.2 = 5.6) in this case no decimal value is required. Can you give something like (3.4 + 2.2 = 6)?
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,954
Members
449,198
Latest member
MhammadishaqKhan

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