Whenever you need to draw dynamic chart in a userform depending on parameters & factors of survey as mentioned above,VBA macro code is mentioned below.You will have to select one parameter at a time mentioned at the left hand side of the chart.The only requirement for this code is to set focus of your cursors on data points on an excel spreadsheet and to load an image control in form
Private Sub UserForm_initialize()
Call showGraph
End Sub
Sub showGraph()
Dim currentrow As Long
Dim currentchart As Chart
Dim pathname As String
Dim chartheader, datasource, chartdatasource As Range
currentrow = ActiveCell.Row
On Error GoTo Errorhandler
If currentrow <= 1 Then
MsgBox "Move cell pointer to a row that contains data of Row Labels"
End If
Set chartheader = ActiveSheet.Range("A1:F1")
Set datasource = ActiveSheet.Range(Cells(currentrow, 1), Cells(currentrow, 6))
Set chartdatasource = Union(chartheader, datasource)
Range(chartdatasource.Address).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=chartdatasource, PlotBy:=xlRows
ActiveChart.ChartType = xlColumnClustered
pathname = ThisWorkbook.Path & "\temp.gif"
If Not Dir(pathname) = "" Then
Kill pathname
End If
ActiveChart.Export Filename:=pathname, filtername:="gif"
pathname = ThisWorkbook.Path & "\temp.gif"
UserForm1.Image1.Picture = LoadPicture(pathname)
ActiveSheet.ChartObjects.Delete
Exit Sub
Errorhandler:
MsgBox "Error description" & Err.Description
End Sub
Private Sub UserForm_initialize()
Call showGraph
End Sub
Sub showGraph()
Dim currentrow As Long
Dim currentchart As Chart
Dim pathname As String
Dim chartheader, datasource, chartdatasource As Range
currentrow = ActiveCell.Row
On Error GoTo Errorhandler
If currentrow <= 1 Then
MsgBox "Move cell pointer to a row that contains data of Row Labels"
End If
Set chartheader = ActiveSheet.Range("A1:F1")
Set datasource = ActiveSheet.Range(Cells(currentrow, 1), Cells(currentrow, 6))
Set chartdatasource = Union(chartheader, datasource)
Range(chartdatasource.Address).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=chartdatasource, PlotBy:=xlRows
ActiveChart.ChartType = xlColumnClustered
pathname = ThisWorkbook.Path & "\temp.gif"
If Not Dir(pathname) = "" Then
Kill pathname
End If
ActiveChart.Export Filename:=pathname, filtername:="gif"
pathname = ThisWorkbook.Path & "\temp.gif"
UserForm1.Image1.Picture = LoadPicture(pathname)
ActiveSheet.ChartObjects.Delete
Exit Sub
Errorhandler:
MsgBox "Error description" & Err.Description
End Sub
No comments:
Post a Comment