VBA: Gantt chart coloring

HansDK

New Member
Joined
Oct 19, 2021
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
Hi all. I am trying to create a dynamic gantt chart in excel through VBA. I am currently struggling with the offsetting the coloring part.

Below, you can see my code which I have produced so far. I am having problems with the final bit. I hope someone will be able to help me.

BR Hans.

VBA Code:
Sub gantt()
Application.ScreenUpdating = False
    
    Dim duration As Range
    Dim DateColumn As Range
  
    Dim h As Range
    Dim i As Range
    Dim j As Range
    Dim D1 As Long
    Dim D2 As Range
  
    Set DateColumn = Worksheets("Tender Schedule").Range("I11:I98") 'users will define day, month and year. DateColumn is used to put the date together. The date from DateColumn will be used as a starting point for the coloring
    Set duration = Worksheets("Tender Schedule").Range("H11:H98") 'duration is the expected duration which will define the length of the colored part.
  
    D1 = Worksheets("Tender Schedule").Range("M2") - 1 'D1 is the overall start of the project
  
    For Each h In DateColumn
        h.Value = Format(h.Offset(0, -4) & "-" & h.Offset(0, -3) & "-" & h.Offset(0, -2), "dd/mm/yyyy") ' This collects the date from the 3 cells to the left.
    Next h
  
  
    For Each D2 In DateColumn
    For Each i In duration
    If Int(i.Value) > 0 Then 'There are blank cells in the date column, as there are different sections of the Gantt chart. Therefore, not all cells should contain information
            D2 = CLng(VBA.DateValue(i.Offset(0, 1))) ' This collect "date 2" which is fed to DateColumn. This will be the beginning of a specific event/part project.
            'Debug.Print (D2)
            'Debug.Print (DateValue(D2) - DateValue(D1))
            i.Offset(0, (D2 - D1) + 1).Resize(0, i.Value).Interior.Color = vbGreen 'this should offset from the duration column and color the relevant part of the Gantt chart.
    End If
    Next i
    Next D2

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top