excel - 如何防止打开相同的工作表
问题描述
我有多个通过 Dropbox 共享的工作表。我需要检测文件是否已在另一台设备上打开。
我已经尝试了下面的代码,但问题是每次我打开工作簿时,它都会说文件已打开。
Function IsFileOpen(fileName As String)
Dim fileNum As Integer
Dim errNum As Integer
'Allow all errors to happen
On Error Resume Next
fileNum = FreeFile()
'Try to open and close the file for input.
'Errors mean the file is already open
Open fileName For Input Lock Read As #fileNum
Close fileNum
'Get the error number
errNum = Err
'Do not allow errors to happen
On Error GoTo 0
'Check the Error Number
Select Case errNum
'errNum = 0 means no errors, therefore file closed
Case 0
IsFileOpen = False
'errNum = 70 means the file is already open
Case 70
IsFileOpen = True
'Something else went wrong
Case Else
IsFileOpen = errNum
End Select
End Function
Private Sub Workbook_Open()
Dim fileName As String
fileName = ActiveWorkbook.FullName
'Call function to check if the file is open
If IsFileOpen(fileName) = False Then
MsgBox "YOU ARE GOOD TO GO"
Else
'The file is open or another error occurred
MsgBox " STOP THIS FILE IS ALREADY OPEN."
End If
End Sub
解决方案
推荐阅读
- ios - 在 AppleWallet 中检查卡
- docker - Docker exec 命令中的管道返回错误
- pdfbox - 在 convertToImage 方法之后 PDFBox 不清理 tmp 文件
- angularjs - Angularjs选择带有对象数组的ng-options
- amazon-web-services - 当查询字符串包含没有值的键时,AWS API Gateway 失败并显示“无法调用”
- vba - VBA - 宏完成运行后 IE 崩溃
- ios - 如何自动关闭 Safari 视图控制器或 Chrome 自定义选项卡并返回移动应用程序?
- java - 按从用户以字符串格式插入的日期检索子值
- python - 无法使用 Matplotlib Python 缩放数据
- php - PHP中日期的动态颜色