VBAfind图表标题和比较string
我正在尝试写一些VBA来做检查,以确保图表具有特定的标题。 这就是我现在所拥有的。
Sub check_chart_title() Worksheets(1).ChartObjects(3).Activate With ActiveWorkbook.ActiveChart .HasTitle = True .ChartTitle.Select End With If Selection = "Sales" Then 'code crashes here x = "good" 'proceed to the rest of the code, no chart title mismatch Else: x = "bad" 'specific action to take if there is a chart title mismatch End If End Sub
我知道这个问题是与if语句的条件有关的。 但我一直无法find一个方法来运行图表标题的条件。 我所需要的是能够识别图表标题是否与特定string相同的代码。
任何帮助是极大的赞赏。
而不是select图表标题,然后testing它,只要把它放在你的IF
语句。
With ActiveWorkbook.ActiveChart .HasTitle = True .ChartTitle.Select If .ChartTitle.Text = "Sales" Then 'code crashes here x = "good" 'proceed to the rest of the code, no chart title mismatch Else x = "bad" 'specific action to take if there is a chart title mismatch End If End With
可能你是这样的东西:
Sub check_chart_title() Worksheets(1).ChartObjects(3).Activate With ActiveChart .HasTitle = True .ChartTitle.Select If .name = "Sales" Then x = "good" 'proceed to the rest of the code, no chart title mismatch Else x = "bad" 'specific action to take if there is a chart title mismatch End If End With End Sub
你可以随时尝试这个,如果你想with
:
Dim myChart As Chartobject set myChart = Sheets(1).ChartObjects(3) With myChart if .ChartTitle.Text = "Sales" then x = "Good" Else x = "Bad" end if 'Do some other stuff end with
“