Sunday, April 2, 2017

Charts

Option Explicit
'Basic Excel Pie Chart
Sub Chart_Exercise()

Dim rng As Range
Dim chrt As Chart

    'Intialization
    Set rng = Range("A1").CurrentRegion
    Set chrt = Sheet1.Shapes.AddChart.Chart

        'Chart Properties
        chrt.SetSourceData rng
        chrt.ChartType = xl3DPie
        chrt.ChartTitle.Text = "Quarterly Sales Figure"
End Sub
--------------------------------------------------------------------------------------------------------------------------
'Macro to user selection through radio button and change in Chart




Private Sub ComboBox1_Change()


'Variable Declaration
Dim rng As Range
Dim chrt As ChartObject

'----- Range Initialization

Set rng = Range("D3").CurrentRegion

'----- Chart Intialization

Set chrt = Sheets("Chart").ChartObjects(1)

'----- Chart Properties

chrt.Chart.SetSourceData rng
chrt.Chart.ChartTitle.Text = "Monthly Sales Figure"

'----- Conditions against dropdown

If ComboBox1.Value = "Bar" Then
    chrt.Chart.ChartType = xl3DBarStacked
   
ElseIf ComboBox1.Value = "Pie" Then
    chrt.Chart.ChartType = xlPie

ElseIf ComboBox1.Value = "Line" Then
    chrt.Chart.ChartType = xlLine

ElseIf ComboBox1.Value = "Area" Then
    chrt.Chart.ChartType = xlArea

End If

End Sub

Private Sub CommandButton1_Click()
'----- Variable declartion
Dim tme As Date

'----- Time initialization
tme = Now

'-----Loop to refresh sheet

Do Until Now > tme + (1 / 24 / 60 / 60) 'Adding 10 sec to time
   
    Sheets("Chart").Calculate

Loop

End Sub



Private Sub OptionButton1_Click()

'Variable Declaration
Dim rng As Range
Dim chrt As ChartObject

'----- Range Initialization

Set rng = Range("D3").CurrentRegion

'----- Chart Intialization

Set chrt = Sheets("Chart").ChartObjects(1)

'----- Chart Properties

chrt.Chart.SetSourceData rng
chrt.Chart.ChartTitle.Text = "Monthly Sales Figure"
chrt.Chart.ChartType = xl3DLine

End Sub

Private Sub OptionButton2_Click()
'Variable Declaration
Dim rng As Range
Dim chrt As ChartObject

'----- Range Initialization

Set rng = Range("D3").CurrentRegion

'----- Chart Intialization

Set chrt = Sheets("Chart").ChartObjects(1)

'----- Chart Properties

chrt.Chart.SetSourceData rng
chrt.Chart.ChartType = xlBarClustered
End Sub

Private Sub OptionButton3_Click()
'Variable Declaration
Dim rng As Range
Dim chrt As ChartObject

'----- Range Initialization

Set rng = Range("D3").CurrentRegion

'----- Chart Intialization

Set chrt = Sheets("Chart").ChartObjects(1)

'----- Chart Properties

chrt.Chart.SetSourceData rng
chrt.Chart.ChartType = xlPie
End Sub

Private Sub OptionButton4_Click()
'Variable Declaration
Dim rng As Range
Dim chrt As ChartObject

'----- Range Initialization

Set rng = Range("D3").CurrentRegion

'----- Chart Intialization

Set chrt = Sheets("Chart").ChartObjects(1)

'----- Chart Properties

chrt.Chart.SetSourceData rng
chrt.Chart.ChartType = xlArea
End Sub

No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...