我用一个日历控件做的EXCEL报表的查询程序。
Public xlbook As Object
Public xlsheet As Object
Public todate As StringPrivate Sub Command1_Click()
todate = Calendar1.Value
Set xlbook = GetObject("E:\lindehua\" & todate & ".XLS")
Set xlsheet = xlbook.Sheets(1)
xlbook.Application.Visible = True
xlbook.Parent.Windows(1).Visible = True
End Sub
现在的问题是当文件夹没有当天的文件时,程序会报错。能不能当找不到文件时就弹出一个提示框说找不到文件请另外再查找呢。关键是程序不能出错。

解决方案 »

  1.   

    加上错误处理,捕捉到Err.Num号码,在错误处理中,针对此Err.Num提示用户未找到相关的文件!
      

  2.   

    Private Sub Command1_Click()
    todate = Calendar1.Value
    if dir("E:\lindehua\" & todate & ".XLS")<>"" then
      Set xlbook = GetObject("E:\lindehua\" & todate & ".XLS")
      Set xlsheet = xlbook.Sheets(1)
      xlbook.Application.Visible = True
      xlbook.Parent.Windows(1).Visible = True
    else
      msgbox "文件不存在!"
    end if
    End Sub