2

I want to create a bar chart from the following data which will look like the attached picture. The data will have a start time, end time, and a number value.

Sample Data:

enter image description here

Bar Graph:
Bar Graph

Basically, I want to create a Bar Chart where in the X-axis I will have the date,time and I want to see from when to when the "number" values are ranging.

I tried to use the default bar graphs in Excel, but they are just showing each bar separately.

Normal bar chart:
normal bar chart

3
  • 1
    Please edit your post & share some sample data with us, also be specific whether you want to create TIME LINE chart or BAR chart !! Commented May 7, 2021 at 5:35
  • I have edited my post with sample data. Basically, I want to create a Bar Chart where in the X-axis I will have the date, time and I want to see from when to when the "number" values are ranging. Commented May 7, 2021 at 15:26
  • Hi @Abdullah Al Mamun now check my post,, I've solved the issue,, and I'm sure this is what you are looking for ☺ Commented May 8, 2021 at 5:07

1 Answer 1

3

Can't really do it with bars, though if you want filled areas, it starts with the approach below, then gets complicated.

You can rearrange your data into three separate blocks as shown below. Select the first block, and insert an XY Scatter chart. Select and copy the second block, select the chart, and use Paste Special to add the data as new series, with series names in first row and X values in first column. Repeat with third block. With a little formatting, it looks like the chart below.

enter image description here

Or you can rearrange your data into one block with some blank rows between sections. Create an XY Scatter chart, and do the formatting.

enter image description here

You can download my workbook from here: Bar With Start and End Time.xlsx

EDIT: VBA approach to arrange data.

I have written a VBA routine that starts with data like the first block in the screenshot below, does minimal validation (is it three columns, is there a header row), asks the user which output is desired (one series for each row or one series for all rows combined), asks the user where to put the output, then produces the appropriate output. The output cells link to the input cells, so if the user changes a value in the input range, the output value will reflect the change.

It's minimally documented, feel free to ask questions.

The user first selects the input range (or one cell in the input range) and runs the code.

After running the code, the user needs only select the output range (or one cell in the output range), and insert an XY Scatter Chart with Lines and no Markers.

VBA Routine: Input and Output Data

Here is the VBA procedure:

Sub Reformat_StartTimeCount_OneSeries()
  If TypeName(Selection) <> "Range" Then
    MsgBox "Select a range of data and try again.", vbExclamation, "No Data Selected"
    GoTo ExitSub
  End If
  
  ' input range: three columns (start, end, count), one row box, maybe header row
  Dim InputRange As Range
  Set InputRange = Selection
  If InputRange.Cells.Count = 1 Then
    Set InputRange = InputRange.CurrentRegion
  End If
  If InputRange.Columns.Count <> 3 Then
    MsgBox "Select a three-column range of data and try again.", vbExclamation, "No Data Selected"
    GoTo ExitSub
  End If
  
  ' one or multiple colors
  Dim Question As String
  Question = "Do you want one series (one line color) for each row of data?"
  Question = Question & vbNewLine & vbNewLine & "(Yes for multiple colors, No for one color)"
  Dim Answer As VbMsgBoxResult
  Answer = MsgBox(Question, vbQuestion + vbYesNo, "How Many Lines")
  If Answer = vbYes Then
    Dim MultipleSeries As Long
    MultipleSeries = 1
  End If
  
  ' ignore header row
  If Not IsNumeric(InputRange.Cells(1, 3)) Then
    Dim HasHeaderRow As Boolean
    HasHeaderRow = True
    With InputRange
      Set InputRange = .Offset(1).Resize(.Rows.Count - 1)
    End With
  End If
  
  ' how many rows?
  Dim RowCount As Long
  RowCount = InputRange.Rows.Count
  
  ' build array of formulas
  Dim OutputArray As Variant
  ReDim OutputArray(1 To RowCount * (5 - MultipleSeries) + MultipleSeries, 1 To 2 + MultipleSeries * (RowCount - 1))
  
  Dim RowIndex As Long
  For RowIndex = 1 To RowCount
    Dim RowBase As Long, ColumnBase As Long
    RowBase = (RowIndex - 1) * (5 - MultipleSeries)
    ColumnBase = 2 + MultipleSeries * (RowIndex - 1)
    If MultipleSeries Then
      If HasHeaderRow Then
        OutputArray(1, ColumnBase) = "=" & InputRange.Cells(0, 3).Address(False, False) & "&"" " & RowIndex & """"
      Else
        OutputArray(1, ColumnBase) = "Count " & RowIndex
      End If
    Else
      If RowIndex = 1 Then
        If HasHeaderRow Then
          OutputArray(RowBase + 1, 2) = "=" & InputRange.Cells(0, 3).Address(False, False)
        Else
          OutputArray(RowBase + 1, 2) = "Count"
        End If
      Else
        OutputArray(RowBase + 1, 2) = "#n/a"
      End If
    End If
    OutputArray(RowBase + 2, 1) = "=" & InputRange.Cells(RowIndex, 1).Address(False, False)
    OutputArray(RowBase + 3, 1) = "=" & InputRange.Cells(RowIndex, 1).Address(False, False)
    OutputArray(RowBase + 4, 1) = "=" & InputRange.Cells(RowIndex, 2).Address(False, False)
    OutputArray(RowBase + 5, 1) = "=" & InputRange.Cells(RowIndex, 2).Address(False, False)
    OutputArray(RowBase + 2, ColumnBase) = 0
    OutputArray(RowBase + 3, ColumnBase) = "=" & InputRange.Cells(RowIndex, 3).Address(False, False)
    OutputArray(RowBase + 4, ColumnBase) = "=" & InputRange.Cells(RowIndex, 3).Address(False, False)
    OutputArray(RowBase + 5, ColumnBase) = 0
  Next
  
  ' output formulas
  Dim OutputRange As Range
  On Error Resume Next
  Set OutputRange = Application.InputBox("Select the top left cell of the output range.", "Select Output Range", , , , , , 8)
  On Error GoTo 0
  If OutputRange Is Nothing Then GoTo ExitSub
  
  With OutputRange.Resize(RowCount * (5 - MultipleSeries) + MultipleSeries, 2 + MultipleSeries * (RowCount - 1))
    .Value2 = OutputArray
    .EntireColumn.AutoFit
  End With
  
ExitSub:
End Sub

I have uploaded a new workbook, which contains both parts of the answer. Download it here: Bar With Start and End Time.xlsm

14
  • Hi @Jon Pettier,, I'm unable to reproduce the chart in the way U have suggested,, if possible share the GIF !! Commented May 9, 2021 at 5:06
  • Hi @Jon Pettier, in your method, is the X axis in here refers to the actual date with time? Or is it just date values placed in order? Commented May 10, 2021 at 8:02
  • As stated, it is an XY Scatter Chart. The X values are date-time values, which are numerical, with the date being the whole number part and the time being the fractional part. Ill find the workbook and upload it. Commented May 10, 2021 at 12:16
  • I've included a download link for the workbook at the end of my answer. Commented May 10, 2021 at 17:53
  • @RajeshS: The chart and solution is perfectly correct, just because you don't see labels at each point doesn't mean chart is not correct. Please stop writing negative comments just because somebody else found a better solution than you. Commented May 12, 2021 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .