请大神帮忙改一段VB代码。避免同一EXCEL文件重复打开问题,希望能加进判断不重复打开柜体2.xlsb文件。
OptionExplicitDimstrDataPathAsStringDimxlAppAsexcel.ApplicationDimxlBookAsexcel.Workb...
Option Explicit
Dim strDataPath As String
Dim xlApp As excel.Application
Dim xlBook As excel.Workbook
Dim xlSheet As excel.Worksheet
Private Sub Form_Load()
strDataPath = App.Path & "\Database\柜体2.xlsb"
If dir(strDataPath, vbDirectory) = "" Then
MsgBox "柜体数据不存在,请检查数据库路径", vbInformation, "数据库错误"
'Exit Function
End If
Set xlApp = CreateObject("Excel.Application")
'xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open(strDataPath)
xlApp.Visible = False
'End Function
'End Function
End Sub
希望能判断“柜体.xlsb”文件是否以经打开,如果以经打开了,就不再重复打开,如果没打开就打开它。请大神帮忙把代码修改一下达到这个要求。谢谢,就10分全上了。
首先感谢 bdxbr 老师的热心解答,但是问题依旧,刚才试着给老师老私信但是百度验证码输入永远是错误的,只好做罢,另外还请大神们伸手指点,不胜感激…… 展开
Dim strDataPath As String
Dim xlApp As excel.Application
Dim xlBook As excel.Workbook
Dim xlSheet As excel.Worksheet
Private Sub Form_Load()
strDataPath = App.Path & "\Database\柜体2.xlsb"
If dir(strDataPath, vbDirectory) = "" Then
MsgBox "柜体数据不存在,请检查数据库路径", vbInformation, "数据库错误"
'Exit Function
End If
Set xlApp = CreateObject("Excel.Application")
'xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open(strDataPath)
xlApp.Visible = False
'End Function
'End Function
End Sub
希望能判断“柜体.xlsb”文件是否以经打开,如果以经打开了,就不再重复打开,如果没打开就打开它。请大神帮忙把代码修改一下达到这个要求。谢谢,就10分全上了。
首先感谢 bdxbr 老师的热心解答,但是问题依旧,刚才试着给老师老私信但是百度验证码输入永远是错误的,只好做罢,另外还请大神们伸手指点,不胜感激…… 展开
1个回答
展开全部
Option Explicit
Dim strDataPath As String
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Private Sub Form_Load()
On Error Resume Next '这一行新增
strDataPath = App.Path & "\Database\柜体2.xlsb"
If Dir(strDataPath, vbDirectory) = "" Then
MsgBox "柜体数据不存在,请检查数据库路径", vbInformation, "数据库错误"
'Exit Function
End If
s = Workbooks("柜体2.xlsb").Name '获取文件名
Err.Number = 0
If Err.Number <> 0 Then '如果发生错误,表示没有打开此文件
Err.Number = 0
Set xlApp = CreateObject("Excel.Application")
'xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open(strDataPath)
xlApp.Visible = False
End If
'End Function
'End Function
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询