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

  • 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 !! – Rajesh Sinha May 07 '21 at 05: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. – Abdullah Al Mamun May 07 '21 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 ☺ – Rajesh Sinha May 08 '21 at 05:07

1 Answers1

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

Jon Peltier
  • 4,392
  • 22
  • 26
  • Hi @Jon Pettier,, I'm unable to reproduce the chart in the way U have suggested,, if possible share the GIF !! – Rajesh Sinha May 09 '21 at 05: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? – Abdullah Al Mamun May 10 '21 at 08: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. – Jon Peltier May 10 '21 at 12:16
  • I've included a download link for the workbook at the end of my answer. – Jon Peltier May 10 '21 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. – Máté Juhász May 12 '21 at 06:38
  • @MátéJuhász ,, I never work with negative mind set otherwise I would have down vote the post,,, my point is simple that Jon has by passed OP data & re created a data set to make the Chart,,, in case if OP has 100 Rows then the end user has to work hard to create correct data set,,, and is never a wise step,,, this is what I was trying to say ,, !! – Rajesh Sinha May 13 '21 at 05:02
  • @RajeshS The biggest problem people have with charts is getting the data into a suitable arrangement to use in a chart. The OP's data needed to be rearranged, or a chart could not be created that showed how he wanted the chart to represent the data. You can't claim I didn't use the original data just because I adapted its layout. If there were 100 rows, then a formula or VBA approach could be used to arrange the data. – Jon Peltier May 13 '21 at 12:17
  • @jon peltier ,, needless to say I'm aquated with all you are talking about since I'm professional data analyst,, my point is very simple,, if I or any one has suggested write solution should be appreciated,,!! – Rajesh Sinha May 13 '21 at 15:45
  • As a professional data analyst, you must know that sometimes, often, or always, you need to manipulate data in order to carry out your analysis. This does not mean changing the data or deleting it, it means changing the way the data is laid out tables or files or spreadsheets. That is exactly what I have done, while respecting the integrity of the data itself.. – Jon Peltier May 20 '21 at 15:02
  • @jon peltier, Your solution is great and working for me. However, is there any way I can automatically create the data tables (that you have created e.g one, two, three etc) from my original data set? Because I have a big dataset and it will take a lot of time to manually create it. Is there any easier way to do this? – Abdullah Al Mamun May 20 '21 at 19:22
  • @AbdullahAlMamun This can be automated with VBA or Power Query, but I think perhaps I might do it with formulas in an Excel Table. When I get a chance, I will look at it. What version of Excel are you using? – Jon Peltier May 21 '21 at 20:55
  • @Jon Peltier I am using Microsoft Office Professional Plus 2019. – Abdullah Al Mamun May 24 '21 at 16:02
  • @Jon Peltier The VBA process is great. I have some questions. How can I modify the VBA code for more than 3 entries? – Abdullah Al Mamun May 24 '21 at 18:58
  • @AbdullahAlMamun - Simply select as much data as you want to include. The program will use all the data you've selected. – Jon Peltier May 25 '21 at 20:53