Macro to combine start and end dates of resoucres into one line

latkan

New Member
Joined
Nov 7, 2013
Messages
10

<tbody>
[TD="class: votecell"][/TD]
[TD="class: postcell"] I work with a resourcing ledger which is also used for other financial purposes. As a result of the financial phases each resource in the file is split over 4-5 rows. What I would like to do is create a macro which finds their start date and corresponding end date on the next few rows and combines them whilst deleting the other rows and dates in between which show the interim date.


Another twist is that the file also holds roles yet to be fulfilled and are therefore just simply TBC.


I also need to do the same for the TBCs but instead will use the name of the role rather than the resource name to do the deletion of in between dates I have no idea how to go about this

So far I have created the following macros which goes through the dates and as long as they are in ldest to newest order - will seek out the lines which follow on from the original line. It does not however combine the start and final end date

Sub LoopRange3()

'Start at the currently selected cell
x = ActiveCell.Row
y = x + 1

'Outside loop
Do While Cells(x, 1).Value <> ""
'Inside loop
Do While Cells(y, 1).Value <> ""
'Test for duplication:
'If the values of the third column (C) and the fifth column (E) match in two rows
'delete the second row of the pair, otherwise go to the next row until the end
If (Cells(x, 3).Value = Cells(y, 3).Value) _
And (Cells(x, 5).Value = Cells(y, 5).Value) _
And (Cells(x, 7).Value = Cells(y, 7).Value) _
And (Cells(x, 9).Value = Cells(y, 9).Value) _
And (Cells(x, 10).Value = Cells(y, 10).Value) _
And (Cells(x, 13).Value <> Cells(y, 13).Value) _
And (Cells(y, 13).Value >= Cells(x, 14).Value) _
Then

'FOR DUPLICATE DELETION: Uncommment the following line by removing the apostrophe
'Cells(y, 3).EntireRow.Delete

'Shade the entire row green if it's a duplicate
'FOR DUPLICATE DELETION: Make the following line a comment by adding an apostrophe
Cells(y, 3).EntireRow.Interior.ColorIndex = 3

Else

'FOR DUPLICATE DELETION: Uncomment the following line by removing the apostrophe

End If

'FOR DUPLICATE DELETION: Make the following line a comment by adding an apostrophe
y = y + 1
Loop
'increase the value of x by 1 to move the loop starting point to the next row
x = x + 1
'reset y so it starts at the next row
y = x + 1
Loop

End Sub


:confused:



any help would be great

[/TD]

</tbody>
 
latkan,

Is the macro picking up the correct data/values for the cells colored BLUE, VIOLET, light GREEN?????

Please respond:
1. Is cell O13 the correct value from F13 or F14?
2. Is cell O15 the correct value from F15 or F16 or F17?
3. Is cell O18 the correct value from F18 or F19 or F20?



Excel 2007
FGHKLMNOPQTUV
2Unique IDProject Role NameFull NameStatusStart DateEnd DateResults
31.0Marketing AnalystJane AustenActive1-Nov-134-Jan-141.0Marketing AnalystJane AustenActive1-Nov-1329-Sep-14
445.0Marketing AnalystJane AustenScheduled5-Jan-1431-May-14
51334.0Marketing AnalystJane AustenScheduled1-Jun-1429-Sep-14
6649.0Marketing AnalystJim MurrayActive29-Jun-1321-Jan-14649.0Marketing AnalystJim MurrayActive29-Jun-134-Nov-14
7677.0Marketing AnalystJim MurrayScheduled22-Jan-144-Nov-14
8453.0Marketing AnalystJoe BloggsActive26-Jul-1328-Dec-13453.0Marketing AnalystJoe BloggsActive26-Jul-131-Feb-14
94685.0Marketing AnalystJoe BloggsScheduled28-Dec-131-Feb-14
1033.0ManagerTBCSearching22-Nov-131-Feb-1433.0ManagerTBCSearching22-Nov-1326-Apr-14
113888.0ManagerTBCSearching2-Feb-1426-Apr-14
124098.0ManagerTBCSearching28-Apr-1430-Aug-14
135876.0Finance AnalystTBCSearching28-Nov-1330-Sep-145432.0Finance AnalystTBCSearching28-Nov-131-Apr-15
145432.0Finance AnalystTBCSearching2-Oct-141-Apr-15
15342.0Offshore HRTBCSearching3-Feb-1413-Feb-15342.0Offshore HRTBCSearching1-Jan-1413-Feb-15
16123.0Offshore HRTBCSearching6-Jan-1413-Feb-15
17321.0Offshore HRTBCSearching1-Jan-1430-Nov-14
185461.0Data AnalyserTBCSearching4-Nov-1313-Apr-153333.0Data AnalyserTBCSearching4-Nov-1313-Sep-15
193333.0Data AnalyserTBCSearching14-Apr-1513-Sep-15
201111.0Data AnalyserTBCSearching4-Nov-1313-Feb-15
Sheet1
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
latkan,

