Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 创建图表时,如果没有结束if错误,则阻止_Excel_Syntax Error_Vba - Fatal编程技术网

Excel 创建图表时,如果没有结束if错误,则阻止

Excel 创建图表时,如果没有结束if错误,则阻止,excel,syntax-error,vba,Excel,Syntax Error,Vba,我正在尝试在excel中自动创建4个图表。我知道其他人也有同样的“阻塞如果没有结束如果”的问题,但我似乎无法让这个工作 这是整个Sub的更新代码。在这个Sub之前,还有3个Sub可以工作,但它们似乎没有问题 Sub BuildCharts(rngIn As Range, rngOut As Range, strChartType As String) ' Variable Declarations Dim rngAIRThisYear As Range 'Data Source of AIRt

我正在尝试在excel中自动创建4个图表。我知道其他人也有同样的“阻塞如果没有结束如果”的问题,但我似乎无法让这个工作

这是整个Sub的更新代码。在这个Sub之前,还有3个Sub可以工作,但它们似乎没有问题

Sub BuildCharts(rngIn As Range, rngOut As Range, strChartType As String)

' Variable Declarations
Dim rngAIRThisYear As Range 'Data Source of AIRthisyear
Dim rngAIRLastYear As Range 'Data Source of AIRLastYear
Dim rngSEICompare As Range 'Data Source of SEICompare
Dim rngSEIThisYear As Range 'Data Source of SEIChart

Dim strAIRThisYearName As String 'Name of the first chart object
Dim strAIRLastYearName As String 'Name of the second chart object
Dim strSEICompareName As String 'Name of the first chart object
Dim strSEIThisYearName As String 'Name of the second chart object

Dim strAIRThisYearAddr As String 'Template of AIRThisYear
Dim strAIRLastYearAddr As String 'Template of AIRLastYear
Dim strSEICompareAddr As String 'Template of SEIcompare
Dim strSEIThisYearAddr As String 'Template of SEIChart

Dim strTitleAIRThisYear As String 'Title of AIRThisYear
Dim strTitleAIRLastYear As String 'Title of AIRLastYear
Dim strTitleSEICompare As String 'Title of AIRThisYear
Dim strTitleSEIThisYear As String 'Title of AIRLastYear

Dim intLnTitle As Integer 'Length of the chart's Title
Dim intLnPeriod As Integer 'Length of the chart's time period (which is part of the title)

Dim strPercentage As String 'Percentage of Total TODO REMOVE AS UNUSED?

Dim strChartExportFile As String 'Filepath where charts will be saved: needs to end in .png

    ' Variable Initialization

     If (strChartType = "AIRThisYear") Then ' These are AIRThisYear
        Set rngAIRThisYear = Sheets("ChartData").Range("A12:D17")
        strAIRThisYearAddr = ThisWorkbook.Path & "\ - AIRThisYear.crtx"
        strTitleAIRThisYear = rngIn.Offset(0, 1) & " - AIRThisYear"
        strAIRThisYearName = rngIn.Offset(0, 1) & " - AIRThisYear"

    ElseIf (strChartType = "AIRLastYear") Then ' These are AIRLastYear
        Set rngAIRLastYear = Sheets("ChartData").Range("A3:D7")
        strAIRLastYearAddr = ThisWorkbook.Path & "\ - AIRLastYear.crtx"
        strTitleAIRLastYear = rngIn.Offset(0, 1) & " - AIRLastYear"
        strAIRLastYearName = rngIn.Offset(0, 1) & " - AIRLastYear"

    ElseIf (strChartType = "SEICompare") Then ' These are These are SEICompare
        Set rngSEICompare = Sheets("ChartData").Range("A12:D17")
        strSEICompareAddr = ThisWorkbook.Path & "\ - SEICompare.crtx"
        strTitleSEICompare = rngIn.Offset(0, 1) & " - SEICompare"
        strSEICompareName = rngIn.Offset(0, 1) & " - SEICompare"

    ElseIf (strChartType = "SEIThisYear") Then 'These are SEI This Year Charts
        Set rngSEIThisYear = Sheets("ChartData").Range("A12:D17")
        strSEIThisYearAddr = ThisWorkbook.Path & "\ - SEIThisYear.crtx"
        strTitleSEIThisYear = rngIn.Offset(0, 1) & " - SEIThisYear"
        strSEIThisYearName = rngIn.Offset(0, 1) & " - SEIThisYear"

    End If

  rngIn.Offset(0, 1) = rngIn.Offset(0, 2)

   'Create AIRThisYear -----------
