Hi I have this macro and a few others that are similar that struggle when the worksheet names include "-", " ", "(" or ")". It normally fails on line when the graph series is created ActiveChart.FullSeriesCollection(1).XValues = "=" & ws.Name & "!$G$61:$G$" & iBas In the debugger when I hover over ws.Name it says Automation Error.
I have got around this by changing the worksheet names to remove these characters but I would like to understand why it fails and what if anything I can do to leave the worksheet names as they are?
Sub COREStepChart()
'
' Insert additional rows to have x value for base of core run in order to create bar/step shaped chart by depth/elevation and update graph series to new array
' Macro created by Dan Brenton 20200814
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim wb As Workbook
Dim ws As Worksheet
Dim strRef, strRange As String
Dim iRow, iBase, iTCR, iSCR, iRQD, iDepthBase, iOrder As Integer
Set wb = ThisWorkbook
For Each ws In wb.Worksheets
Select Case ws.Name
Case "Template", "Report", "Configuration (CORE)", "Configuration (Moisture)"
Case Else
ws.Name = Replace(ws.Name, "-", "")
ws.Name = Replace(ws.Name, " ", "")
ws.Name = Replace(ws.Name, "(", "")
ws.Name = Replace(ws.Name, ")", "")
ws.Activate
iTop = 61
iBase = 62
iLoca = 2
iDepthTop = 5
iDepthBase = 6
iTCR = 7
iSCR = 8
iRQD = 9
iOrder = 12
iElev = 17
Do While ws.Cells(iTop, iTCR) <> ""
Rows(iBase & ":" & iBase).Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range(Cells(iTop, iLoca), Cells(iTop, iElev)).Select
Selection.Copy
Cells(iBase, iLoca).Select
ActiveSheet.Paste
Cells(iTop, iDepthBase).Select
Selection.Copy
Cells(iBase, iDepthTop).Select
ActiveSheet.Paste
Cells(iTop, iOrder).Select
ActiveCell.FormulaR1C1 = "1"
Cells(iBase, iOrder).Select
ActiveCell.FormulaR1C1 = "2"
iTop = iTop + 2
iBase = iBase + 2
Loop
ActiveSheet.ChartObjects("Chart2").Activate
ActiveChart.PlotArea.Select
ActiveChart.FullSeriesCollection(1).XValues = "=" & ws.Name & "!$G$61:$G$" & iBase
ActiveChart.FullSeriesCollection(1).Values = "=" & ws.Name & "!$Q$61:$Q$" & iBase
ActiveChart.FullSeriesCollection(2).XValues = "=" & ws.Name & "!$H$61:$H$" & iBase
ActiveChart.FullSeriesCollection(2).Values = "=" & ws.Name & "!$Q$61:$Q$" & iBase
ActiveChart.FullSeriesCollection(3).XValues = "=" & ws.Name & "!$I$61:$I$" & iBase
ActiveChart.FullSeriesCollection(3).Values = "=" & ws.Name & "!$Q$61:$Q$" & iBase
ActiveSheet.ChartObjects("Chart5").Activate
ActiveChart.PlotArea.Select
ActiveChart.FullSeriesCollection(1).XValues = "=" & ws.Name & "!$G$61:$G$" & iBase
ActiveChart.FullSeriesCollection(1).Values = "=" & ws.Name & "!$E$61:$E$" & iBase
ActiveChart.FullSeriesCollection(2).XValues = "=" & ws.Name & "!$H$61:$H$" & iBase
ActiveChart.FullSeriesCollection(2).Values = "=" & ws.Name & "!$E$61:$E$" & iBase
ActiveChart.FullSeriesCollection(3).XValues = "=" & ws.Name & "!$I$61:$I$" & iBase
ActiveChart.FullSeriesCollection(3).Values = "=" & ws.Name & "!$E$61:$E$" & iBase
End Select
Next
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub