Need help modifying an Excel VBA coded to add a check for new hires less that 90 days.

spittingfire

New Member
Joined
Aug 27, 2014
Messages
16
Hi All,

I have the below code that works well and basically no issues except now I would like to have a check for seniority. Basically, adding something in some statement like If Today - Seniority < 90 then "New_Hire" or something along those lines. I just inherited this code and not verse enough to make such a change.

And help will be greatly appreciated.

VBA Code:
'new
Sub test_file_run()
NumRow_proc = Worksheets("_proc").Range("A2", Worksheets("_proc").Range("A2").End(xlDown)).Rows.Count + 1
NumRow_Voyageur_Result = Worksheets("_Voyageur_Result").Range("B2", Worksheets("_Voyageur_Result").Range("B2").End(xlDown)).Rows.Count + 1
NumRow_time_off = Worksheets("_time_off").Range("B2", Worksheets("_time_off").Range("B2").End(xlDown)).Rows.Count + 1
Dim Weekx As Variant
Dim Y As Integer
Dim Lookuparray As Range
Dim myindex As Range
Set Lookuparray = Worksheets("_Voyageur_Result").Range("B2:B" & NumRow_Voyageur_Result)
Set myindex = Worksheets("_Voyageur_Result").Range("F2:Y" & NumRow_Voyageur_Result)
Dim to_Lookuparray As Range
Dim to_myindex As Range
Set to_myindex = Worksheets("_time_off").Range("F2:L" & NumRow_time_off)
Set to_Lookuparray = Worksheets("_time_off").Range("A2:A" & NumRow_time_off)
Dim i As Integer
Worksheets("_proc").Range("A2").End(xlDown)).Rows.Count + 1
Worksheets("_Voyageur_Result").Range("B2").End(xlDown)).Rows.Count + 1
Worksheets("_time_off").Range("B2").End(xlDown)).Rows.Count + 1


For Y = 2 To NumRow_proc
Debug.Print Worksheets("_proc").Cells(Y, 2) & "  -  " & i
Dim var As Range
Set var = Worksheets("_proc").Range("A" & Y)
x = 1
i = 8
Repeat:
If IsError(Application.Match(var, Lookuparray, 0)) Then
    Worksheets("_proc").Cells(Y, i).Resize(1, 10) = "No_Selection"
    i = 18
        Else
    If IsError(Application.Index(myindex, Application.Match(var, Lookuparray, 0), x)) Then
        Weekx = "No_Selection"
            Else:
        Weekx = Application.Index(myindex, Application.Match(var, Lookuparray, 0), x)
    End If
    Debug.Print "Weekx : " & Weekx
    
    
    If Weekx = "No_Selection" Then
        'Fill Function here
        Fill_us_No_Selection (Y)
            Else:
        Weekx_day = Application.Text(Weekx, "ddd")
        Debug.Print "Weekx_day : " & Weekx_day
        
        Select Case Weekx_day
            Case "Mon"
                Weekx_day_col = 1
            Case "Tue"
                Weekx_day_col = 2
            Case "Wed"
                Weekx_day_col = 3
            Case "Thu"
                Weekx_day_col = 4
            Case "Fri"
                Weekx_day_col = 5
            Case "Sat"
                Weekx_day_col = 6
            Case "Sun"
                Weekx_day_col = 7
        End Select
        
        Debug.Print "Weekx_day_col : " & Weekx_day_col
        If IsEmpty(Application.Index(to_myindex, Application.Match(var, to_Lookuparray, 0), Weekx_day_col)) Then
            x = x + 1
            Debug.Print "X : " & x
            If x = 21 Then GoTo Endit
            GoTo Repeat
                Else:
            Allotx = Application.Index(Worksheets("_Voyageur_Allot").Range("A2:F372"), Application.Match(CLng(CDate(Weekx)), Worksheets("_Voyageur_Allot").Range("A2:A372"), 0), 5)
            Debug.Print "Allotx : " & Allotx
    
            If CLng(Allotx) >= 8 Then
                var_row = Application.Match(CLng(CDate(Weekx)), Worksheets("_Voyageur_Allot").Range("A2:A372"), 0) + 1
                Worksheets("_Voyageur_Allot").Range("E" & var_row).Value = Worksheets("_Voyageur_Allot").Range("E" & var_row).Value - 8
                Worksheets("_proc").Cells(Y, i).Value = Weekx
                x = x + 1
                If x = 21 Then GoTo Endit
                Debug.Print "X : " & x
                i = i + 1
                Debug.Print Worksheets("_proc").Cells(Y, 2) & "  -  " & i
                If i = 18 Then GoTo Skipit
                GoTo Repeat
                    Else:
                Worksheets("_proc").Cells(Y, i).Value = "No_Allotment"
                x = x + 1
                If x = 21 Then GoTo Endit
                'i = i + 1
                If i = 18 Then GoTo Skipit
                    
                GoTo Repeat
            End If
            If x = 21 Then GoTo Endit
        End If
    End If
