Sub AddChartObject()
Dim chrt_LONG, chrt_SHORT, chrt As ChartObject
Dim ws As Worksheet
Dim rng_X, rng_DEPTH As Range
Dim srs_NEW As Series
Dim dt_XSTART, dt_MAX, dt_CURRENTMAX, dt_MJRSCALE, dt_MNRSCALE As Date
Dim str_GSELEVBOX, str_KCWABOX, str_PAGENOBOX, str_MAXSHEET, str_CURRENTSHEET As String
Dim num_MAXDEPTH, num_LASTROW, num_YMAX, num_YMIN, num_INTERVAL, num_WELLS, num_SERIESNO, num_SRSTOTAL As Double
Dim str_LABEL, num_CURRENT, num_MAX As Double
Dim int_WS, int_YEARMULT, int_MAJORUNIT As Integer
'----- DELETES ANY CHARTOBJECT -----
For Each chrt In ActiveSheet.ChartObjects
chrt.Delete
Next
'==========================================================================================================================
'================================ FINDS MAXIMUM DEPTH AND READING DATE OF ALL WORKSHEETS ==================================
'==========================================================================================================================
str_CURRENTSHEET = ActiveSheet.Name
Application.ScreenUpdating = False
num_MAX = 0
dt_MAX = DateSerial(2000, 1, 1)
For int_WS = 1 To Worksheets.Count
Worksheets(int_WS).Activate
num_LASTROW = ActiveSheet.UsedRange.Rows.Count
str_LABEL = 4
Do While Len(Cells(6, str_LABEL)) > 0
If Left(Cells(6, str_LABEL), 5) = "DEPTH" Then
Set rng_DEPTH = Range(Cells(7, str_LABEL), Cells(num_LASTROW, str_LABEL))
num_CURRENT = WorksheetFunction.Max(rng_DEPTH)
If num_CURRENT > num_MAX Then
num_MAX = num_CURRENT
str_MAXSHEET = ActiveSheet.Name
str_WELL = Cells(1, str_LABEL - 1)
End If
ElseIf Right(Cells(6, str_LABEL), 4) = "DATE" Then
Set rng_DEPTH = Range(Cells(7, str_LABEL), Cells(num_LASTROW, str_LABEL))
dt_CURRENTMAX = WorksheetFunction.Max(rng_DEPTH)
If dt_CURRENTMAX > dt_MAX Then
dt_MAX = dt_CURRENTMAX
str_MAXSHEET = ActiveSheet.Name
End If
End If
str_LABEL = str_LABEL + 1
Loop
Next int_WS
num_MAX = WorksheetFunction.RoundUp(num_MAX, 0)
'MsgBox ("num_MAX = " & num_MAX & Chr(10) & "dt_MAX = " & dt_MAX)
Application.ScreenUpdating = True
Worksheets(str_CURRENTSHEET).Activate
'==========================================================================================================================
'=============================================== MAKES THE SHORT HYDROGRAPH ===============================================
'==========================================================================================================================
If Cells(12, 2) = "Yes" Then
'----- CHART OPTIONS -----
Set chrt_SHORT = ActiveSheet.ChartObjects.Add(250, 100, 800, 616) '(left, top, width, height)
chrt_SHORT.Name = "Short"
With chrt_SHORT.Chart
.ChartArea.AutoScaleFont = False
.ChartType = xlXYScatter 'Chart Type
.HasTitle = True 'Sets Chart to have a Title
.ChartTitle.Characters.Text = Cells(2, 2) 'Creates Chart Title
.ChartTitle.Font.Name = "Calibri" 'Chart Title Font
.ChartTitle.Font.Bold = True 'Makes Chart Title Bold
.ChartTitle.Font.Size = 18 'Chart Title Font Size
.ChartArea.Border.LineStyle = xlLineStyleNone
'----- X-AXIS DETAILS -----
With .Axes(xlCategory, xlPrimary)
.HasMajorGridlines = True
.HasMinorGridlines = False
' dt_MAX = InputBox("dt_MAX", , "6/15/2010")
int_YEARMULT = Year(dt_MAX) Mod 4
Select Case int_YEARMULT
Case 0
Select Case (Month(dt_MAX) + 1)
Case 3, 5
dt_MNRSCALE = DateSerial(Year(dt_MAX) - 2, Month(dt_MAX) + 1, 1)
dt_MJRSCALE = dt_MNRSCALE + 24 * 31
int_MAJORUNIT = 31
Case Else
dt_MJRSCALE = DateSerial(Year(dt_MAX), Month(dt_MAX) + 1, 1)
dt_MNRSCALE = dt_MJRSCALE - 24 * 30
int_MAJORUNIT = 30
End Select
Case Else
Select Case (Month(dt_MAX) + 1)
Case 3, 4, 5
dt_MNRSCALE = DateSerial(Year(dt_MAX) - 2, Month(dt_MAX) + 1, 1)
dt_MJRSCALE = dt_MNRSCALE + 24 * 31
int_MAJORUNIT = 31
Case Else
dt_MJRSCALE = DateSerial(Year(dt_MAX), Month(dt_MAX) + 1, 1)
dt_MNRSCALE = dt_MJRSCALE - 24 * 30
int_MAJORUNIT = 30
End Select
End Select
.MajorUnit = int_MAJORUNIT
.MinimumScale = dt_MNRSCALE
.MaximumScale = dt_MJRSCALE
.TickLabels.NumberFormat = "MMM-dd-YY" 'Sets X-Axis Format
.TickLabels.Orientation = 45 'Rotates X-Axis Lables to set degrees
.HasTitle = True 'Sets X-Axis Title On/Off
.AxisTitle.Characters.Text = "Date" 'Creates Chart Title
.AxisTitle.Font.Name = "Calibri" 'Chart Title Font
.AxisTitle.Font.Bold = True 'Makes Chart Title Bold
.AxisTitle.Font.Size = 14 'Chart Title Font Size
.Border.Color = vbBlack
.Border.Weight = 1
With .MajorGridlines
.Border.LineStyle = xlContinuous
.Border.Weight = xlHairline
.Border.Color = vbBlack
End With
End With
'----- Y-AXIS PRIMARY DETAILS -----
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Characters.Text = "Water Level Elevation (ft. amsl)"
.AxisTitle.Font.Name = "Calibri"
.AxisTitle.Font.Size = 14
.AxisTitle.Font.Bold = True
With .MajorGridlines
.Border.LineStyle = xlContinuous
.Border.Weight = xlHairline
.Border.Color = vbBlack
End With
End With
End With
'----- CREATES ELEVATION DATA SERIES FOR CHART -----
num_WELLS = 5
num_SERIESNO = 0
num_LASTROW = ActiveSheet.UsedRange.Rows.Count
Do While Cells(6, num_WELLS) <> ""
If Left(Cells(6, num_WELLS), 4) = "ELEV" Then
Set srs_NEW = chrt_SHORT.Chart.SeriesCollection.NewSeries
With srs_NEW
.ClearFormats
.Name = Cells(3, num_WELLS)
.Values = ActiveSheet.Range(Cells(7, num_WELLS), Cells(num_LASTROW, num_WELLS))
.XValues = Range(Cells(7, 4), Cells(num_LASTROW, 4))
.AxisGroup = xlPrimary
End With
num_WELLS = num_WELLS + 2
num_SERIESNO = num_SERIESNO + 1
ElseIf Left(Cells(6, num_WELLS), 4) = "READ" Then
Set srs_NEW = chrt_SHORT.Chart.SeriesCollection.NewSeries
With srs_NEW
.ClearFormats
.Name = Cells(3, num_WELLS)
.Values = ActiveSheet.Range(Cells(7, num_WELLS + 1), Cells(num_LASTROW, num_WELLS + 1))
.XValues = Range(Cells(7, num_WELLS), Cells(num_LASTROW, num_WELLS))
.AxisGroup = xlPrimary
End With
num_WELLS = num_WELLS + 3
num_SERIESNO = num_SERIESNO + 1
Else
num_WELLS = num_WELLS + 1
End If
Loop
'----- Y-AXIS PRIMARY DETAILS -----
num_YMAX = Round(num_MAX, 0)
num_INTERVAL = WorksheetFunction.RoundUp(num_MAX / 20, 0) * 20
num_YMIN = num_YMAX - num_INTERVAL
With chrt_SHORT.Chart.Axes(xlValue)
.MaximumScale = num_YMAX
.MinimumScale = num_YMIN
.CrossesAt = num_YMIN
.MajorUnit = 20
.TickLabels.NumberFormat = "0"
.Border.Color = vbBlack
.Border.Weight = 1
End With
'----- CHANGES THE SERIES MARKER SIZE AND LINE WIDTH -----
For num_SRSTOTAL = 1 To num_SERIESNO
With chrt_SHORT.Chart
.SeriesCollection(num_SRSTOTAL).Format.Line.Weight = Cells(27, 2)
.SeriesCollection(num_SRSTOTAL).MarkerSize = Cells(26, 2)
.SeriesCollection(num_SRSTOTAL).Shadow = False
End With
Next num_SRSTOTAL
'=============================================== ADDING SECONDARY AXIS ===============================================
'----- CREATES SECONDARY Y-AXIS DATA SERIES FOR CHART (DEPTH OF WATER DATA -----
Set srs_NEW = chrt_SHORT.Chart.SeriesCollection.NewSeries
With srs_NEW
.Name = "srs_SECONDARY"
.XValues = Range(Cells(7, 4), Cells(num_LASTROW, 4))
.Values = ActiveSheet.Range(Cells(7, 6), Cells(num_LASTROW, 6))
.AxisGroup = xlSecondary
.MarkerStyle = None
.Border.LineStyle = None
End With
'----- Secondary Y-Axis Scale Details -----
With chrt_SHORT.Chart.Axes(xlValue, xlSecondary)
.MaximumScale = num_INTERVAL
.MinimumScale = 0
.ReversePlotOrder = True
.MajorUnit = 20
.TickLabels.NumberFormat = "0"
.HasTitle = True
.Border.Color = vbBlack
.Border.Weight = 1
With .AxisTitle
.Characters.Text = "Depth to Water Level (ft.)"
.Font.Name = "Calibri"
.Font.Size = 14
.Font.Bold = True
End With
End With
'=============================================== CHART OPTIONS ===============================================
'----- LEGEND BOX PROPERTIES -----
With chrt_SHORT.Chart.Legend
.LegendEntries(num_SERIESNO + 1).Delete
.IncludeInLayout = False
.Position = xlLegendPositionTop
.Top = 555
End With
'----- CHART PLOT AREA PROPERTIES -----
With chrt_SHORT.Chart
.PlotArea.Interior.Pattern = xlNone
.PlotArea.Height = 475
.PlotArea.Top = 55
End With
'=============================================== TEXTBOXES & IMAGES ===============================================
'----- ADD FOR GS ELEVATION TEXTBOX -----
With chrt_SHORT.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 60, 45, 125, 25)
str_GSELEVBOX = "GS Elevation " & Format(Cells(3, 2), "#0.0") & " ft."
With .TextFrame.Characters
.Text = str_GSELEVBOX
.Font.Name = "Calibri"
.Font.FontStyle = "Regular"
.Font.Size = 10
.Font.ColorIndex = xlAutomatic
End With
End With
'----- ADD PAGE NUMBER TEXTBOX -----
With chrt_SHORT.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 350, 850, 100, 15)
str_PAGENOBOX = "Page " & Cells(16, 2) & " of " & Cells(18, 2)
With .TextFrame
.Characters.Text = str_PAGENOBOX
.Characters.Font.Name = "Calibri"
.Characters.Font.FontStyle = "Regular"
.Characters.Font.Size = 9
.Characters.Font.Bold = False
.Characters.Font.ColorIndex = xlAutomatic
.VerticalAlignment = xlVAlignCenter
.HorizontalAlignment = xlHAlignCenter
End With
End With
End If
End Sub