I'm pretty new to excel and programming. Been taking courses online etc. to get caught up after it suddenly became my responsibility at work a few months ago.
I have a data table that lists tasks, their parent task (ie, the project the task was for),and some other data (sales units, designer responsible, etc.)
I am trying to write a macro that
In other words, I want to take this table on "Cleaned Data":
and generate this table on "Project Summaries":
Any recommendations of how to start?
If I understand you correctly... the sample data in sheet Cleaned Data is something like this :
From the sample data above, there are repeating value in column F. So, the unique values are : aaa, bbb, ccc, ddd and zzz in column F.
Sample of the beginning condition (before running the sub) in sheet Project summaries is something like this :
From the image above, x = whatever value, doesn't matter.
What you want is if the unique value in column F (for example : aaa) of sheet Cleaned Data is found in column A sheet Project summaries, then sum the hours of each aaa found in column F of sheet Cleaned Data then paste the sum result to the aaa row of column HOURS in sheet Project Summaries ..... if (aaa) not found then copy each row of aaa from column F to I in sheet Cleaned Data then paste to the empty row in column A of sheet Project summaries.
So the expected result is like this :
If that is the kind you want, the sub is something like this :
Sub test()
Dim rgCD As Range: Dim rgPS As Range: Dim rg As Range
Dim cell As Range: Dim c As Range: Dim arr: Dim el
With Sheets("Cleaned Data")
Set rgCD = .Range("F2", .Range("F" & Rows.Count).End(xlUp))
End With
With Sheets("Project summaries")
Set rgPS = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
Set arr = CreateObject("scripting.dictionary")
For Each cell In rgCD: arr.Item(cell.Value) = 1: Next
For Each el In arr
With rgCD
.Replace el, True, xlWhole, , False, , False, False
Set rg = .SpecialCells(xlConstants, xlLogical)
.Replace True, el, xlWhole, , False, , False, False
Set c = rgPS.Find(el, lookat:=xlWhole)
If Not c Is Nothing Then
c.Offset(0, 3).Value = Application.Sum(rg.Offset(0, 3))
Else
Range(rg, rg.Offset(0, 3)).Copy Destination:=rgPS(rgPS.Rows.Count + 1, 1)
Set rgPS = rgPS.Resize(rgPS.Rows.Count + rg.Rows.Count, 1)
End If
End With
Next
End Sub
The sub create the range of data in sheet Cleaned Data column F into variable rgCD, and in sheet Project Summaries column A into rgPS variable.
Then it get the unique value in rgCD put those unique in arr variable.
It loop to each element in arr then get the cell addres of each element found in column F sheet Clean Data (the rgCD variable) into variable rg.
Then it check if the element name is found in rgPS as c variable.
If c is nothing then it copy range rg to rg.offset(0,3) - paste to the last empty row of column A sheet Project Summaries. If not c is nothing then it sum the rg.offset(0,3)... the hours value, and put the sum result into c.offset(0,3) ---> column D respective row of the found element name.
Please note that the value in column A sheet Project Summaries must be exactly the same (if any) in column F sheet Cleaned Data or vice versa.
The sub won't run correctly if for example one value is "aaa" without space, and the other value is "aaa " with space.
Based on your image data, the sub won't run correctly - because for your Project 21, in sheet Cleaned Data you have : "Client 1: Project 21" and "Client 1 - Project 21" while in sheet Project Summaries you have "Client 1: Project 21". If for example in sheet Project Summaries you have "Client 1:Project 21" or "Client 1 : Project 21" or "Client 1-Project 21" the sub will ignore this value, because the unique value the sub found in sheet Cleaned Data is : "Client 1: Project 21" and "Client 1 - Project 21"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.