การทำ Gantt Chart ด้วย Excel



หลายคนคงอยากเขียนตารางการทำงานโดยใช้ Excel และไม่ต้องพึ่งโปรแกรมอย่าง Microsoft Project วันนี้เรามาลอง ทำตารางการทำงานกันบน excel ดูครับ
ความรู้ที่ต้องมีคือ การใช้งาน Excel พื้นฐาน และ การเขียน VBA บน Excel


วิธีการทำ
0 สร้าง macro vba ตาม code ท้ายบทความ
1 ใส่ข้อมูลพื้นฐานสำหรับการจัดการโครงการ เช่น Task , เวลาที่ใช้ในการทำ
2 วันที่ตรงหมายเลข  1 ในรูปเป็นการใส่วันที่เรียงไปเรื่อยๆ หรือใช้สูตร +a1+1 แล้ว copy ไปทาง row (แล้วแต่ถนัดครับ)
3 วันที่ start date ตรงหมายเลข 2 ได้จาก Function  =startDate(D4)
4 วันที่เริ่มต้นของโครงการเราต้องกำหนดด้วยตัว E ตามหมายเลข 3 ในรูป
     S = StartDate
     X = WorkDate
     E = EndDate
5 การใส่สัญญาลักษณ์ S,X,E ในแต่ละ row ทำโดย Function mFill  และ Function Fill4Line สำหรับการใส่สัญญาลักษณ์ครั้งละ 4 แถว

ตอนต่อไปจะเขียนถึงเรื่อง Conditional Format ครับ

Sub Fill4line()

        ar = ActiveCell.Row
        For i = 1 To 4
               Call mFill
               Cells(ActiveCell.Row + 1, 5).Select
        Next
End Sub

Sub mFill()

    'Range("E4").Select

    startcol = 5
    ar = ActiveCell.Row
    For i = 5 To 200
        If Cells(ar - 1, i) = "E" Then
            startcol = i + 1
        End If
    Next

    j = Cells(ar, 4)
    i = 0
    While j <> 0

        Cells(ar, startcol + i).Select

        If i = 0 Then

            Cells(ar - 1, startcol + i) = Format(Cells(2, startcol + i), "dd mmm yy")
            Cells(ar - 1, startcol + i).Font.Color = RGB(0, 0, 255)
            ActiveCell.FormulaR1C1 = "S"
        Else
            ActiveCell.FormulaR1C1 = "X"
        End If
        If isHoliday(Cells(2, startcol + i)) Then
            Selection.Font.Color = 255
            j = j + 1
        Else
            Selection.Font.Color = 0
        End If
        j = j - 1
        i = i + 1

    Wend

    Range(Cells(ar, startcol + i), Cells(ar, startcol + i + 100)) = ""

    ActiveCell.FormulaR1C1 = "E"

'    Range("E5").Select

End Sub

Function isHoliday(d As Date)


    isHoliday = False
    If IsNull(d) Then
        Exit Function
    End If
    If Trim(d) = "" Then
        Exit Function
    End If

    If Weekday(d) = 1 Or Weekday(d) = 7 Then
        isHoliday = True
    Else
        isHoliday = False
    End If


    Dim dArray(10) As Date
    Dim i As Long

    dArray(1) = DateValue("31 dec 2555")
    dArray(2) = DateValue("1 jan 2556")
    dArray(3) = DateValue("25 feb 2556")
    dArray(4) = DateValue("6 apr 2556")
    dArray(5) = DateValue("8 apr 2556")
    dArray(6) = DateValue("15 apr 2556")
    dArray(7) = DateValue("16 apr 2556")
    dArray(8) = DateValue("6 may 2556")
    dArray(9) = DateValue("13 may 2556")
    dArray(10) = DateValue("24 may 2556")


    For i = LBound(dArray) To UBound(dArray)
        If dArray(i) = d Then
            isHoliday = True
        End If
    Next i

End Function


Function startDate(cell As Variant)

        'startDate = 5
        'startDate = cell.Row
        Dim d As Date
         For i = 4 To 300
                If Cells(cell.Row, i) = "S" Then
                        startDate = Cells(2, i)
                End If
         Next
         d = startDate
         startDate = NextWorkDate(d)

End Function

Function NextWorkDate(d As Date)

        While isHoliday(d)
            d = d + 1
        Wend
        NextWorkDate = d

End Function



Create Date : 12 มกราคม 2556
Last Update : 16 มกราคม 2556 10:15:02 น.
Counter : 18181 Pageviews.

3 comments
  
สำหรับผึ้งน้อยใช้ Conditional Formatting ค่ะ เพราะถ้าเปลี่ยนวันและเวลา แถบสีจะเปลี่ยนให้โดยอัตโนมัติโดยไม่ต้อง Run VBA ใหม่
โดย: ผึ้งน้อย19711128 IP: 58.11.206.126 วันที่: 12 มกราคม 2556 เวลา:21:17:12 น.
  
ลืมบอกไปว่า สีที่เห็นก็ใช้ Conditional Format เหมือนกัน เอาไว้รอตอน 2 นะครับ สีน้ำเงิน สีเทา และ สีเหลืองที่เห็น ทำจาก Conditional Format ครับ หลิ่วตา ส่วน VBA เอาไว้สร้าง รหัส X,S,E และตรวจวันหยุด ขอบคุณที่ติชมครับ

thaiger_u
วันเสาร์ เวลา 21:29 น.

ความคิดเห็นที่ 3
อ้อ ถ้าเช่นนั้นก็ใช้ Conditional Formatting ร่วมกับกับสูตร Weekday ค่ะ เพื่อตรวจสอบว่าวันไหนเป็นวันหยุด ให้ทำรูปแบบของเซลล์ให้แปลกประหลาดกว่าเซลล์วันธรรมดา

กรณีของวันหยุดนักขัตฤกษ์ ก็ต้องมีตารางรายการวันหยุดนักขัตฤกษ์แล้วใช้สูตร Match ร่วมกับ Conditional Formatting ค่ะ
แก้ไขข้อความเมื่อ วันเสาร์ เวลา 23:18 น.

ผึ้งน้อย19711128
วันเสาร์ เวลา 23:15 น.

ความคิดเห็นที่ 4
ไม่ได้แยกตารางวันหยุดครับ แต่ใส่วันหยุดใน function isHoliday
ถ้าต้องการแยกตารางต้องปรับโค้ดนิดหน่อยครับ
โดย: thaiger_u วันที่: 15 มกราคม 2556 เวลา:21:33:43 น.
  
excellent
โดย: pandit IP: 58.9.117.178 วันที่: 8 พฤษภาคม 2556 เวลา:16:48:45 น.
ชื่อ :
Comment :
 *ใช้ code html ตกแต่งข้อความได้เฉพาะสมาชิก
 

thaiger_u
Location :
  

[ดู Profile ทั้งหมด]
 ฝากข้อความหลังไมค์
 Rss Feed
 Smember
 ผู้ติดตามบล็อก : 3 คน [?]



Dancinga
@ฟรี
โปรแกรมปฏิทิน 2564 - Free android app


@ รับสอน เขียนโปรแกรม Python ระดับมัธยมปลาย
มกราคม 2556

 
 
1
2
3
4
5
6
7
8
9
10
11
13
14
15
17
18
19
21
22
23
24
26
27
28
29
31
 
 
All Blog