ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   What is the advantage of creating this Class. (https://www.excelbanter.com/excel-programming/406854-what-advantage-creating-class.html)

WhytheQ

What is the advantage of creating this Class.
 
I wanted to create a Class, which I could reuse, to make it easy to
update the range used by charts.
So I've created a Class called "clUpdateGraph" and updated my normal
module code to use this class to update some graphs. It works. It
doesn't seem to have added any great advantage to just using 'normal'
code in a general module.

If anyone has the time could they try this Class and Source code and
tell me what I'm missing; I'm sure this would have been a good
situation to use a Class module: either it wasn't a good situation, or
my use of the class is wrong!

Any help greatly appreciated
JasonQ


'Class Module "clUpdateGraph":
Private GS As Worksheet 'graph sheet name
Private DS As Worksheet 'data sheet name
Private GN As String 'graph name
Private DA As String 'data address


'######################
'read/write string property returning sheet that graph is located in
'######################
Public Property Let GraphSheet(sGSheet As String)
Set GS = ActiveWorkbook.Sheets(sGSheet)
End Property
Public Property Get GraphSheet() As String
GraphSheet = GS.Name
End Property
'
'
'
'######################
'read/write string property returning sheet that data is located in
'######################
Public Property Let DataSheet(sDSheet As String)
Set DS = ActiveWorkbook.Sheets(sDSheet)
End Property
Public Property Get DataSheet() As String
DataSheet = DS.Name
End Property
'
'
'
'######################
'read/write string property returning name of the chart
'######################
Public Property Let GraphName(sGName As String)
GN = sGName
End Property
Public Property Get GraphName() As String
GraphName = GN
End Property
'
'
'
'######################
'read/write string property returning new address for the graph
sourcedata
'######################
Public Property Let NewDataAddress(sAddress As String)
DA = sAddress
End Property
Public Property Get NewDataAddress() As String
NewDataAddress = DA
End Property
'
'
'
'######################
'method that resets data area
'######################
Public Function UpdateGraphRange(GraphName As String) As Boolean

UpdateGraphRange = False
On Error GoTo ErrorOccured
With GS
.ChartObjects(GraphName).Chart.SetSourceData GS.Range(DA)
UpdateGraphRange = True
End With
Exit Function
ErrorOccured:
Err.Raise vbObjectError + 700001, "", "Possibly some of the
properties are not set"

End Function



'in a normal Module:
Sub UpdateGraphRanges()

Dim x As Integer
x = ThisWorkbook.Sheets("Charts").Range("Ac3")

Set myclUpdateGraph = New ClUpdateGraph
With myclUpdateGraph

.GraphSheet = "Charts"
.DataSheet = "Charts"

.NewDataAddress = "AD4:AI" & x
.UpdateGraphRange "Chart 1"

.NewDataAddress = "AG4:AI" & x
.UpdateGraphRange "Chart 2"

.NewDataAddress = "AG4:AG" & x & ",AI4:AI" & x
.UpdateGraphRange "Chart 4"

End With
Set myclUpdateGraph = Nothing

End Sub


All times are GMT +1. The time now is 02:07 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com