Const init_info As String = "InitInfo"
Const end_row_op As Integer = 100  'tmp
Const end_row_holiday As Integer = 100  'tmp
Const sheet_name_op As String = "Project_YY"  'tmp
Const sheet_name_holiday As String = "Holiday"  'tmp
Const column_index_task As Integer = 3 'tmp
Const column_index_cost As Integer = 5 'tmp
Const column_index_start_date As Integer = 6 'tmp
Const column_index_end_date As Integer = 7 'tmp
Const column_index_man As Integer = 8 'tmp
Const column_index_sort As Integer = 9 'tmp
Const column_index_holiday As Integer = 2 'tmp
Const column_index_workday As Integer = 3 'tmp

Function is_date_in_col(ByVal sheet_name As String, ByVal col As Integer, ByVal value As Date) As Integer
    Dim end_row As Integer
    end_row = end_row_holiday 'tmp  get_row_range
    
    Dim test_row As Integer
    Dim ret As Integer
    is_date_in_col = 0
    
    Dim col_date As Date
    
    For test_row = 3 To end_row
        col_date = Sheets(sheet_name).Cells(test_row, col).value
        If value = col_date Then
            is_date_in_col = test_row
            Exit For
        End If
    Next
End Function

Function is_date_in_holiday_list(ByVal in_date As Date) As Boolean
    
    If is_date_in_col(sheet_name_holiday, column_index_holiday, in_date) <> 0 Then
        is_date_in_holiday_list = True
    Else
        is_date_in_holiday_list = False
    End If
    
End Function

Function is_date_in_workday_list(ByVal in_date As Date) As Boolean
    
    If is_date_in_col(sheet_name_holiday, column_index_workday, in_date) <> 0 Then
        is_date_in_workday_list = True
    Else
        is_date_in_workday_list = False
    End If
    
End Function

Function is_satday_or_sunday(ByVal in_date As Date) As Boolean
    Dim wd As Integer
    wd = Weekday(in_date)
    
    If wd = vbSunday Or wd = vbSaturday Then
        is_satday_or_sunday = True
    Else
        is_satday_or_sunday = False
    End If
    
End Function

Function get_next_workday(ByVal wd As Date) As Date
    wd = wd + 1
    
    While True
    
        If is_date_in_holiday_list(wd) Then
            wd = wd + 1
            
        ElseIf is_date_in_workday_list(wd) Then
            get_next_workday = wd
            Exit Function
        
        ElseIf is_satday_or_sunday(wd) Then
            wd = wd + 1
            
        Else
            get_next_workday = wd
            Exit Function
            
        End If
        
    Wend

End Function

Function calc_end_date(ByVal start_date As Date, ByVal cost_from_begin As Double, ByRef end_date As Date, ByRef cost_used As Double)

    end_date = start_date
    cost_used = cost_from_begin
    
    While cost_from_begin > 1
        cost_from_begin = cost_from_begin - 1
        cost_used = cost_used - 1
        end_date = get_next_workday(end_date)
    Wend

End Function

Function get_next_task(ByRef row_edit As Integer) As Boolean

    get_next_task = False
    
    Dim in_row As Integer
    in_row = row_edit
    
    Dim new_sort As Integer
    new_sort = 0
    
    Dim in_sort As Integer
    in_sort = Sheets(sheet_name_op).Cells(in_row, column_index_sort).value
    
    Dim target_sort As Integer
    Dim tmp_sort As Integer
    target_sort = 32767
    
    For i = 3 To end_row_holiday
        tmp_sort = Sheets(sheet_name_op).Cells(i, column_index_sort).value
        
        If tmp_sort > in_sort Then
            get_next_task = True
            
            If tmp_sort < target_sort Then
                target_sort = tmp_sort
                row_edit = i
            End If
            
        End If
    Next