Is the macro picking up the correct data/values for the cells colored BLUE, VIOLET, light GREEN?????

Please respond:
1. Is cell O13 the correct value from F13 or F14?
2. Is cell O15 the correct value from F15 or F16 or F17?
3. Is cell O18 the correct value from F18 or F19 or F20?

Hi Hiker,

Yes I can confirm that the macro is picking up the correct cells

would I be able to get a copy of the macro file?

Thanks!
 
Upvote 0
latkan,

Sample raw data:


Excel 2007
FGHIJKLM
2Unique IDProject Role NameFull NameDivisionSub DivisionStatusStart DateEnd Date
31.0Marketing AnalystJane AustenFinanceLedgerActive1-Nov-134-Jan-14
445.0Marketing AnalystJane AustenFinanceLedgerScheduled5-Jan-1431-May-14
51334.0Marketing AnalystJane AustenFinanceLedgerScheduled1-Jun-1429-Sep-14
6649.0Marketing AnalystJim MurrayHRCustomer CareActive29-Jun-1321-Jan-14
7677.0Marketing AnalystJim MurrayHRCustomer CareScheduled22-Jan-144-Nov-14
8453.0Marketing AnalystJoe BloggsMarketingSalesActive26-Jul-1328-Dec-13
94685.0Marketing AnalystJoe BloggsMarketingSalesScheduled28-Dec-131-Feb-14
1033.0ManagerTBCMerchandisingShop RetailSearching22-Nov-131-Feb-14
113888.0ManagerTBCMerchandisingShop RetailSearching2-Feb-1426-Apr-14
124098.0ManagerTBCMerchandisingShop RetailSearching28-Apr-1430-Aug-14
135876.0Finance AnalystTBCMerchandisingShop RetailSearching28-Nov-1330-Sep-14
145432.0Finance AnalystTBCMerchandisingShop RetailSearching2-Oct-141-Apr-15
15342.0Offshore HRTBCLogisticsTransportSearching3-Feb-1413-Feb-15
16123.0Offshore HRTBCLogisticsTransportSearching6-Jan-1413-Feb-15
17321.0Offshore HRTBCLogisticsTransportSearching1-Jan-1430-Nov-14
185461.0Data AnalyserTBCLegalContractsSearching4-Nov-1313-Apr-15
193333.0Data AnalyserTBCLegalContractsSearching14-Apr-1513-Sep-15
201111.0Data AnalyserTBCLegalContractsSearching4-Nov-1313-Feb-15
21
Sheet1


After the macro (with the results to the right of the raw data):


