create a macro that will replicate the output of my helper columns

ajm

Well-known Member
Joined
Feb 5, 2003
Messages
2,056
Office Version
  1. 365
Platform
  1. Windows
G'day folks, an interesting job has been given to me at work. We have a work planning tool that displays all jobs and projects (Work Type) that are ongoing and planned and calculates the human hours (“effort hours”) required to fulfil them based on start and end date. boss then decides whether to bring in temporary labour if we are overrun.

An issue has been identified which i now need to fix. Where we have a planned project, (these are broken down into individual jobs ("project jobs")), the amount of required effort is being duplicated. This is because the effort hours are recorded against the Project line and then also for each project job.

De dupe Forward Plan Hours.xlsx
ABCDEFGH
1TaskEnd DateNeedStatusWork TypeStart DateWork CodeTotal Length (Days)
2Leaks17-Sep-2023YesOKProject09-Mar-2020Keep Going1287
3Leaks17-Sep-2023YesOKJob17-Mar-2021Keep Going914
4Blanket Change27-Feb-2023YesOKJob28-Feb-2018Keep Going1825
5Blanket Change13-Mar-2028YesOKJob14-Mar-2017Keep Going4017
6Blanket Change23-Jun-2025YesOKJob24-Jun-2020Swap Out1825
7Blanket Change09-Jan-2027YesOKJob10-Jan-2022Swap Out1825
8Chemical Supply30-Apr-2025YesOKProject01-May-2020Swap Out1825
9Chemical Supply30-Apr-2025YesOKPricing01-May-2020Swap Out1825
10Chemical Supply30-Apr-2025YesOKPricing01-May-2020Swap Out1825
11Chemical Supply30-Apr-2025YesOKPricing01-May-2020Swap Out1825
12Chemical Supply30-Apr-2025YesOKPricing01-May-2020Swap Out1825
13Chemical Supply30-Apr-2025YesOKPricing01-May-2020Swap Out1825
Sheet1


So, I have concatenated several fields (Task & Need & Status & Work Type & Work Code & Start Date) and done a countif to identify my unique records. The aim is to identify work items (project or job) that are duplicated, and to remove that work items hours. That’s all simple enough except….

Cell Formulas
RangeFormula
J2:J13J2=IF(AND(C2="Yes",D2="OK"),COUNTIFS($A$2:$A$1451,A2,$C$2:$C$1451,C2,$D$2:$D$1451,D2,$G$2:$G$1451,G2),"")
K2:K13K2=IF(J2="","",IF(J2>1,A2&C2&D2&E2&G2&B2,""))
L2:L13L2=IF(K2="","",COUNTIF($K$2:K2,K2))
M2:M13M2=IF(L2=1,H2,"")



Where a Project and a Job have the same end date, the effort is to be recorded against only one of these.

Cell Formulas
RangeFormula
K8:K9K8=IF(J8="","",IF(J8>1,A8&C8&D8&E8&G8&B8,""))
L8:L9L8=IF(K8="","",COUNTIF($K$2:K8,K8))
M8:M9M8=IF(L8=1,H8,"")


To get around this, I used the concatenated strings from above, and removed the work type. Then did a countif based on the amended concatenated string to get a single number for effort for the entire project.

Cell Formulas
RangeFormula
N8:N9N8=IF(M8="","",A8&C8&D8&G8&B8)
O8:O9O8=IF(N8="","",COUNTIF($N$2:N8,N8))
P8:P9P8=IF(O8=1,M8,"")



While I am ok with these extra helper columns, I would like to add these steps into the a macro. Can someone help me code it?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
code will create a sheet named "OUTPUT" to dupplicate sheet1
Create a button in sheet "Sheet1" and assign below code into:
VBA Code:
Option Explicit
Sub project()
Dim lr&, i&, rng, sp, id As String, dic As Object
Set dic = CreateObject("Scripting.Dictionary")

'dupplicate to sheet OUTPUT
If Evaluate("=ISREF(OUTPUT!A1)") Then Sheets("OUTPUT").Delete
Application.CopyObjectsWithCells = False 'do not copy button
Sheets("Sheet1").Copy after:=Sheets("Sheet1")
Application.CopyObjectsWithCells = True
ActiveSheet.Name = "OUTPUT"
'----------
With Sheets("OUTPUT")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:H" & lr).Value2
    For i = 1 To UBound(rng)
        id = rng(i, 1) & "|" & rng(i, 3) & "|" & rng(i, 4) & "|" & rng(i, 7) ' id like "Leaks|Yes|OK|Keepgoing"
        If Not dic.exists(id) And rng(i, 5) = "Project" Then ' if "Project" then add id into dictionary
            dic.Add id, rng(i, 6) & "|" & rng(i, 2)
        End If
    Next
    For i = 1 To UBound(rng)
        id = rng(i, 1) & "|" & rng(i, 3) & "|" & rng(i, 4) & "|" & rng(i, 7)
        If rng(i, 5) <> "Project" And dic.exists(id) Then ' id found in dictionary, but not "Project"
            sp = Split(dic(id), "|")
            If rng(i, 6) >= CDate(sp(0)) And rng(i, 2) <= CDate(sp(1)) Then rng(i, 8) = "" ' if duration is within project date range
        End If
    Next
    .Range("A2").Resize(UBound(rng), UBound(rng, 2)).Value = rng ' paste original data, but delete days column whete Task dupplicate
    .Range("H2:H" & lr).SpecialCells(xlBlanks).EntireRow.Delete ' delete duplicate Tasks. remove this line if you want it to be presented.
End With
End Sub
 
Upvote 0
code will create a sheet named "OUTPUT" to dupplicate sheet1
Create a button in sheet "Sheet1" and assign below code into:
VBA Code:
Option Explicit
Sub project()
Dim lr&, i&, rng, sp, id As String, dic As Object
Set dic = CreateObject("Scripting.Dictionary")

'dupplicate to sheet OUTPUT
If Evaluate("=ISREF(OUTPUT!A1)") Then Sheets("OUTPUT").Delete
Application.CopyObjectsWithCells = False 'do not copy button
Sheets("Sheet1").Copy after:=Sheets("Sheet1")
Application.CopyObjectsWithCells = True
ActiveSheet.Name = "OUTPUT"
'----------
With Sheets("OUTPUT")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:H" & lr).Value2
    For i = 1 To UBound(rng)
        id = rng(i, 1) & "|" & rng(i, 3) & "|" & rng(i, 4) & "|" & rng(i, 7) ' id like "Leaks|Yes|OK|Keepgoing"
        If Not dic.exists(id) And rng(i, 5) = "Project" Then ' if "Project" then add id into dictionary
            dic.Add id, rng(i, 6) & "|" & rng(i, 2)
        End If
    Next
    For i = 1 To UBound(rng)
        id = rng(i, 1) & "|" & rng(i, 3) & "|" & rng(i, 4) & "|" & rng(i, 7)
        If rng(i, 5) <> "Project" And dic.exists(id) Then ' id found in dictionary, but not "Project"
            sp = Split(dic(id), "|")
            If rng(i, 6) >= CDate(sp(0)) And rng(i, 2) <= CDate(sp(1)) Then rng(i, 8) = "" ' if duration is within project date range
        End If
    Next
    .Range("A2").Resize(UBound(rng), UBound(rng, 2)).Value = rng ' paste original data, but delete days column whete Task dupplicate
    .Range("H2:H" & lr).SpecialCells(xlBlanks).EntireRow.Delete ' delete duplicate Tasks. remove this line if you want it to be presented.
End With
End Sub
Bebo, many thanks. i will have a play around with it today. cheers and Happy New Year.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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