To create a Gantt chart in Excel with timestamps for day, hour, and minute, you can use VBA code. Here are the steps to create a Gantt chart with timestamps:
Step 1: Prepare your data
Your data should be in the following format:
Columns B and C should contain timestamps in the format of "mm/dd/yyyy hh:mm:ss". Note that the format may vary depending on your system settings.
Step 2: Create a new module
In the Excel workbook, press ALT + F11 to open the Visual Basic Editor. Then, go to "Insert" > "Module" to create a new module.
Step 3: Insert the VBA code
Copy and paste the following VBA code into the module:
Sub CreateGanttChart()
Dim cht As Chart
Dim i As Integer
'Create a new chart
Set cht = ActiveSheet.Shapes.AddChart2(297, xlXYScatterLinesNoMarkers).Chart
'Set chart title
cht.HasTitle = True
cht.ChartTitle.Text = "Gantt Chart"
'Set X-axis and Y-axis labels
cht.Axes(xlCategory).HasTitle = True
cht.Axes(xlCategory).AxisTitle.Text = "Timeline"
cht.Axes(xlValue).HasTitle = True
cht.Axes(xlValue).AxisTitle.Text = "Task Name"
'Set X-axis to display time
cht.Axes(xlCategory).TickLabels.NumberFormat = "mm/dd/yyyy hh:mm:ss"
'Loop through the data and create series for each task
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
With cht.SeriesCollection.NewSeries
.Name = Range("A" & i).Value
.XValues = Array(Range("B" & i).Value, Range("C" & i).Value)
.Values = Array(i - 1, i - 1)
.ApplyDataLabels
.DataLabels.NumberFormat = Range("D" & i).Value
End With
Next i
End Sub
Step 4: Run the macro
In the Visual Basic Editor, go to "Debug" > "Compile VBAProject" to check for any errors in the code. Then, go back to the Excel workbook and press ALT + F8 to open the Macros dialog box. Select the "CreateGanttChart" macro and click "Run" to create the Gantt chart.
After running the macro, you should see a Gantt chart with timestamps for day, hour, and minute. The X-axis will display the timeline in the format of "mm/dd/yyyy hh:mm:ss". The Y-axis will display the task names. Each task will be represented as a horizontal line with a label indicating the task duration.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-05-07 11:00:00 +0000
Seen: 9 times
Last updated: Jul 28 '22
How to split an Excel file with a single column containing many records?
How can I use VBA to bring a csv file into MSAccess?
How can new worksheets be automatically generated and saved according to the date?
Can Visual Basic be used to code the Grep command?
How can I use VBA to create a border around cells that have text or a value in them?
How to rearrange slides using Excel VBA?
How can I use Excel VBA to transfer information from an Oracle table to an Excel worksheet?