简体   繁体   中英

Update value in a column from another worksheet depending on the date in a range

Update value in a column from another worksheet depending on the date in a range

Our new pay system at work went live in August 2017.

This is the new pay calendar (called Pay Periods)

Start Date End Date Pay period

10 Aug 17 23 Aug 17 PP0001

24 Aug 17 06 Sep 17 PP0002

07 Sep 17 20 Sep 17 PP0003

21 Sep 17 04 Oct 17 PP0004

05 Oct 17 18 Oct 17 PP0005

19 Oct 17 01 Nov 17 PP0006

This is the table for members (called MemberList)

Name Due Date Amount Processed in Pay Cal

Raghu 15 Jul 17 $248.00 PP0001

Vima 20 Jul 17 $354.00 PP0001

Abhi 10 Aug 17 $954.00 PP0001

Neelima 23 Aug 17 $134.00 PP0001

Raghu 14 Sep 17 $134.00 PP0003

Vima 21 Sep 17 $524.00 PP0004

Abhi 06 Oct 17 $332.00 PP0005

Neelima 20 Oct17 $158.00 PP0006

Raghu 06 Sep16 $456.00 PP0002

Vima 19 Sep 17 $159.00 PP0003

Abhi 03 Oct 17 $357.00 PP0004

Neelima 18 Oct 17 $852.00 PP0005

I want to update the Processed in Pay Cal column with the pay periods.

Anything with a due date on or 23 AUG 17 to be processed in PP0001 and the rest of the appropriate pay calendar. I need help with the code to write a macro.

Pseudo code:

Sub updateColunm4()

If duedate <= 23Aug17 then

    Update column 4 to PP0001

Else

    Update column 4 to the appropriate pay period

End if

End Sub

Thank you

Regards

Raghu

Here is an UDF you can use. You can run the test procedure to see the result, if your data is set-up as in attached image. Or simply deploy udf direct.

It's signature is

GetPayPeriod(dueDate, lookupTable)

The function name is GetPayPeriod which you call from in the cell by putting

=GetPayPeriod(dueDate, lookupTable)

dueDate is the date for which you want the pay period. lookupTable is the range to look in.

Public Sub test()

Dim lookupTable As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet3") 'change as appropriate
Set lookupTable = ws.Range("A2:C7")

Dim dueDate As Date

dueDate = ws.Range("B15").Value2

MsgBox GetPayPeriod(dueDate, lookupTable)

End Sub

Public Function GetPayPeriod(ByVal dueDate As Date, ByVal lookupTable As Range) As String

If dueDate <= 42970 Then '23rd Aug 17
    GetPayPeriod = "PP0001"
    Exit Function
End If

Dim payPeriodsArray()

payPeriodsArray = lookupTable

Dim i As Long

For i = LBound(payPeriodsArray, 1) To UBound(payPeriodsArray, 1)

   If dueDate >= payPeriodsArray(i, 1) And dueDate <= payPeriodsArray(i, 2) Then

        GetPayPeriod = payPeriodsArray(i, 3)
        Exit Function

   End If

Next i

GetPayPeriod = "Period not found"

End Function

Example usage in the sheet:

在工作表中的用法

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