所以我有一个生成新工作簿的表单,保存它然后使用下面的这个小剪辑将用户返回到控件表单(注意 - 这并不包括所有数据操作):
'create new workbook
Set NewWorkbook = Workbooks.Add
With NewWorkbook
On Error GoTo Finish
.Title = "Project Milestones" & projectref
.SaveAs Filename:=savelocation & "\" & projectref & " Project_Order.xlsx"
End With
' ===== Fixed the error on thie line =====
Workbooks("COFFIE Project Tracker V2.0.0").Sheets("Project Order Template").Copy Before:=Workbooks(projectref & " Project_Order.xlsx").Sheets("Sheet1")
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E6:E7").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E9:E10").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E15:E16").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E6:E7").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E21:E22").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E27:k35").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E40:E43").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E48:E50").ClearContents
Application.DisplayAlerts = True
MsgBox ("Project Order data compiled.")
Application.ScreenUpdating = True
Unload Me
frm_Control.Show
Exit Sub
End
End If
Finish:
Workbooks(projectref & " Project_Order.xlsx").Saved = True
Workbooks(projectref & " Project_Order.xlsx").Close
Kill (savelocation & "\" & projectref & " Project_Order.xlsx")
Set NewWorkbook = Workbooks.Add
With NewWorkbook
.Title = "Project Milestones" & projectref
.SaveAs Filename:=savelocation & "\" & projectref & " Project_Order.xlsx"
End With
ThisWorkbook.Sheets("Project Order Template").Copy Before:=NewWorkbook.Sheets("Sheet1")
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E6:E7").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E9:E10").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E15:E16").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E6:E7").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E21:E22").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E27:k35").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E40:E43").ClearContents
Workbooks("COFFIE Project Tracker V2.0.0").Worksheets("Project Order template").Range("E48:E50").ClearContents
Application.DisplayAlerts = True
MsgBox ("Project Order data compiled.")
Application.ScreenUpdating = True
但是,用户可能希望从"报告控件"运行更多报告。然后,他们会单击主控制表单上的命令按钮,然后将它们带到报表控件表单。但是,当cmd_reprot_Click()
第二次运行时,它会抛出运行时错误9 - 下标超出范围。我虽然可以绕过"这个问题是通过让错误处理程序尝试并强制打开表单,但它仍然会出现相同的错误。 (请参阅下面的代码以获取按钮)
Private Sub cmd_reprot_Click()
On Error GoTo display
frm_Control.Hide
frm_reportControl.Show 'error throws here and goes to error handler below
display:
frm_reportControl.Show ' still throws the runtime 9 error
End Sub
我的问题是 - 为什么我会在第二次尝试打开表单时遇到运行时错误9,以及有什么(如果有的话)解决方法?
编辑:这是根据BruceWayne在评论中提出的建议更新的按钮事件
Private Sub cmd_reprot_Click()
On Error GoTo display
If frm_Control.Visible = True Then
frm_Control.Hide
frm_reportControl.Show
Else
frm_reportControl.Show
End If
display:
If frm_Control.Visible = True Then
frm_Control.Hide
frm_reportControl.Show
Else
frm_reportControl.Show
End If
End Sub
如果需要,很高兴提供更多信息!
答案 0 :(得分:0)
所以我发现了问题,结果是我在生成报告后正在卸载报告表单。然后,如果用户想要尝试重新打开表单,则.Show
没有任何内容,因为它已被卸载。
要解决此问题,我只需将Unload Me
行替换为frm_reportControl.Hide
。还没有看到那个麻烦的错误9!