End If
Skipit:
Endit:
Debug.Print i
If i < 18 Then
    Worksheets("_proc").Cells(Y, i).Resize(1, 18 - i) = "No_Allotment"
End If
Next Y


End Sub

Function Fill_us_No_Selection(ByVal rowstart As Integer)
Dim j As Integer
For j = 8 To 17
Worksheets("_proc").Cells(rowstart, j).Value = "No_Selection"
Next j
End Function

I will like the seniority date be used from this sheet under the column C (Seniority_Date)

test_data.xlsm
ABCDEFGHIJKLM
1IDNameSeniority_DateMUDays_Per_weekMonTuesWed ThursFriSatSunDays worked
214417807peter174/24/20062058410:000:000:000:0010:0010:0010:007
3253022876peter183/7/2011205858:008:008:008:000:000:008:007
4509647174peter197/7/2014205858:008:008:008:008:000:000:007
5112457891peter209/12/2014206258:008:008:008:008:000:000:007
6480168528peter2111/16/20152058510:000:0010:0010:000:000:0010:007
724756624peter226/10/2016206250:008:008:008:008:008:000:007
8309738752peter117/22/2016206258:008:008:008:000:000:008:007
9690627818peter128/19/2016206258:008:008:008:000:000:008:007
10695256908peter138/26/2016205850:008:008:008:008:008:000:007
11912265825peter1411/18/2016206250:008:008:008:008:008:000:007
12998320692peter153/26/2018205850:008:008:008:008:008:000:007
13245797967peter166/18/2018205850:008:008:008:008:008:000:007
14844973625peter111/5/2018206250:008:008:008:008:008:000:007
15233069302peter211/12/2018205858:008:008:008:000:000:008:007
16132674823peter32/18/2019206250:008:008:008:008:008:000:007
17446466105peter42/18/2019206258:008:008:008:000:000:008:007
18565090917peter53/19/2019206258:008:008:008:000:000:008:007
19494170342peter64/16/2019205858:008:008:008:000:000:008:007
209659517peter712/10/2019205850:008:008:008:008:008:000:007
21443968955peter812/10/20192058510:000:000:000:0010:0010:0010:007
22277173473peter93/23/2020206248:008:008:008:000:000:008:007
23498138869peter107/20/2020205848:008:008:008:000:000:008:007
2453100peter11/1/2022205848:008:008:008:000:000:008:007
_time_off
Cell Formulas
RangeFormula
M2:M24M2=COUNT(F2:L2)
Cells with Data Validation
CellAllowCriteria
A1Any value



When I run the code I get the below results

