Option Explicit
Sub DBA_HIST_SYSMETRIC_SUMMARY()
Application.ScreenUpdating = False
Call Workbook_Activate
‘ get worksheets
Dim shData As Worksheet, shOutput As Worksheet
Set shData = ThisWorkbook.Worksheets(“Dataset”)
Dim Cht As Chart
‘ Delete charts
On Error Resume Next
Application.DisplayAlerts = False
ActiveWorkbook.Charts.Delete
ActiveSheet.Cells.NumberFormat = “General”
Application.DisplayAlerts = True
On Error GoTo 0
Application.DisplayAlerts = True
‘ Clear output worksheet
shData.Range(“A1”).CurrentRegion.ClearContents
shData.Cells.Delete
shData.Cells.ClearFormats
Dim Row, Start_Row, Cht_Count, LastColumn As Integer
Dim Column, X, Y, Z, i As Integer
Dim Series, Series1, Series2, Series3, Series4, Series5, Series6, Series7, Series8 As String
Dim DateColumn, Machine_Name, instance_name, metric_name, start_date, tabname, legend_name As String
‘ Clear immediate screen
Application.SendKeys “^g ^a {DEL}”
‘ get start time
Dim dtime As Double
dtime = Time
‘ Select data
Call SelectDataFromDatabase
‘ Get range of data on whole worksheet
Dim rg As Range
Set rg = shData.Range(“A1”).CurrentRegion
Application.GoTo Reference:=shData.Range(“A1”), Scroll:=False
‘ Find last column
LastColumn = rg.Columns.Count
Debug.Print LastColumn
‘ Find date column
Y = 0
Do While Y < LastColumn
Y = Y + 1
Debug.Print Y
Debug.Print VarType(Worksheets(“Dataset”).Cells(1, Y).Value)
If VarType(Worksheets(“Dataset”).Cells(1, Y).Value) = 7 Then
DateColumn = Worksheets(“Dataset”).Cells(1, Y).Value
Exit Do
End If
Loop
Debug.Print DateColumn
If DateColumn = Null Then
MsgBox “Error no date column found.”
End If
Columns(Y).Select
Selection.NumberFormat = “mm-dd-yy”
‘ Copy timestamp column values to new column after column E.
Columns(Y).Copy
Columns(Y).Insert
‘ Format the new column to show only time.
Columns(Y + 1).NumberFormat = “h:mm;@”
‘ Main code
Application.DisplayAlerts = False
Application.GoTo Reference:=Sheet1.Range(“A1”), Scroll:=False
Start_Row = 1
Row = 1
Cht_Count = 1
‘ Reset last column after adding time column above.
LastColumn = rg.Columns.Count
Debug.Print LastColumn
‘ There are 4 static columns(machine name, database instance, date, and time). This formula calculates the number of series selected in the select statement for assignment to the charts.
X = (LastColumn – 4) / 2
‘ rg.Columns.AutoFit
Machine_Name = Range(“A1”).Value
instance_name = “Database ” + Range(“B1”).Value
start_date = rg.Cells(Row, Y).Text
Debug.Print rg.Cells(Row, Y).Address
‘_____________________________________________________________________________________________
‘ metric_name = Range(“C1”).Value
‘ legend_name = metric_name
Debug.Print rg.Rows.Count
Debug.Print rg.Columns.Count
Debug.Print rg.Cells(1 & Row).Address
While Row <= rg.Rows.Count
tabname = rg.Cells(Row, Y).Text
While rg.Cells(Row, Y).Text = start_date
‘ Start_Row = ActiveCell.row
‘ Debug.Print rg.Cells(row, 5).Address
Debug.Print "Start_Row = " & Start_Row
Debug.Print "row = " & Row
Debug.Print start_date
Debug.Print rg.Cells(Row, Y).Text
Debug.Print rg.Cells(Row, Y).Address ' works
Row = Row + 1
Wend
‘ Set Cht = Charts.Add
Set Cht = Charts.Add
With Cht
‘ example .SetSourceData Source:=.Range(.Cells(42, “A”), .Cells(193, .Range(“C6”) + 1))
‘ .SetSourceData Source:=Range(Cells(1, 1), Cells(row – 1, LastColumn))
‘ .SetSourceData Sheets(“Dataset”).Range(“E” & start_row : lastcolumn & row) ‘works
‘ Debug.Print rg.Cells(Start_Row, “F”).Address
‘ There are 4 static columns before the data begins.
Debug.Print rg.Cells(Start_Row, LastColumn – (LastColumn – 4) / 2).Address
Debug.Print rg.Cells(Row – 1, LastColumn).Address
‘ .SetSourceData Range(Cells(Start_Row, “F”), Cells(row – 1, LastColumn))
.SetSourceData Source:=Sheets(“Dataset”).Range(Sheets(“Dataset”).Cells(Start_Row, LastColumn – (LastColumn – 4) / 2), Sheets(“Dataset”).Cells(Row – 1, LastColumn))
‘ .SetSourceData Source:=Sheets(“Dataset”).rg(Cells(Start_Row, “F”), Cells(row – 1, LastColumn))
‘ etSourceData Source:=.Range(.Cell’ 42, “A”), .Cells(193, .Range(“C6”) + 1))
‘ .SetSourceData Source:=Range(Cells(Start_Row, “F”), Cells(row – 1, LastColumn))
‘ example Range(Cells(42, “A”), Cells(193, [c6] + 1))
.ChartType = xlColumnClustered
.Location where:=xlLocationAsNewSheet
‘ Debug.Print Cells(Start_Row, “F”).Address
‘ ActiveChart.ChartArea.Select
‘ .Legend.Select
.Legend.Position = xlRight
‘ The series descriptions always start on the 3rd column(i.e. C) of the spreadsheet. X is the number of series. 2 is the 2 columns to skip over (i.e. A and B).
i = 3
While i <= X + 2
Debug.Print rg.Cells(i).Value
.SeriesCollection(i – 2).Name = rg.Cells(i)
i = i + 1
Wend
i = 3
.HasTitle = False ‘ added line – not sure why it works!
.HasTitle = True
.ChartTitle.Text = “Chart Title”
.ChartTitle.Text = instance_name
.Axes(1).TickLabels.Orientation = 55
Debug.Print tabname
.Name = tabname
Cht_Count = Cht_Count + 1
Cht.Deselect
End With
‘ Debug.Print Now
‘ Debug.Print rg.Address
‘ Application.SendKeys “^g ^a {DEL}”
‘ start_date = Range(Sheets(“Dataset”).Cells(Start_Row, “F”).text
‘ Start_Date = Range(“E” & row).Text
‘ Bad method
‘ Sheets(“Sheets1”).Activate ‘ Bad method
‘ Range(“E” & row).Select
‘ start_date = Selection.Value
‘ Good method
start_date = rg.Cells(Row, Y).Text
Debug.Print rg.Cells(Row, Y).Address
Debug.Print ActiveWorkbook.FullName
Debug.Print start_date
Start_Row = Row
‘ Next i ‘ For loop
Wend
Call SortWorksheetsTabs
Debug.Print rg.Address
Debug.Print "Time is: " & (Time - dtime) * 1000
Debug.Print Time
Debug.Print Now
Debug.Print "Finished"
Debug.Print Cht_Count
‘ Set Sheet = Worksheets(Metric_na)
End Sub
‘ Connect and select fast
Sub SelectDataFromDatabase()
Dim shData As Worksheet, shOutput As Worksheet
Set shData = ThisWorkbook.Worksheets(“Dataset”)
‘ Go to the VBE’s Tools, References then locate and put a check beside ‘Microsoft ActiveX Data Objects 6.1 Library’ to include the library in your project.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim strCon As String
Dim recArray As Variant
‘DataSource argument according to my tnsnames.ora
strCon = “Provider=OraOLEDB.Oracle;Data Source=cdbp;User ID=system;Password=password;”
cn.Open (strCon)
‘ select distinct(metric_id), metric_name from DBA_HIST_SYSMETRIC_SUMMARY order by 1;
rs.Open ” select ” & _
” host_name, global_name, ” & _
” c.metric_name, d.metric_name, y.metric_name, z.metric_name, a.metric_name, b.metric_name, ” & _
” end_time, ” & _
” c.average, d.average, y.average, z.average, a.average, b.average ” & _
” from ” & _
” (select host_name from v$instance) host, ” & _
” (select global_name from global_name) instance, ” & _
” (select end_time, snap_id from dba_hist_sysmetric_summary where metric_id = 2000) X, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2000) Y, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2047) Z, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2100) A, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2092) B, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2092) C, ” & _
” (select snap_id, metric_name, average from dba_hist_sysmetric_summary where metric_id = 2092) D ” & _
” where x.snap_id = y.snap_id ” & _
” and x.snap_id = z.snap_id ” & _
” and x.snap_id = a.snap_id ” & _
” and x.snap_id = b.snap_id ” & _
” and x.snap_id = c.snap_id ” & _
” and x.snap_id = d.snap_id ” & _
” order by end_time “, cn
‘ ThisWorkbook.Worksheets(“Dataset”).Range(“A:A”).CopyFromRecordset rs
‘ shData.Range(“A:A”).CopyFromRecordset rs
shData.Range(“A1”).CopyFromRecordset rs
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Sub SortWorksheetsTabs()
Application.ScreenUpdating = False
Dim ShCount As Integer, i As Integer, j As Integer
ShCount = Sheets.Count
For i = 1 To ShCount – 1
For j = i + 1 To ShCount
If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then
Sheets(j).Move before:=Sheets(i)
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_Activate()
On Error Resume Next
With Application
.DisplayFullScreen = True
.CommandBars(“Worksheet Menu Bar”).Enabled = False
End With
End Sub
Private Sub Workbook_Deactivate()
On Error Resume Next
With Application
.DisplayFullScreen = False
.CommandBars(“Worksheet Menu Bar”).Enabled = True
End With
End Sub
Sub SelectForMetrics()
Dim shData2 As Worksheet, shOutput As Worksheet
Set shData2 = ThisWorkbook.Worksheets(“Metrics”)
‘ Go to the VBE’s Tools, References then locate and put a check beside ‘Microsoft ActiveX Data Objects 6.1 Library’ to include the library in your project.
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
Dim strCon As String
Dim recArray As Variant
‘ Clear output worksheet
shData2.Range(“A1”).CurrentRegion.ClearContents
shData2.Cells.Delete
shData2.Cells.ClearFormats
‘DataSource argument according to my tnsnames.ora
strCon = “Provider=OraOLEDB.Oracle;Data Source=MRCP;User ID=ldcdba;Password=Fdfsda8_9sda;”
cn.Open (strCon)
rs.Open ” select distinct(metric_id) metric_id, metric_name from DBA_HIST_SYSMETRIC_SUMMARY order by 1 “, cn
‘ ThisWorkbook.Worksheets(“Sheet1”).Range(“A:A”).CopyFromRecordset rs
‘ shData.Range(“A:A”).CopyFromRecordset rs
shData2.Range(“A1”).CopyFromRecordset rs
Application.GoTo Reference:=shData2.Range(“A1”), Scroll:=False
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub
Sub Datavalidation1()
Dim shData2 As Worksheet
Set shData2 = ThisWorkbook.Worksheets(“Metrics”)
Dim Metric_rg As Range
Set Metric_rg = shData2.Range(“F1:G161”).CurrentRegion
Range(“B3”).Validation.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, Formula1:=”=Fruits”
End Sub
Sub Datavalidation2()
ActiveWorkbook.Names.Add Name:=”Metric”, RefersTo:=ThisWorkbook.Worksheets(“Metrics”).Range(“A1:B161”)
Range(“F3″).Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=”=Fruit”
End Sub