简体   繁体   中英

Combining the information from multiple rows in VBA onto a new worksheet

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

  1. Checks all cells in the Parent Task column [F] in workbook "Cleaned Data" [figured this out]
  2. Checks if the contents of a cell are found in the Project column [A] of the workbook "Project summaries".
  3. IF the value of that cell is found in column A of "Project Summaries", THEN add the value of the Hours column [I] in the same row on "Cleaned Data" to the Hours column (D) in "Project Summaries" at the same row as the Parent Task.
  4. ELSE Copy contents of cells in F,G,H,I (Parent Task, Designer, Sales Unit, Hours) from "Cleaned Data" to A,B,C,D of "Project Summaries".

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM