我有一个宏从一张纸上获取数据,然后将图形添加到另一张纸上。当我单步执行它时,宏工作,但当我使用表单按钮调用宏时,我得到
User-defined type not defined.
从我的研究中,大多数这些错误与我的代码都没有的外部程序的引用有关。此外,我的代码过去没有问题,但几个月后回来,我现在也得到了一个
添加此新工作表时发出警告
"unexpected error / Exception from HRESULT: 0x800A03EC"
它也不是典型的格式。它允许点击链接:"复制详细信息" (什么都不做),发送皱眉(警告MS),或关闭。显示alert = false没有解决这个问题,我猜测是因为它更像是一个bug而不是宏的问题?
Option Explicit
Sub Graph()
Application.DisplayAlerts = False
Dim Chart As Chart
Dim LastRow As Long
Dim srs1 As Series
Dim srs3 As Series
Dim srs4 As Series
Dim srs5 As Series
Dim srs6 As Series
Dim srs7 As Series
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'data to be turned into graph
Range("A36:H" & LastRow).Select
With Selection
Set Chart = Charts.Add
End With
With Chart
.HasTitle = True
'determine which chart title to add
If Worksheets("Margin View").Range("R1").Value = 2 Then
.ChartTitle.Text = Range("=Functions!T17").Value
.PlotBy = xlColumns
Else
.ChartTitle.Text = Range("=Functions!S17").Value
.ChartType = xlColumnClustered
End If
'name series in legend
Set srs1 = ActiveChart.SeriesCollection(1)
srs1.Name = "Margin"
Set srs3 = ActiveChart.SeriesCollection(3)
srs3.Name = "Less than 30 days"
Set srs4 = ActiveChart.SeriesCollection(4)
srs4.Name = "30 - 59 days"
Set srs5 = ActiveChart.SeriesCollection(5)
srs5.Name = "60 - 89 days"
Set srs6 = ActiveChart.SeriesCollection(6)
srs6.Name = "90 - 364 days"
Set srs7 = ActiveChart.SeriesCollection(7)
srs7.Name = "365 days +"
'delete 2nd series, name axes, change chart type
.FullSeriesCollection(2).Delete
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Number of Reservations"
.SeriesCollection(1).AxisGroup = xlSecondary
'next line was replaced with above line
'ActiveChart.FullSeriesCollection(1).AxisGroup = 2
.FullSeriesCollection(1).ChartType = xlLineMarkers
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Margin Percentage"
.FullSeriesCollection(1).ApplyDataLabels
End With
ActiveSheet.Buttons.Add(0, 0, 100, 50).Select
With Selection
.OnAction = "DeleteGraph"
.Name = "GraphButton"
.Text = "Return to Margin View"
End With
Application.DisplayAlerts = True
End Sub
修改: 这是我为意外的HRESULT错误获得的代码。
Feedback Type:
Frown (Error)
Error Message:
Exception from HRESULT: 0x800A03EC
Feedback Type:
Frown (Error)
Error Message:
Exception from HRESULT: 0x800A03EC
Stack Trace:
Microsoft.Mashup.Client.Excel.Shim.NativeExcelException: Exception from HRESULT: 0x800A03EC ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC
--- End of inner exception stack trace ---
at Microsoft.Mashup.Client.Excel.NativeExcelFunctionsBase.ValidateResult(Int32 result, Int32[] expectedValues)
at Microsoft.Mashup.Client.Excel.NativeExcelFunctionsC2R.Microsoft.Mashup.Client.Excel.INativeExcelFunctions.TryGetActiveListObject(IntPtr uiFrame, String& listObjectName)
at Microsoft.Mashup.Client.Excel.Shim.NativeWindowContext.TryGetActiveListObject(String& listObjectName)
at Microsoft.Mashup.Client.Excel.ActiveQueryCache.ActiveListObjectDescriptor.GetActive(IWindowContext windowContext, IWorkbookServices workbookServices)
at Microsoft.Mashup.Client.Excel.ActiveQueryCache.GetOrCreateCacheEntry(IWindowContext windowContext)
at Microsoft.Mashup.Client.Excel.CustomTaskPaneManagers.UpdateQueriesTaskPaneVisibility(IWindowContext windowContext)
at Microsoft.Mashup.Client.Excel.ExcelEventHandler.PerformPeriodicUpdate(IEnumerable`1 queriesTaskPaneVisibilityUpdateWindows)
at Microsoft.Mashup.Client.Excel.NativeEventHandler.<OnIdle>b__0()
at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action)
Invocation Stack Trace:
at Microsoft.Mashup.Host.Document.ExceptionExtensions.GetCurrentInvocationStackTrace()
at Microsoft.Mashup.Client.ClientShared.StackTraceInfo..ctor(String exceptionStackTrace, String invocationStackTrace)
at Microsoft.Mashup.Client.ClientShared.FeedbackErrorInfo..ctor(String message, Exception exception)
at Microsoft.Mashup.Client.Excel.Native.NativeUserFeedbackServices.ReportException(IWin32Window activeWindow, WindowsHost windowsHost, FeedbackPackageInfo feedbackPackageInfo, Exception e)
at Microsoft.Mashup.Client.ClientShared.UnexpectedExceptionHandler.<>c__DisplayClass1.<HandleException>b__0()
at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.SendAndMarshalExceptions(SynchronizationContext context, Action callback)
at Microsoft.Mashup.Client.ClientShared.UnexpectedExceptionHandler.HandleException(Exception e)
at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action)
at Microsoft.Mashup.Client.Excel.NativeEventHandler.OnIdle()
at Microsoft.Mashup.Client.Excel.NativeExcelConnectorBase.<OnIdleCallback>b__37()
at Microsoft.Mashup.Client.Excel.ExcelCallbackManager.InvokeAndReturnHResult(Action action)
at Microsoft.Mashup.Client.Excel.NativeExcelConnectorBase.OnIdleCallback()
Supports Premium Content:
False
Formulas:
section Section1;
shared MarginalReportAnd2015 = let
Source = Excel.Workbook(File.Contents("M:\Report Writing\Accounting-RAD\MARGINAL REPORT and 2015.xlsx"), null, true),
Table2_Table = Source{[Item="Table2",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table2_Table,{{"Arrival Date", type any}, {"Reservation", Int64.Type}, {"Margin", type number}, {"Requested By", type text}, {"Billing Name", type any}, {"Length of Stay (days)", type any}})
in
#"Changed Type";
shared SPIDE = let
Source = Excel.Workbook(File.Contents("M:\Report Writing\Accounting-RAD\SPIde.xlsx"), null, true),
SPIDE_Table = Source{[Item="SPIDE",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(SPIDE_Table,{{"Arrival Date", type date}, {"Reservation", Int64.Type}, {"Margin", type number}, {"Requested By", type text}, {"Billing Name", type any}}),
#"Column From Examples" = Table.AddColumn(#"Changed Type", "Literal", each "=INDEX('[TMR All Reservations.xlsx]Sheet1'!$D:$F,MATCH($B2,'[TMR All Reservations.xlsx]Sheet1'!$D:$D,0),3)", type text),
#"Removed Columns" = Table.RemoveColumns(#"Column From Examples",{"Literal"}),
#"Column From Examples1" = Table.AddColumn(#"Removed Columns", "Literal", each "REPLACE", type text),
#"Renamed Columns" = Table.RenameColumns(#"Column From Examples1",{{"Literal", "Length of Stay (days)"}}),
#"Removed Columns1" = Table.RemoveColumns(#"Renamed Columns",{"Length of Stay (days)"})
in
#"Removed Columns1";
[ Description = "Joins the SPIde report that must be ran and the 2015/Margin Report (2016)" ]
shared Append1 = let
Source = Table.Combine({MarginalReportAnd2015, SPIDE})
in
Source;