我在VBA中运行一个子程序,一切正常,除了一件事:有一点我想改变生成图表的PlotArea,我不明白为什么我收到以下错误信息:& #34;运行时错误' -2147467259(80004005)':方法'高度'对象' PlotArea'失败"
代码如下:
Sub DTWO()
[...]
Dim MyEmbeddedChart As Chart
Set MyEmbeddedChart = ActiveSheet.Shapes.AddChart.Chart
With MyEmbeddedChart
[...]
.PlotArea.Height = 200
[...]
End With
End Sub
奇怪的是,在收到错误消息后,如果我按下F8,即“步入”,它确实可以正常工作。那么,为什么会发生这种情况呢?
如果您认为可能需要查看其余代码,可以在这里找到它:
Sub DiagramD2vsFields()
Dim nPatients, nT, i, nOrgan1, nOrgan2, nOrgans As Integer
Dim Organ1, Organ2, OrganN, PatientsConsidered, PatientsNotConsidered As String
nPatients = Worksheets(1).Range("X1")
Worksheets("OAR").Activate
nT = 9
Organ1 = InputBox("Column of the first organ:")
Organ2 = InputBox("If there is, column of the second organ:")
OrganN = InputBox("Name of the organ(s):")
nOrgan1 = Range(Organ1 & 1).Column
If Organ2 <> "" Then
nOrgan2 = Range(Organ2 & 1).Column
End If
Dim TOTAL(), SP(), PP(), SFUD(), IMPT(), Mixed(), TOTALav, SPav, PPav, SFUDav, IMPTav, Mixedav, TOTALE, SPE, PPE, SFUDE, IMPTE, MixedE, sum As Double
Dim nTOTAL, nSP, nPP, nSFUD, nIMPT, nMixed As Integer
'Creating the vectors for Total, SFUD, IMPT and Mixed
For i = 0 To nPatients - 1
If Not IsEmpty(Cells(i * nT + 3, nOrgan1)) Then
If Cells(i * nT + 4, 3) = "SFUD" Then
nTOTAL = nTOTAL + 1
nSFUD = nSFUD + 1
ReDim Preserve TOTAL(nTOTAL - 1)
ReDim Preserve SFUD(nSFUD - 1)
If Organ2 <> "" Then
TOTAL(nTOTAL - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
SFUD(nSFUD - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Else
TOTAL(nTOTAL - 1) = Cells(i * nT + 3, nOrgan1)
SFUD(nSFUD - 1) = Cells(i * nT + 3, nOrgan1)
End If
Else
If Cells(i * nT + 4, 3) = "IMPT" Then
nTOTAL = nTOTAL + 1
nIMPT = nIMPT + 1
ReDim Preserve TOTAL(nTOTAL - 1)
ReDim Preserve IMPT(nIMPT - 1)
If Organ2 <> "" Then
TOTAL(nTOTAL - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
IMPT(nIMPT - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Else
TOTAL(nTOTAL - 1) = Cells(i * nT + 3, nOrgan1)
IMPT(nIMPT - 1) = Cells(i * nT + 3, nOrgan1)
End If
Else
nTOTAL = nTOTAL + 1
nMixed = nMixed + 1
ReDim Preserve TOTAL(nTOTAL - 1)
ReDim Preserve Mixed(nMixed - 1)
If Organ2 <> "" Then
TOTAL(nTOTAL - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Mixed(nMixed - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Else
TOTAL(nTOTAL - 1) = Cells(i * nT + 3, nOrgan1)
Mixed(nMixed - 1) = Cells(i * nT + 3, nOrgan1)
End If
End If
End If
End If
Next i
'Creating the vectors for standard patients and pre-irradiated patients
For i = 0 To nPatients - 1
If Not IsEmpty(Cells(i * nT + 3, nOrgan1)) Then
If IsEmpty(Cells(i * nT + 3, 1)) Then
PatientsConsidered = PatientsConsidered & Cells(i * nT + 2, 1) & "; "
nSP = nSP + 1
ReDim Preserve SP(nSP - 1)
If Organ2 <> "" Then
SP(nSP - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Else
SP(nSP - 1) = Cells(i * nT + 3, nOrgan1)
End If
Else
PatientsConsidered = PatientsConsidered & Cells(i * nT + 2, 1) & "*; "
nPP = nPP + 1
ReDim Preserve PP(nPP - 1)
If Organ2 <> "" Then
PP(nPP - 1) = (Cells(i * nT + 3, nOrgan1) + Cells(i * nT + 3, nOrgan2)) / 2
Else
PP(nPP - 1) = Cells(i * nT + 3, nOrgan1)
End If
End If
Else
If IsEmpty(Cells(i * nT + 3, 1)) Then
PatientsNotConsidered = PatientsNotConsidered & Cells(i * nT + 2, 1) & "; "
Else
PatientsNotConsidered = PatientsNotConsidered & Cells(i * nT + 2, 1) & "*; "
End If
End If
Next i
TOTALav = Application.WorksheetFunction.sum(TOTAL) / (UBound(TOTAL) + 1)
SFUDav = Application.WorksheetFunction.sum(SFUD) / (UBound(SFUD) + 1)
IMPTav = Application.WorksheetFunction.sum(IMPT) / (UBound(IMPT) + 1)
SPav = Application.WorksheetFunction.sum(SP) / (UBound(SP) + 1)
PPav = Application.WorksheetFunction.sum(PP) / (UBound(PP) + 1)
'In VB, for whatever reason, Not myArray returns the SafeArray pointer. For uninitialized arrays, this returns -1.
If (Not Mixed) = -1 Then
' Array has NOT been initialized
Mixedav = 0
Else
' Array has been initialized, so you're good to go.
Mixedav = Application.WorksheetFunction.sum(Mixed) / (UBound(Mixed) + 1)
End If
If (Not Mixed) = -1 Then
' Array has NOT been initialized
nOrgans = UBound(SFUD) + 1 + UBound(IMPT) + 1
Else
' Array has been initialized, so you're good to go.
nOrgans = UBound(SFUD) + 1 + UBound(IMPT) + 1 + UBound(Mixed) + 1
End If
Dim nV As Integer
Dim TermOfSD As Double
sum = 0
For nV = 0 To UBound(TOTAL)
TermOfSD = (TOTAL(nV) - TOTALav) ^ 2
sum = sum + TermOfSD
Next nV
TOTALE = (sum / UBound(TOTAL)) ^ 0.5
sum = 0
For nV = 0 To UBound(SFUD)
TermOfSD = (SFUD(nV) - SFUDav) ^ 2
sum = sum + TermOfSD
Next nV
SFUDE = (sum / UBound(SFUD)) ^ 0.5
sum = 0
For nV = 0 To UBound(IMPT)
sum = sum + (IMPT(nV) - IMPTav) ^ 2
Next nV
IMPTE = (sum / UBound(IMPT)) ^ 0.5
sum = 0
For nV = 0 To UBound(SP)
sum = sum + (SP(nV) - SPav) ^ 2
Next nV
SPE = (sum / UBound(SP)) ^ 0.5
sum = 0
For nV = 0 To UBound(PP)
sum = sum + (PP(nV) - PPav) ^ 2
Next nV
PPE = (sum / UBound(PP)) ^ 0.5
sum = 0
If (Not Mixed) = -1 Then
' Array has NOT been initialized
MixedE = 0
Else
' Array has been initialized, so you're good to go.
For nV = 0 To UBound(Mixed)
sum = sum + (Mixed(nV) - Mixedav) ^ 2
Next nV
If UBound(Mixed) <> 0 Then
MixedE = (sum / UBound(Mixed)) ^ 0.5
Else
MixedE = 0
End If
End If
Cells(500, 500).Select
Dim ER(5), ER1(5), ER2(5), ER3(5), ER4(5), ER5(5), ER6(5) As Double
ER(0) = TOTALE / 2
ER(1) = SPE / 2
ER(2) = PPE / 2
ER(3) = SFUDE / 2
ER(4) = IMPTE / 2
ER(5) = MixedE / 2
ER1(0) = ER(0)
ER1(1) = 0
ER1(2) = 0
ER1(3) = 0
ER1(4) = 0
ER1(5) = 0
ER2(0) = 0
ER2(1) = ER(1)
ER2(2) = 0
ER2(3) = 0
ER2(4) = 0
ER2(5) = 0
ER3(0) = 0
ER3(1) = 0
ER3(2) = ER(2)
ER3(3) = 0
ER3(4) = 0
ER3(5) = 0
ER4(0) = 0
ER4(1) = 0
ER4(2) = 0
ER4(3) = ER(3)
ER4(4) = 0
ER4(5) = 0
ER5(0) = 0
ER5(1) = 0
ER5(2) = 0
ER5(3) = 0
ER5(4) = ER(4)
ER5(5) = 0
ER6(0) = 0
ER6(1) = 0
ER6(2) = 0
ER6(3) = 0
ER6(4) = 0
ER6(5) = ER(5)
Dim MyEmbeddedChart As Chart
Set MyEmbeddedChart = ActiveSheet.Shapes.AddChart.Chart
With MyEmbeddedChart
'Data
.ChartType = xlColumnClustered
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "TOTAL (n=" & nTOTAL & ")"
.SeriesCollection(1).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(1).Values = Array(TOTALav, 0, 0, 0, 0, 0)
.SeriesCollection(1).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER1, MinusValues:=ER1
.SeriesCollection.NewSeries
.SeriesCollection(2).Name = "Standard Patients (n=" & nSP & ")"
.SeriesCollection(2).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(2).Values = Array(0, SPav, 0, 0, 0, 0)
.SeriesCollection(2).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER2, MinusValues:=ER2
.SeriesCollection.NewSeries
.SeriesCollection(3).Name = "Pre-Irradiated Patients (n=" & nPP & ")"
.SeriesCollection(3).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(3).Values = Array(0, 0, PPav, 0, 0, 0)
.SeriesCollection(3).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER3, MinusValues:=ER3
.SeriesCollection.NewSeries
.SeriesCollection(4).Name = "SFUD (n=" & nSFUD & ")"
.SeriesCollection(4).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(4).Values = Array(0, 0, 0, SFUDav, 0, 0)
.SeriesCollection(4).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER4, MinusValues:=ER4
.SeriesCollection.NewSeries
.SeriesCollection(5).Name = "IMPT (n=" & nIMPT & ")"
.SeriesCollection(5).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(5).Values = Array(0, 0, 0, 0, IMPTav, 0)
.SeriesCollection(5).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER5, MinusValues:=ER5
.SeriesCollection.NewSeries
.SeriesCollection(6).Name = "Mixed (n=" & nMixed & ")"
.SeriesCollection(6).XValues = Array("TOTAL (n=" & nTOTAL & ")", "Standard Patients (n=" & nSP & ")", "Pre-Irradiated Patients (n=" & nPP & ")", "SFUD (n=" & nSFUD & ")", "IMPT (n=" & nIMPT & ")", "Mixed (n=" & nMixed & ")")
.SeriesCollection(6).Values = Array(0, 0, 0, 0, 0, Mixedav)
.SeriesCollection(6).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, Amount:=ER6, MinusValues:=ER6
'Titles
.HasTitle = True
.ChartTitle.Characters.Text = "D2%(%) of " & OrganN & " (Technique) [n=" & nOrgans & "]"
.ChartTitle.Characters(Start:=2, length:=2).Font.Subscript = True
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Type of fields"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "D2%(%)"
.Axes(xlValue, xlPrimary).AxisTitle.Characters(Start:=2, length:=2).Font.Subscript = True
.Axes(xlCategory).HasMajorGridlines = True
'Formatting
.ChartGroups(1).Overlap = 100
.Axes(xlCategory).HasMinorGridlines = False
.Axes(xlValue).HasMajorGridlines = True
.Axes(xlValue).MajorUnit = 10
.Axes(xlValue).HasMinorGridlines = True
.HasLegend = False
'Position
.Parent.Top = Cells(nPatients * nT + 5, 1).Top
.Parent.Left = Cells(nPatients * nT + 5, 1).Left
.ChartArea.Height = 400
.ChartArea.Width = 600
'ActiveSheet.ChartObjects("Chart 2").Activate
'ActiveChart.Axes(xlCategory).Select
'ActiveChart.Axes(xlValue).MinorGridlines.Select
'ActiveChart.PlotArea.Select
'Selection.Height = 270.46
.PlotArea.Height = 200
'ActiveChart.PlotArea.Height = 270.46
'.Shapes.PlotArea.Height = 270.46
'Adding textbox with the number of Patients included: expression.AddTextbox(Orientation, Left, Top, Width, Height)
.Shapes.AddTextbox(msoTextOrientationHorizontal, 5, 300, 800, 20).TextFrame.Characters.Text = "Included patients: " & PatientsConsidered & "Not Included Patients: " & PatientsNotConsidered
'Copy statistics data on the bottom of the table
Worksheets("OAR").Cells(nPatients * nT + 2, nOrgan1) = TOTALav
Worksheets("OAR").Cells(nPatients * nT + 3, nOrgan1) = TOTALE
Worksheets("OAR").Cells(nPatients * nT + 4, nOrgan1) = SFUDav
Worksheets("OAR").Cells(nPatients * nT + 5, nOrgan1) = SFUDE
Worksheets("OAR").Cells(nPatients * nT + 6, nOrgan1) = IMPTav
Worksheets("OAR").Cells(nPatients * nT + 7, nOrgan1) = IMPTE
Worksheets("OAR").Cells(nPatients * nT + 8, nOrgan1) = Mixedav
Worksheets("OAR").Cells(nPatients * nT + 9, nOrgan1) = MixedE
Worksheets("OAR").Cells(nPatients * nT + 10, nOrgan1) = SPav
Worksheets("OAR").Cells(nPatients * nT + 11, nOrgan1) = SPE
Worksheets("OAR").Cells(nPatients * nT + 12, nOrgan1) = PPav
Worksheets("OAR").Cells(nPatients * nT + 13, nOrgan1) = PPE
Worksheets("OAR").Cells(nPatients * nT + 14, nOrgan1) = PatientsConsidered
Worksheets("OAR").Cells(nPatients * nT + 15, nOrgan1) = PatientsNotConsidered
Cells(1, 1).Select
End With
End Sub