KySoto

Silly Excel thingy

Jul 3rd, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public DataLen As String 'number of rows in the dataset including the headers, gets autoset in the resetbuttons sub
  2. Public Const DataSheet As String = "Data"
  3. Public Const ChartSheet As String = "Crunch"
  4. Public Const TableName As String = "Table_DB2_DB2_VMI_PerformanceCounter_TraceView_DB2" 'Table name in the datasheet
  5.  
  6. Public Sub ResetButtons()
  7. On Error GoTo Error_Handler
  8. Dim t As ListObject
  9. Dim i As Long
  10. Dim b As Button
  11. Set t = Sheets(DataSheet).ListObjects.Item(TableName)
  12. DataLen = t.DataBodyRange.Rows.Count + 1
  13. Debug.Print DataLen
  14.     For Each b In Sheets(ChartSheet).Buttons
  15.         If b.TopLeftCell.Cells.Row = 1 And b.TopLeftCell.Cells.Column = 18 Then
  16.         'b.Name = "btnReset"
  17.        b.Caption = "Reset Buttons to Data Columns"
  18.         b.OnAction = "ResetButtons"
  19.         'Debug.Print b.Name
  20.        Else
  21.        
  22.         b.Caption = "" 't.ListColumns.Item(i).Name
  23.        b.OnAction = "" '"Sheet1.s" & Format(i, "00") & "_Click"
  24.        End If
  25.     Next b
  26. For i = 2 To t.ListColumns.Count
  27. 'Debug.Print t.ListColumns.Item(i).Name
  28.    For Each b In Sheets(ChartSheet).Buttons
  29.         If b.TopLeftCell.Cells.Row = i Then
  30.         'Debug.Print b.Name
  31.        b.Caption = t.ListColumns.Item(i).Name
  32.         b.OnAction = "s" & Format(i, "00") & "_Click"
  33.         End If
  34.     Next b
  35. Next i
  36. Error_Exit:
  37. Exit Sub
  38. Error_Handler:
  39.     MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
  40.            "Error Number: " & Err.Number & vbCrLf & _
  41.            "Error Source: ResetButtons" & vbCrLf & _
  42.            "Error Description: " & Err.Description _
  43.            , vbOKOnly + vbCritical, "An Error has Occured!"
  44. Resume Error_Exit
  45. End Sub
  46.  
  47. Public Function ChangeChart(argCol As Long)
  48. Dim chart As ChartObject
  49. For Each chart In Sheets(ChartSheet).ChartObjects
  50.  
  51.     Dim ser As Series
  52.     chart.chart.ChartTitle.Text = Sheets(DataSheet).Cells(1, argCol).Value
  53.     For Each ser In chart.chart.SeriesCollection
  54.         'Debug.Print ser.Formula
  55.        ser.Formula = "=SERIES(," & DataSheet & "!$A$2:$A$" & DataLen & "," & DataSheet & "!$" & ColLtr(argCol) & "$2:$" & ColLtr(argCol) & "$" & DataLen & ",1)"
  56.     Next ser
  57.  
  58. Next chart
  59. End Function
  60.  
  61. Public Function ColLtr(ByVal iCol As Long) As String
  62.     ' shg 2012
  63.    If iCol Then ColLtr = ColLtr((iCol - 1) \ 26) & Chr(65 + (iCol - 1) Mod 26)
  64. End Function
  65.  
  66. Public Sub s02_click()
  67. ChangeChart 2
  68. End Sub
  69. Public Sub s03_click()
  70. ChangeChart 3
  71. End Sub
  72. 'This goes on for about 99 buttons.
Advertisement
Add Comment
Please, Sign In to add comment