test_data.xlsm
ABCDEFGHIJKLMNOPQ
1IDNameTOGTOG_NameID_MUName_MUDate_seniorityDay 1Day 2Day 3Day 4Day 5Day 6Day 7Day 8Day 9Day 10
214417807peter1720114/24/067/30/20237/31/20238/1/20238/2/20238/3/20238/6/20238/7/202310/30/202310/31/202310/1/2023
3253022876peter1820113/7/1112/1/202312/2/202311/30/202312/29/202311/24/202311/25/202311/28/20231/3/20231/4/202310/21/2023
4509647174peter1920117/7/143/26/20237/5/20237/6/202312/31/202312/28/202312/27/202312/26/202312/24/20233/27/20237/27/2023
5112457891peter2020119/12/147/31/20238/1/20238/2/20238/3/20238/7/202310/30/202310/31/202312/24/20232/14/20232/15/2023
6480168528peter21201111/16/156/24/20236/23/20237/15/20237/14/20232/14/20233/15/20231/28/20237/29/20237/13/20236/22/2023
724756624peter2220116/10/161/12/20235/14/20236/18/202312/24/202311/22/202312/26/20234/10/20233/19/20233/20/20233/21/2023
8309738752peter1120117/22/162/15/20232/12/20235/22/20235/21/20235/7/20235/8/202310/29/202310/26/202311/26/20237/16/2023
9690627818peter1220118/19/1612/24/20237/2/20238/13/20237/9/20236/4/20236/11/20238/20/20237/23/20236/25/20238/27/2023
10695256908peter1320118/26/165/4/20235/5/202311/21/20232/16/202310/28/20236/13/20236/14/20236/15/20236/16/20236/17/2023
11912265825peter14201111/18/166/10/20239/27/20235/11/20237/17/20237/18/20237/19/20237/20/20237/21/202311/24/202311/25/2023
12998320692peter1520113/26/181/14/20232/11/20232/25/20233/11/20233/25/20234/8/20234/22/20235/6/20235/20/20236/3/2023
13245797967peter1620116/18/183/3/20233/4/20233/7/202312/26/202312/27/202311/24/20238/8/20239/2/20237/5/202310/6/2023
14844973625peter1201111/5/182/19/20232/22/20232/23/20239/3/20239/6/20239/7/20231/4/20231/5/20237/10/20237/12/2023
15233069302peter2201111/12/185/11/20233/12/20233/13/202312/26/202312/27/202312/28/202311/1/202311/2/20237/18/20237/19/2023
16132674823peter320112/18/1911/16/20236/1/20236/5/20238/14/20235/28/20232/16/20234/10/202311/19/2023No_AllotmentNo_Allotment
17446466105peter420112/18/1912/23/202310/27/20239/22/20239/23/20236/2/20236/30/20237/1/20237/14/20231/13/20238/18/2023
18565090917peter520113/19/191/6/20231/20/20231/27/20232/3/20232/10/20232/17/20232/24/20233/3/20233/10/20233/17/2023
19494170342peter620114/16/194/18/20232/1/202310/8/202312/31/20237/24/20237/20/202312/3/20236/1/20236/2/20236/5/2023
209659517peter7201112/10/192/13/20233/10/20233/24/20234/7/20234/21/20235/5/20235/19/20236/16/20236/30/20237/10/2023
21443968955peter8201112/10/1911/24/20237/3/20233/29/20233/17/20234/26/20234/27/20239/5/202312/29/20237/21/20235/26/2023
22277173473peter920113/23/20No_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_Selection
23498138869peter1020117/20/201/3/20231/5/202312/31/20237/3/20237/6/20231/7/20239/10/20233/24/20232/6/20232/22/2023
2453100peter201111/1/223/29/20234/26/20234/27/20239/5/20235/26/20234/21/20238/4/20236/23/20233/31/20234/7/2023
_proc
Cells with Data Validation
CellAllowCriteria
A1Any value


However, the results that I would like to have is below. The last entry for example should say New_Hire based on the Seniority date.

