我一直在写,打开Excel的一个HTML文件(以对其执行各种计算)VBA宏。 Excel将搜索当前文件夹内的HTML文档。如果它不能找到它会产生一个文件打开框,用户可以手动浏览到HTML文件的位置。所有好为止。但是如果用户选择取消(而不是选择一个文件),我希望Excel显示一条消息,并退出。
该消息被产生,但然后代码停止与以下错误:
Run-time error '424': Object required.
这听起来并不像太多的麻烦,却接连试图钉是什么原因造成的问题,我已经运行到一个砖墙。
这似乎没有工作的子是:
Sub ExitWithoutPrompt()
MsgBox "You failed to select a file, therefore Excel will now close. Please refer to the readme file."
Excel.Application.DisplayAlerts = False
Excel.Application.Quit
End Sub
我使用的是MS Excel 2002中,但我渴望为解决Excel的许多变体上工作成为可能。
任何帮助感激地接收到哪里我错了。我顺便说一个完整的新手,所以如果可能的话,请长篇大论跟你可能对我有什么指导意见?
因为它可能包括在下文中使用(在做这个职位笨重的风险)是另外两个潜艇。我正在使用的宏:
首先子:
Sub Endurance()
Call OpenHTML
Range("G27").Value = "Category"
Range("G28").Value = "Meat"
Range("G29").Value = "Veg"
Range("G30").Value = "PRP"
Range("F27").Value = "Fleet"
Range("E27").Value = "Consumption"
Range("E32").Value = "Endurance"
Range("E33").Value = "Lowest Category"
Range("E34").Value = "Fleet"
Range("E35").Value = "Consumption"
Range("E27, F27, G27, E32").Font.Bold = True
Range("F28").Value = WorksheetFunction.Sum(Range("E8,E9,E11,E14,E21"))
Range("E28").Value = WorksheetFunction.Sum(Range("G8,G9,G11,G14,G21"))
Range("F29").Value = WorksheetFunction.Sum(Range("E10,E16"))
Range("E29").Value = WorksheetFunction.Sum(Range("G10,G16"))
Range("F30").Value = WorksheetFunction.Sum(Range("E20,E22"))
Range("E30").Value = WorksheetFunction.Sum(Range("G20,G22"))
Columns("E:F").EntireColumn.AutoFit
Range("G28:G30, E27, F27, G27, G33").Select
With Selection
.HorizontalAlignment = xlRight
End With
Range("E27:G30, E32:G35").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Dim Endurance As Double
Endurance = WorksheetFunction.Min(Range("F28:F30"))
Range("G34").Value = WorksheetFunction.RoundDown(Endurance, 0)
Endurance = WorksheetFunction.Min(Range("E28:E30"))
Range("G35").Value = WorksheetFunction.RoundDown(Endurance, 0)
Range("G33").Value = Endurance
Dim LowCat As String
LowCat = WorksheetFunction.VLookup(Endurance, Range("E28:G30"), 3, False)
Range("G33").Value = LowCat
ActiveSheet.PageSetup.PrintArea = "$A$1:$G$35"
ActiveSheet.PageSetup.Orientation = xlLandscape
Range("G36").Select
If MsgBox("Print endurance statement?", vbYesNo + vbDefaultButton2, "Print endurance") = vbYes Then
ActiveWindow.SelectedSheets.PrintOut Copies:=1
Else
Range("G36").Select
End If
End Sub
而第二子:
Sub OpenHTML()
On Error GoTo MissingFile
Workbooks.Open FileName:=ThisWorkbook.Path & "\TRICAT Endurance Summary.html"
Exit Sub
MissingFile:
Dim Finfo As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
' Set up list of file filters
Finfo = "HTML Files (*.html),*.html," & _
"All Files (*.*),*.*,"
' Display *.html by default
FilterIndex = 1
' Set the dialog box caption
Title = "Select TRICAT Endurance Summary"
' Get the filename
FileName = Application.GetOpenFilename(FInfor, FilterIndex, Title)
' Handle Return info from dialog box
If FileName = False Then
Call ExitWithoutPrompt
Else
MsgBox "You selected" & FileName
Workbooks.Open FileName
End If
End Sub
如果你已经远远得到这个,感谢您的阅读....
添加呼叫ActiveWorkbook.Close
到ExitWithoutPrompt
:
Sub ExitWithoutPrompt()
MsgBox "You failed to select a file, therefore Excel will now close. Please refer to the readme file."
Excel.Application.DisplayAlerts = False
Excel.Application.Quit
ActiveWorkbook.Close False
End Sub
这个Excel 2003在为我工作。
出于某种原因,呼吁Application.Quit
和ActiveWorkbook.Close
的顺序很重要。与直觉相反,至少对我来说,如果你ActiveWorkbook.Close
你仍然得到错误之前调用Application.Quit
。