0

我有一个电子表格,要求我们的组织每周为员工提交疾病信息。电子表格一年中的每一周都有一个选项卡,我已经有一个宏可以将员工详细信息从一周复制到下一周。

我正在尝试在下面的代码中构建,它会挑选仍在休病假的员工,并将病假的开始日期复制到下一周。该代码通过在循环中选取一个员工编号作为字符串来工作,然后为开始日期创建一个对象。

由于模板上的布局和其他信息,我不能简单地从一周到下一周复制和粘贴整张工作表。

已修订 下面的修订代码现在将第一个SicknessStart日期复制到NextWeek. 但是,它仅适用于第一个日期,并且由于某种原因不会从后续行中复制信息。日期格式也在以美国格式复制,但我正在调查。

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
4

1 回答 1

1

您没有显示CurrentWeek或如何NextWeek声明或设置。我假设它们是在其他地方设置的全局worksheet变量(顺便说一句,它们应该是传递给 this 的参数sub)。


然后用一个或另一个限定所有 cell引用。

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

这条线是你的问题的原因(删除它)
为什么?因为第二次通过不合格EmployeeNumber = Cells(I, 3)等参考表NextWeek

NextWeek.Select

Find使用Range变量设置员工编号

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
于 2013-01-09T12:51:46.117 回答