test_data.xlsm
ABCDEFGHIJKLMNOPQ
1IDNameTOGTOG_NameID_MUName_MUDate_seniorityDay 1Day 2Day 3Day 4Day 5Day 6Day 7Day 8Day 9Day 10
214417807peter1720114/24/067/30/20237/31/20238/1/20238/2/20238/3/20238/6/20238/7/202310/30/202310/31/202310/1/2023
3253022876peter1820113/7/1112/1/202312/2/202311/30/202312/29/202311/24/202311/25/202311/28/20231/3/20231/4/202310/21/2023
4509647174peter1920117/7/143/26/20237/5/20237/6/202312/31/202312/28/202312/27/202312/26/202312/24/20233/27/20237/27/2023
5112457891peter2020119/12/147/31/20238/1/20238/2/20238/3/20238/7/202310/30/202310/31/202312/24/20232/14/20232/15/2023
6480168528peter21201111/16/156/24/20236/23/20237/15/20237/14/20232/14/20233/15/20231/28/20237/29/20237/13/20236/22/2023
724756624peter2220116/10/161/12/20235/14/20236/18/202312/24/202311/22/202312/26/20234/10/20233/19/20233/20/20233/21/2023
8309738752peter1120117/22/162/15/20232/12/20235/22/20235/21/20235/7/20235/8/202310/29/202310/26/202311/26/20237/16/2023
9690627818peter1220118/19/1612/24/20237/2/20238/13/20237/9/20236/4/20236/11/20238/20/20237/23/20236/25/20238/27/2023
10695256908peter1320118/26/165/4/20235/5/202311/21/20232/16/202310/28/20236/13/20236/14/20236/15/20236/16/20236/17/2023
11912265825peter14201111/18/166/10/20239/27/20235/11/20237/17/20237/18/20237/19/20237/20/20237/21/202311/24/202311/25/2023
12998320692peter1520113/26/181/14/20232/11/20232/25/20233/11/20233/25/20234/8/20234/22/20235/6/20235/20/20236/3/2023
13245797967peter1620116/18/183/3/20233/4/20233/7/202312/26/202312/27/202311/24/20238/8/20239/2/20237/5/202310/6/2023
14844973625peter1201111/5/182/19/20232/22/20232/23/20239/3/20239/6/20239/7/20231/4/20231/5/20237/10/20237/12/2023
15233069302peter2201111/12/185/11/20233/12/20233/13/202312/26/202312/27/202312/28/202311/1/202311/2/20237/18/20237/19/2023
16132674823peter320112/18/1911/16/20236/1/20236/5/20238/14/20235/28/20232/16/20234/10/202311/19/2023No_AllotmentNo_Allotment
17446466105peter420112/18/1912/23/202310/27/20239/22/20239/23/20236/2/20236/30/20237/1/20237/14/20231/13/20238/18/2023
18565090917peter520113/19/191/6/20231/20/20231/27/20232/3/20232/10/20232/17/20232/24/20233/3/20233/10/20233/17/2023
19494170342peter620114/16/194/18/20232/1/202310/8/202312/31/20237/24/20237/20/202312/3/20236/1/20236/2/20236/5/2023
209659517peter7201112/10/192/13/20233/10/20233/24/20234/7/20234/21/20235/5/20235/19/20236/16/20236/30/20237/10/2023
21443968955peter8201112/10/1911/24/20237/3/20233/29/20233/17/20234/26/20234/27/20239/5/202312/29/20237/21/20235/26/2023
22277173473peter920113/23/20No_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_SelectionNo_Selection
23498138869peter1020117/20/201/3/20231/5/202312/31/20237/3/20237/6/20231/7/20239/10/20233/24/20232/6/20232/22/2023
2453100peter201111/1/22New_HireNew_HireNew_HireNew_HireNew_HireNew_HireNew_HireNew_HireNew_HireNew_Hire
_proc
Cells with Data Validation
CellAllowCriteria
A1Any value
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I found a solution, thanks

VBA Code:
'new
Sub uscc_file_run()
'Worksheets("_proc").Activate
NumRow_proc = Worksheets("_proc").Range("A2", Worksheets("_proc").Range("A2").End(xlDown)).Rows.Count + 1
NumRow_Voyageur_Result = Worksheets("_Voyageur_Result").Range("B2", Worksheets("_Voyageur_Result").Range("B2").End(xlDown)).Rows.Count + 1
NumRow_time_off = Worksheets("_time_off").Range("B2", Worksheets("_time_off").Range("B2").End(xlDown)).Rows.Count + 1
'Debug.Print "NumRow_proc : " & NumRow_proc
Dim cutOfdDate As Date
Dim srDate As Date
Dim Weekx As Variant
Dim Y As Integer
Dim Lookuparray As Range
Dim myindex As Range
Set Lookuparray = Worksheets("_Voyageur_Result").Range("B2:B" & NumRow_Voyageur_Result)
Set myindex = Worksheets("_Voyageur_Result").Range("F2:Y" & NumRow_Voyageur_Result)
Dim to_Lookuparray As Range
Dim to_myindex As Range
Set to_myindex = Worksheets("_time_off").Range("F2:L" & NumRow_time_off)
Set to_Lookuparray = Worksheets("_time_off").Range("A2:A" & NumRow_time_off)
Dim i As Integer
'NumRow_proc = Worksheets("_proc").Range("A2", Worksheets("_proc").Range("A2").End(xlDown)).Rows.Count + 1
'NumRow_Voyageur_Result = Worksheets("_Voyageur_Result").Range("B2", Worksheets("_Voyageur_Result").Range("B2").End(xlDown)).Rows.Count + 1
'NumRow_time_off = Worksheets("_time_off").Range("B2", Worksheets("_time_off").Range("B2").End(xlDown)).Rows.Count + 1
'Debug.Print "NumRow_proc : " & NumRow_proc
'Debug.Print "NumRow_Voyageur_Result : " & NumRow_Voyageur_Result
'Debug.Print "NumRow_time_off : " & NumRow_time_off




