Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代码优化-Excel VBA集合和集合排序_Excel_Vba - Fatal编程技术网

代码优化-Excel VBA集合和集合排序

代码优化-Excel VBA集合和集合排序,excel,vba,Excel,Vba,我有一张excel表格,上面有员工的详细信息。我需要在另一张纸上展示一些关于他们在公司工作年限的细节 我想展示的细节是 本月纪念日 下个月的周年纪念日 本周纪念日 下周是周年纪念日 今天的周年纪念日 我需要显示员工姓名、周年纪念日和在公司工作年限的详细信息。这些细节中的每一个都应该显示在一个带有标题的表格中,并且它们在相同的列(B、C和D)中 所有这些都是由下面的代码完成的,但排序功能不起作用,我需要知道在这种情况下是否有更有效的方法使用集合 这是我的密码 Sub PopulateAnniver

我有一张excel表格,上面有员工的详细信息。我需要在另一张纸上展示一些关于他们在公司工作年限的细节

我想展示的细节是

  • 本月纪念日
  • 下个月的周年纪念日
  • 本周纪念日
  • 下周是周年纪念日
  • 今天的周年纪念日
  • 我需要显示员工姓名、周年纪念日和在公司工作年限的详细信息。这些细节中的每一个都应该显示在一个带有标题的表格中,并且它们在相同的列(B、C和D)中

    所有这些都是由下面的代码完成的,但排序功能不起作用,我需要知道在这种情况下是否有更有效的方法使用集合

    这是我的密码

    Sub PopulateAnniversaryData()
        'Declaring Collections
        Set TodayAnv = New Collection           'collection to store anniversaries today.
        Set ThisWeekAnv = New Collection        'collection to store anniversaries this week.
        Set NextWeekAnv = New Collection        'collection to store anniversaries next week.
        Set CurrentMonthAnv = New Collection    'collection to store anniversaries of current month.
        Set NextMonthAnv = New Collection       'collection to store anniversaries of next month.
    
    
        'getting current details
        CurrentDay = Day(Now())                                             'getting current year.
        CurrentMonth = Month(Now())                                         'getting current month.
        CurrentYear = Year(Now())                                           'getting current year.
        CurrentWeek = Application.WorksheetFunction.WeekNum(Now())          'getting the current week number.
        CurrentDate = Year(Now()) & "/" & Month(Now()) & "/" & Day(Now())   'forming current date.
    
        EmpDetailsLR = LastRowInColumn(1, ED.Name)  'finding the last row in employee details page.
        Dim EmpADate As Date    'declaring a variable to hold employee anniversary date.
        For EmpDetailsFR = 2 To EmpDetailsLR
            JoiningMonth = Month(ED.Range(JoinDateColumnNa & EmpDetailsFR).Value)   'finding employee joining month.
            JoiningDay = Day(ED.Range(JoinDateColumnNa & EmpDetailsFR).Value)       'finding employee joining day.
            JoiningYear = Year(ED.Range(JoinDateColumnNa & EmpDetailsFR).Value)     'finding employee joining year.
            YearsInEY = CurrentYear - JoiningYear                                   'finding number of years employee worked for EY.
            EmpName = ED.Range("C" & EmpDetailsFR).Value                            'finding Employee name.
            EmpJDate = ED.Range(JoinDateColumnNa & EmpDetailsFR).Value              'finding Employee joining date.
            EmpADate = Year(Now()) & "/" & Month(EmpJDate) & "/" & Day(EmpJDate)    'forming employee anniversary date.
            JoiningWeek = Application.WorksheetFunction.WeekNum(EmpADate)           'finding employee joining week.
    
            If Trim(LCase(ED.Range("H" & EmpDetailsFR).Value)) <> "resigned" And YearsInEY > 0 Then
                'Finding employees with anniversary today.
                If CurrentDate = EmpADate Then _
                    TodayAnv.Add Array(EmpName, "Today", YearsInEY)
                'Finding employees with anniversary this week.
                If CurrentWeek = JoiningWeek Then _
                    ThisWeekAnv.Add Array(EmpName, WeekDayName(EmpADate), YearsInEY)
                'Finding employees with anniversary next week.
                If CurrentWeek + 1 = JoiningWeek Then _
                    NextWeekAnv.Add Array(EmpName, EmpADate, YearsInEY)
                'Finding employees with anniversary this month.
                If CurrentMonth = JoiningMonth Then _
                    CurrentMonthAnv.Add Array(EmpName, EmpADate, YearsInEY)
                'Finding employees with anniversary next month.
                If CurrentMonth + 1 = JoiningMonth Then _
                    NextMonthAnv.Add Array(EmpName, EmpADate, YearsInEY)
            End If
        Next
    
        'sorting current month anniversaries based on anniversary date.
        For Collection_Counti = 1 To CurrentMonthAnv.Count - 1
            For Collection_Countj = Collection_Counti + 1 To CurrentMonthAnv.Count
                If CurrentMonthAnv(Collection_Counti)(1) > CurrentMonthAnv(Collection_Countj)(1) Then
                    'store the lesser item
                    vTemp = CurrentMonthAnv(Collection_Countj)
                    'remove the lesser item
                    CurrentMonthAnv.Remove Collection_Countj
                    're-add the lesser item before the greater Item
                    CurrentMonthAnv.Add vTemp(Collection_Counti)
                End If
            Next Collection_Countj
        Next Collection_Counti
    
    
        'sorting next month anniversaries based on anniversary date.
        For Collection_Counti = 1 To NextMonthAnv.Count - 1
            For Collection_Countj = Collection_Counti + 1 To NextMonthAnv.Count
                If NextMonthAnv(Collection_Counti)(1) > NextMonthAnv(Collection_Countj)(1) Then
                    'store the lesser item
                    vTemp2 = NextMonthAnv(Collection_Countj)
                    'remove the lesser item
                    NextMonthAnv.Remove Collection_Countj
                    're-add the lesser item before the greater Item
                    NextMonthAnv.Add vTemp2(Collection_Counti)
                End If
            Next Collection_Countj
        Next Collection_Counti
    
        WriteInRow = 3
        'populating anniversaries this month
        If CurrentMonthAnv.Count <> 0 Then
            AN.Range("B2").Value = "Anniversaries This Month"
            AN.Range("C2").Value = "Date"
            AN.Range("D2").Value = "Years In EY"
            For AnvDic = 1 To CurrentMonthAnv.Count
                AN.Range("B" & WriteInRow).Value = CurrentMonthAnv(AnvDic)(0)
                AN.Range("C" & WriteInRow).Value = CurrentMonthAnv(AnvDic)(1)
                AN.Range("D" & WriteInRow).Value = CurrentMonthAnv(AnvDic)(2)
                WriteInRow = WriteInRow + 1
            Next
            WriteInRow = WriteInRow + 1
        End If
    
        'populating anniversaries next month
        If NextMonthAnv.Count <> 0 Then
            AN.Range("B" & WriteInRow).Value = "Anniversaries Next Month"
            AN.Range("C" & WriteInRow).Value = "Date"
            AN.Range("D" & WriteInRow).Value = "Years In EY"
            WriteInRow = WriteInRow + 1
            For AnvDic = 1 To NextMonthAnv.Count
                AN.Range("B" & WriteInRow).Value = NextMonthAnv(AnvDic)(0)
                AN.Range("C" & WriteInRow).Value = NextMonthAnv(AnvDic)(1)
                AN.Range("D" & WriteInRow).Value = NextMonthAnv(AnvDic)(2)
                WriteInRow = WriteInRow + 1
            Next
        End If
    
        'similarly I will populate anniv this week, next week, today etc
    
        ActiveSheet.Columns.AutoFit
    End Sub
    
    Sub-PopulateAnniversaryData()
    '声明集合
    将TodayAnv=New Collection'集合设置为今天存储周年纪念。
    将ThisWeekAnv=New Collection'集合设置为本周存储周年纪念。
    将NextWeekAnv=New Collection'集合设置为下周存储周年纪念。
    将CurrentMonthAnv=New Collection'集合设置为存储当月的周年纪念日。
    将NextMonthAnv=New Collection'集合设置为存储下个月的周年纪念日。
    “获取当前详细信息
    CurrentDay=Day(Now())'正在获取当前年份。
    CurrentMonth=Month(Now())'正在获取当前月份。
    CurrentYear=Year(Now())'正在获取当前年份。
    CurrentWeek=Application.WorksheetFunction.WeekNum(现在())'获取当前周数。
    CurrentDate=构成当前日期的年(现在())&“/”月(现在())&“/”日(现在())”。
    EmpDetailsLR=LastRowInColumn(1,ED.Name)'查找员工详细信息页面中的最后一行。
    Dim EmpADate As Date'声明用于保存员工周年纪念日的变量。
    对于EmpDetailsFR=2到EmpDetailsLR
    JoiningMonth=Month(ED.Range(JoinDateColumnNa&EmpDetailsFR.Value))查找员工加入月份。
    JoiningDay=Day(ED.Range(JoinDateColumnNa&EmpDetailsFR.Value))查找员工加入日期。
    JoiningYear=Year(ED.Range(JoinDateColumnNa&EmpDetailsFR.Value))查找员工加入年份。
    YearsInEY=当前年份-JoiningYear'查找员工为安永工作的年数。
    EmpName=ED.Range(“C”&EmpDetailsFR).Value'查找员工姓名。
    EmpJDate=ED.Range(JoinDateColumnNa&EmpDetailsFR)。值“查找员工加入日期”。
    EmpADate=构成员工周年纪念日的年(现在())&“/”月(EmpJDate)&“/”日(EmpJDate)。
    JoiningWeek=Application.WorksheetFunction.WeekNum(EmpADate)'查找员工加入周。
    如果Trim(LCase(ED.Range(“H”和EmpDetailsFR.Value))“辞职”且YearsInEY>0,则
    “今天找周年纪念日的员工。
    如果CurrentDate=EmpADate,则_
    添加数组(EmpName,“今天”,YearsInEY)
    “本周寻找周年纪念日员工。
    如果CurrentWeek=JoiningWeek,则_
    ThisWeekAnv.Add数组(EmpName、WeekDayName(EmpADate)、YearsInEY)
    “寻找下周有周年纪念日的员工。
    如果CurrentWeek+1=JoiningWeek,则_
    NextWeekAnv.Add数组(EmpName、EmpADate、YearsInEY)
    “寻找本月有周年纪念日的员工。
    如果CurrentMonth=JoiningMonth,则_
    CurrentMonthAnv.Add数组(EmpName、EmpADate、YearsInEY)
    “下个月将迎来员工周年纪念日。
    如果CurrentMonth+1=合并月份,则_
    下一个月添加数组(EmpName、EmpADate、YearsInEY)
    如果结束
    下一个
    '根据周年日期排序当前月份周年纪念。
    对于集合_Counti=1到CurrentMonthAnv.Count-1
    对于Collection_Countj=Collection_Counti+1到CurrentMonthAnv.Count
    如果CurrentMonthAnv(集合计数)(1)>CurrentMonthAnv(集合计数)(1),则
    '存储较小的项目
    vTemp=CurrentMonthAnv(集合计数j)
    '删除较小的项目
    CurrentMonthAnv.删除集合\u Countj
    '在较大项之前重新添加较小项
    当前月份添加vTemp(集合计数)
    如果结束
    下一集
    下一集
    '根据周年纪念日对下个月的周年纪念日进行排序。
    对于集合_Counti=1到下一个月v.Count-1
    对于集合_Countj=集合_Counti+1到下一个月的v.Count
    如果下一个月(收集计数)(1)>下一个月(收集计数)(1),则
    '存储较小的项目
    vTemp2=NextMonthAnv(收集计数j)
    '删除较小的项目
    下个月v.删除集合\u Countj
    '在较大项之前重新添加较小项
    下个月添加vTemp2(收集计数)
    如果结束
    下一集
    下一集
    WriteInRow=3
    “本月的周年纪念日
    如果CurrentMonthAnv.计数为0,则
    AN.Range(“B2”).Value=“本月周年纪念日”
    a.范围(“C2”).Value=“日期”
    A.范围(“D2”).Value=“安永年”
    对于AnvDic=1到CurrentMonthAnv.Count
    AN.Range(“B”和WriteInRow).Value=CurrentMonthAnv(AnvDic)(0)
    范围(“C”和WriteInRow).Value=CurrentMonthAnv(AnvDic)(1)
    一个.Range(“D”和WriteInRow).Value=CurrentMonthAnv(AnvDic)(2)
    WriteInRow=WriteInRow+1
    下一个
    WriteInRow=WriteInRow+1
    如果结束
    "人口",
    
    Public EmployeeName as string
    Public EmployeeDate as Date
    Public YearsInEY as string
    
    Dim oTodayAnniversary as new Anniversary
    oTodayAnniversary.EmployeeName = value
    oTodayAnniversary.EmployeeDate  = value
    oTodayAnniversary.YearsInEY  = value