VBA Graphing

ameya

Board Regular
Joined
Jun 10, 2014
Messages
105
If I have two arrays, dates and values, how would I graph the two in VBA? (values on y axis, and dates on x axis).

I have created a userform for this, with inputs Initial Date, Final Date, and Graph Type.

In the code behind the form, I have established a range of dates, and the corresponding range of values.

How would I generate a bar graph with values (y axis) vs. dates (axis)?
How would I generate a line grpah with values (y axis) vs. dates (axis)?

The values are named rOEERange and the dates are labeled rDateRange. Below is my code I used to find the rOEERange and the rDateRange.

Code:
Dim rInitialDate As Range, rFinalDate As Range, rDateRange As Range
    
With Worksheets("Calculation")
        Set rInitialDate = .Find(What:=DateValue(InitialDate), After:=.Cells(1, 5), LookIn:=xlFormulas, LookAt:=xlWhole, SearchDirection:=xlNext, SearchFormat:=False)
        Set rFinalDate = .Find(What:=DateValue(FinalDate), After:=.Cells(1, 5), LookIn:=xlFormulas, LookAt:=xlWhole, SearchDirection:=xlPrevious, SearchFormat:=False)
    End With
    If rInitialDate Is Nothing Or rFinalDate Is Nothing Then
      MsgBox "Not able to find both dates"
    Else
      Set rDateRange = .Range(rInitialDate, rFinalDate)
    End If
End With
Dim startRow As Integer, endRow As Integer, rDateRangeIndex As Integer, rOEERange As Range
Set rInitialDate.Row = startRow
Set rFinalDate.Row = endRow
With Worksheets("Calculation")
    Set startOEECell = .Cells(startRow, 45)
    Set endOEECell = .Cells(endRow, 45)
    Set rOEERange = .Range(startOEECell, endOEECell)
End With
'to create new Range to reflect MC,Shift,Date
With Worksheets("Calculation")
    For i = startRow To endRow Step 1
        If Cells(i, 7) Is Not MC.Value Or Cells(i, 6) Is Not Shift.Value _
            Or DateValue(rDateRange(i - (startRow - 1))) < DateValue(InitialDate) _
            Or DateValue(rDateRange(i - (startRow - 1))) > DateValue(FinalDate) Then
                rDateRange (i - (startRow - 1)) Is Nothing
                rOEERange (i - (startRow - 1)) Is Nothing
        End If
    Next i
End With
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,214,606
Messages
6,120,479
Members
448,967
Latest member
visheshkotha

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