'For Y = 2 To NumRow_proc + 1
For Y = 2 To NumRow_proc
Debug.Print Worksheets("_proc").Cells(Y, 2) & "  -  " & i
Dim var As Range
Set var = Worksheets("_proc").Range("A" & Y)
srDate = Worksheets("_proc").Range("G" & Y)
cutOfDate = Worksheets("_time_off").Range("Q" & 2)

x = 1
i = 8
If srDate + 90 >= cutOfDate Then
    Worksheets("_proc").Cells(Y, i).Resize(1, 10) = "New_Hire"
       Else
repeat:
    If IsError(Application.Match(var, Lookuparray, 0)) Then
        Worksheets("_proc").Cells(Y, i).Resize(1, 10) = "No_Selection"
        i = 18
            Else
        If IsError(Application.Index(myindex, Application.Match(var, Lookuparray, 0), x)) Then
            Weekx = "No_Selection"
                Else:
            Weekx = Application.Index(myindex, Application.Match(var, Lookuparray, 0), x)
        End If
        Debug.Print "Weekx : " & Weekx
        
        
        If Weekx = "No_Selection" Then
            'Fill Function here
            Fill_us_No_Selection (Y)
                Else:
            Weekx_day = Application.Text(Weekx, "ddd")
            Debug.Print "Weekx_day : " & Weekx_day
            
            Select Case Weekx_day
                Case "Mon"
                    Weekx_day_col = 1
                Case "Tue"
                    Weekx_day_col = 2
                Case "Wed"
                    Weekx_day_col = 3
                Case "Thu"
                    Weekx_day_col = 4
                Case "Fri"
                    Weekx_day_col = 5
                Case "Sat"
                    Weekx_day_col = 6
                Case "Sun"
                    Weekx_day_col = 7
            End Select
            
            Debug.Print "Weekx_day_col : " & Weekx_day_col
            If IsEmpty(Application.Index(to_myindex, Application.Match(var, to_Lookuparray, 0), Weekx_day_col)) Then
                x = x + 1
                Debug.Print "X : " & x
                If x = 21 Then GoTo Endit
                GoTo repeat
                    Else:
                Allotx = Application.Index(Worksheets("_Voyageur_Allot").Range("A2:F372"), Application.Match(CLng(CDate(Weekx)), Worksheets("_Voyageur_Allot").Range("A2:A372"), 0), 5)
                Debug.Print "Allotx : " & Allotx
        
                If CLng(Allotx) >= 8 Then
                    var_row = Application.Match(CLng(CDate(Weekx)), Worksheets("_Voyageur_Allot").Range("A2:A372"), 0) + 1
                    Worksheets("_Voyageur_Allot").Range("E" & var_row).Value = Worksheets("_Voyageur_Allot").Range("E" & var_row).Value - 8
                    Worksheets("_proc").Cells(Y, i).Value = Weekx
                    x = x + 1
                    If x = 21 Then GoTo Endit
                    Debug.Print "X : " & x
                    i = i + 1
                    Debug.Print Worksheets("_proc").Cells(Y, 2) & "  -  " & i
                    If i = 18 Then GoTo Skipit
                    GoTo repeat
                        Else:
                    Worksheets("_proc").Cells(Y, i).Value = "No_Allotment"
                    x = x + 1
                    If x = 21 Then GoTo Endit
                    'i = i + 1
                    If i = 18 Then GoTo Skipit
                        
                    GoTo repeat
                End If
                If x = 21 Then GoTo Endit
            End If
        End If
    End If
Skipit:
Endit:
    Debug.Print i
    If i < 18 Then
        Worksheets("_proc").Cells(Y, i).Resize(1, 18 - i) = "No_Allotment"
    End If
End If
Next Y
End Sub


Function Fill_us_No_Selection(ByVal rowstart As Integer)
Dim j As Integer
For j = 8 To 17
Worksheets("_proc").Cells(rowstart, j).Value = "No_Selection"
Next j
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
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