简体   繁体   中英

Excel VBA Attempting to set cell value as date from another cell

I have a spreadsheet where our organisation is required to submit sickness information on a weekly basis for staff. The spreadsheet has one tab for each week of the year and I already have a macro to copy employee details from one week to the next.

I'm attempting to build in the code below where it picks up staff who are still on sick leave and copies the start date of the sickness period to the following week. The code works by picking up an employee number as a string in the loop, then creating an object for the start date.

Due to the layout and other information on the templates I can't simply copy and paste the whole sheet from one week to the next.

REVISED The revised code below now copies the first SicknessStart date to NextWeek . However it only works on the first date and won't copy the information from the subsequent rows for some reason. The date format is also copying accross in US format but I'm looking into that.

Sub CopyDate

 Dim I As Integer, CopyDate As Boolean
 I = 6
 CopyDate = False

 'Use the Employee Number column (C) to perform the check on the sickness dates
 Do While CurrentWeek.Cells(I, 3) <> ""

'Check if there is a sickness start date in column R
If CurrentWeek.Cells(I, 18) <> "" Then
'Check if they have entered 'Still Away' or left the cell blank in column S
    If CurrentWeek.Cells(I, 19) = "Still Away" Or CurrentWeek.Cells(I, 19) = "" Then

        Dim EmployeeNumber As String
        Dim SicknessStart As String
            EmployeeNumber = Cells(I, 3)
            SicknessStart = Cells(I, 18)

        NextWeek.Select

'Find the employee number string on the following week's tab and enter sickness start date   
        Columns(3).Find(What:=EmployeeNumber, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Offset(0, 15) = SicknessStart

End If
End If

I = I + 1
Loop
CopySickness = True
End Sub

You don't show how CurrentWeek or NextWeek are declared or set. I'll assume they are global worksheet variables (a bad idea by the way, they should be parameters passed into this sub ) that are set elsewhere.


Then qualify all cell references with one or the other.

        EmployeeNumber = CurrentWeek.Cells(I, 3)
        SicknessStart = CurrentWeek.Cells(I, 18)

This line is the cause of your problems (delete it)
Why? Because on the second time through the unqualified EmployeeNumber = Cells(I, 3) etc refer to sheet NextWeek

NextWeek.Select

Find ing the Employee number using a Range variable

Dim rStartDate as Range

' replace your Columns(3).Find(... code with this
Set rStartDate = NextWeek.Columns(3).Find( _
  What:=EmployeeNumber, _
  LookIn:=xlValues, _
  LookAt:=xlWhole, _
  SearchOrder:=xlByRows, _
  SearchDirection:=xlNext, _
  MatchCase:=False, _
  SearchFormat:=False)
If Not rStartDate Is Nothing Then
    rStartDate.Offset(0, 15) = SicknessStart
Else
    ' EmployeeNumber is not found in NextWeek.Column(3)
End If

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