End Function

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column <> column_index_start_date Then
        Exit Sub
    End If
    
    Dim cost_used As Double
    cost_used = 0

    Dim row_edit As Integer
    row_edit = Target.Row
    
    Dim row_edit_cost As Double
    row_edit_cost = Sheets(sheet_name_op).Cells(row_edit, column_index_cost).value
    
    Dim start_date As Date
    start_date = Sheets(sheet_name_op).Cells(row_edit, column_index_start_date).value
    
    'If start_date = "" Then
    '    MsgBox "have not input start date"
    '    Exit Sub
    'End If
    
    'if start_date is not workday then error

    cost_from_begin = row_edit_cost + cost_used
    
    Dim end_date As Date
    end_date = start_date
    
    calc_end_date start_date, cost_from_begin, end_date, cost_used
    Sheets(sheet_name_op).Cells(row_edit, column_index_end_date).value = end_date
    
    'next edit row
    While get_next_task(row_edit)
        
        start_date = end_date
        If cost_used >= 1 Then
            start_date = get_next_workday(start_date)
            cost_used = cost_used - 1
        End If
        
        Sheets(sheet_name_op).Cells(row_edit, column_index_start_date).value = start_date
        
        calc_end_date start_date, cost_from_begin, end_date, cost_used
        Sheets(sheet_name_op).Cells(row_edit, column_index_end_date).value = end_date
        
    Wend
    
End Sub

schedule.csv

No.,TaskName,,Cost,StartDate,EndDate,Man,Sort
1,TaskX,,3,,,,
2,,SubX1,1,2022/1/26,2022/1/27,Gu,2
3,,SubX2,2,,,Li,
4,TaskY,,5,,,,
5,,SubY1,2,,,Rui,
6,,SubY2,2,2022/1/24,2022/1/25,Gu,1
7,,SubY3,1,,,Rui,
8,TaskZ,,8,,,,
9,,SubZ1,3,,,Li,
10,,SubZ2,2,2022/1/29,2022/1/30,Gu,3
11,,SubZ3,3,,,Rui,

vba_pj_0001_auto_date的更多相关文章

  1. TCP/IP笔记(1)

    TCP/IP 背景和介绍 上世纪 70 年代,随着计算机技术的发展,计算机使用者意识到:要想发挥计算机更大的作用,就要将世界各地的计算机连接起来.但是简单的连接是远远不够的,因为计算机之间无法沟通.因 ...

随机推荐

  1. 嵌入式Linux—FreeType矢量字体

    freetype矢量字体 常用API 1.FT_Init_FreeType函数是FreeType库中的一个函数,其作用是初始化FreeType库,并返回一个指向FT_Library对象的指针.下面是该 ...

  2. JZOJ 2020.02.25【NOIP提高组】模拟A 组

    闲话 难度似乎比之前的简单了一些 但是难的题还是很难(我太菜了) 总结 针对三个题,先罗列正解所涉及的算法:字符哈希,组合数学,点分治 最后一个不会 组合数学?还好吧 字符哈希? 放在 \(T1\) ...

  3. 题解 P1627 [CQOI2009] 中位数

    傻逼题但是被自己的傻逼操作爆了好几次零(悲愤 .... 没什么好讲的,一眼题... //SIXIANG #include <iostream> #define int long long ...

  4. Golang如何快速构建一个CLI小工示例

    这篇文章主要为大家介绍了Golang如何快速构建一个CLI小工具详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪 如何Golang快速构建一个CLI小工具 在现实开发的 ...

  5. (unsigned)short溢出后隐式转换为int

    同学被面试官问到一个细节中的细节,虽然个人觉得意义不大,但还是好奇并在和同学一起实验后搞清楚了,记录一下 如下: int main() { unsigned short a = 65535, b = ...

  6. qt_2d画图

    qt中如何画图? 使用Qpainter类进行2D绘画 例如以下代码进行直线的绘制 QPainter painter(this);painter.setPen(Qt::red);painter.draw ...

  7. SAP SPOOL_INTERNAL_ERROR

    运行SE38程序 RSPO0041

  8. 正点原子sys.h文档详解

    文档主体内容为位带的映射,目的是实现位带操作. 第一部分:位带映射的宏函数 1 #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x200 ...

  9. vue2中请求函数防抖处理

  10. Hanlp 在Python环境中安装、介绍及使用

    Hanlp HanLP是由一系列模型与算法组成的Java工具包,目标是普及自然语言处理在生产环境中的应用.HanLP具备功能完善.性能高效.架构清晰.语料时新.可自定义的特点. 功能:中文分词 词性标 ...