工作表名称崩溃的宏

问题描述

嗨,我有这个宏和其他一些相似的宏,当工作表名称包含“-”,“”,“(”或“)”时,它们很费劲。创建图系列时,它通常无法在线上运行ActiveChart.FullSeriesCollection(1).XValues =“ =”&ws.Name&“!$ G $ 61:$ G $”&iBas在调试器中,当我将鼠标悬停在ws.Name上时它显示自动错误”。

我已经通过更改工作表名称删除这些字符来解决此问题,但我想了解为什么它会失败以及如果可以做些什么来使工作表名称保持原样怎么办?

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.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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)