Excel 2007
NOPQRSTUVW
2Unique IDProject Role NameFull NameDivisionSub DivisionStatusStart DateEnd Date
31.0Marketing AnalystJane AustenFinanceLedgerActive1-Nov-1329-Sep-14
4649.0Marketing AnalystJim MurrayHRCustomer CareActive29-Jun-134-Nov-14
5453.0Marketing AnalystJoe BloggsMarketingSalesActive26-Jul-131-Feb-14
633.0ManagerTBCMerchandisingShop RetailSearching22-Nov-1330-Aug-14
75876.0Finance AnalystTBCMerchandisingShop RetailSearching28-Nov-131-Apr-15
8342.0Offshore HRTBCLogisticsTransportSearching1-Jan-1413-Feb-15
95461.0Data AnalyserTBCLegalContractsSearching4-Nov-1313-Sep-15
10
11
12
13
14
15
16
17
18
19
20
21
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub GetUniqueShortList()
' hiker95, 11/19/2013
' http://www.mrexcel.com/forum/excel-questions/737912-macro-combine-start-end-dates-resoucres-into-one-line.html
Dim o As Variant, no As Variant, i As Long, c As Long
Dim r As Long, lrf As Long, lr As Long, n As Long, rng As Range, rngL As Range, rngM As Range
Dim LDate As Date, MDate As Date
Application.ScreenUpdating = False
Columns("P:W").ClearContents
lrf = Cells(Rows.Count, "F").End(xlUp).Row
no = Range("N2:O" & lrf)
Range("N2:O" & lrf).ClearContents
Range("N2") = "N"
With Range("N3:N" & lrf)
  .FormulaR1C1 = "=RC[-7]&RC[-6]"
  .Value = .Value
End With
Range("N2:N" & lrf).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("O2:O" & lrf), Unique:=True
n = Application.CountA(Range("O3:O" & lrf))
ReDim o(1 To n, 1 To 8)
Range("O2:O" & lrf).ClearContents
For r = 3 To lrf
  n = Application.CountIf(Columns(14), Cells(r, 14).Value)
  If n = 1 Then
    i = i + 1
    For c = 6 To 13 Step 1
      o(i, c - 5) = Cells(r, c)
    Next c
  ElseIf n > 1 Then
    LDate = WorksheetFunction.Min(Range("L" & r & ":L" & r + n - 1))
    MDate = WorksheetFunction.Max(Range("M" & r & ":M" & r + n - 1))
    i = i + 1
    For c = 6 To 11 Step 1
      o(i, c - 5) = Cells(r, c)
    Next c
    o(i, 7) = LDate
    o(i, 8) = MDate
  End If
  r = r + n - 1
Next r
Range("N2:O" & lrf) = no
With Range("P2").Resize(, 8)
  .Value = Range("F2").Resize(, 8).Value
  .Font.Bold = True
  .HorizontalAlignment = xlCenter
End With
Range("P3").Resize(UBound(o, 1), UBound(o, 2)) = o
With Range("P3:P" & lrf)
  .NumberFormat = "0.0"
  .HorizontalAlignment = xlCenter
End With
With Range("V3:W" & lrf)
  .NumberFormat = "d-mmm-yy"
End With
Columns("P:W").AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetUniqueShortList macro.


1. Do you want the results to replace the raw data?

2. Do you want the results to be on a new worksheet?
 
Last edited:
Upvote 0
Hi Hiker,

Thanks, the raw data to the side is fine, if not better as it allows easy reference.

However I have spoken to my lead and the resourcing file has thrown a curve ball to me in two manners

1. There will be some people working 50% on one task and 50% on another task
2. The people may "progress" in their development and move into different role or be in the same role but in a different country work location but still the same person will need to be recorded

I do have a employee number per person which I can add to the file and I think this could be used to drive the macro. I have attached the file again with the extra raw data information required

Would you be able to help on this. the macros works well up to a certain extent which is annoying as its so close with the end product.

Apologies for being very needy in this case
 
Upvote 0
Also another aspect is that when they do move onto different roles they may start the role at a later date e.g. if they finish in Dec 28th, they will naturally be pencilled in for the start of the new year but after office shut down so in this case 4th Jan 2014. Will the macro pick this 5-7 day grace?
 
Upvote 0
latkan,

I can see the logic for the current task.

You have two choices:

1. The MrExcel.com team can provide consulting services for all of the applications in the Microsoft Office Suite!
Excel Consultant | Excel Consulting | Microsoft Office Consultants

2. Click on the Reply to Thread button, and just put the word BUMP in the post. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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