Sheets("ChartData").Activate
rngAIRThisYear.Select
Sheets("ChartData").Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate (strAIRThisYearAddr)
ActiveChart.SetSourceData Source:=rngAIRThisYear
ActiveChart.Parent.Name = strAIRThisYearName

'Change Title
ActiveChart.ChartTitle.Text = strTitleAIRThisYear
Call ChangeChartTitle(ActiveChart, intLnTitle, intLnPeriod)

'Export the chart image
strChartExportFile = strAIRThisYearName & ".png"
Call SaveChartAsImage(ActiveChart, strChartExportFile)

'Update the merge sheet with the path of the image we just exported
If (strChartType = "AIRThisYear") Then
    rngOut.Offset(0, 9) = strChartExportFile
Else
    rngOut.Offset(0, 11) = strChartExportFile
End If

'Done with the chart, so delete it...
Sheets("ChartData").ChartObjects(strAIRThisYearName).Delete

    'Create AIRLastYear 
Sheets("ChartData").Activate
rngAIRLastYear.Select
Sheets("ChartData").Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate (strAIRLastYearAddr)
ActiveChart.SetSourceData Source:=rngAIRLastYear
ActiveChart.Parent.Name = strAIRLastYearName

'Change Title
ActiveChart.ChartTitle.Text = strTitleAIRLastYear
Call ChangeChartTitle(ActiveChart, intLnTitle, intLnPeriod)

'Export the chart image
strChartExportFile = strAIRLastYearName & ".png"
Call SaveChartAsImage(ActiveChart, strChartExportFile)

'Update the merge sheet with the path of the image we just exported
If (strChartType = "AIRLastYear") Then
    rngOut.Offset(0, 10) = strChartExportFile
Else
    rngOut.Offset(0, 12) = strChartExportFile
End If

'Done with the chart, so delete it...
Sheets("ChartData").ChartObjects(strAIRLastYearName).Delete

'Create SEICompare 
rngSEICompare.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate (strSEICompareAddr)
ActiveChart.SetSourceData Source:=rngSEICompare
ActiveChart.Parent.Name = strSEICompareName
ActiveChart.PlotBy = xlColumns

'Change Title
ActiveChart.ChartTitle.Text = strTitleSEICompare
Call ChangeChartTitle(ActiveChart, intLnTitle, intLnPeriod)

'Export the chart image
strChartExportFile = strSEICompareName & ".png"
Call SaveChartAsImage(ActiveChart, strChartExportFile)

'Update the merge sheet with the path of the image we just exported
If (strChartType = "SEICompare") Then
    rngOut.Offset(0, 13) = strChartExportFile
Else
    rngOut.Offset(0, 15) = strChartExportFile
End If

'Done with the chart, so delete it...
Sheets("ChartData").ChartObjects(strSEICompareName).Delete

'Create SEIChart ------
rngSEIThisYear.Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ApplyChartTemplate (strSEIThisYearAddr)
ActiveChart.SetSourceData Source:=rngSEIThisYear
ActiveChart.Parent.Name = strSEIThisYearName
ActiveChart.PlotBy = xlColumns

'Change Title
ActiveChart.ChartTitle.Text = strTitleSEIThisYear
Call ChangeChartTitle(ActiveChart, intLnTitle, intLnPeriod)

'Export the chart image
strChartExportFile = strSEIThisYearName & ".png"
Call SaveChartAsImage(ActiveChart, strChartExportFile)

'Update the merge sheet with the path of the image we just exported
If (strChartType = "SEIThisYear") Then
    rngOut.Offset(0, 14) = strChartExportFile
Else
    rngOut.Offset(0, 16) = strChartExportFile
End If

Sheets("ChartData").ChartObjects(strSEIThisYearName).Delete

End Sub

尝试编译时是否发生错误?你确定这是因为这个实际的过程吗?有更多的代码你没有显示到这个子?宏人,谢谢你的回答。据我所知,我还没有尝试编译。我对宏相当陌生,一直在更新以前的用户复杂的宏。在此之前有一个子集,用于将数据从一张工作表移动到图表字段。也许我错了,但是不同的潜艇会在危急关头运行,但不会影响下一个潜艇吗?我对这一点还是新手。再次感谢!尝试编译时是否发生错误?你确定这是因为这个实际的过程吗?有更多的代码你没有显示到这个子?宏人,谢谢你的回答。据我所知,我还没有尝试编译。我对宏相当陌生,一直在更新以前的用户复杂的宏。在此之前有一个子集,用于将数据从一张工作表移动到图表字段。也许我错了,但是不同的潜艇会在危急关头运行,但不会影响下一个潜艇吗?我对这一点还是新手。再次感谢!