Vba 需要有关在网络上打开另一个工作簿的帮助吗
你能帮我弄清楚吗 我需要在工作簿(a)上运行一些代码,以便在网络上打开其他两个工作簿(B、C、D和E)。这些其他的工作手册经常被其他人使用。所以我打开这些其他的工作簿没有问题。。。如果这些工作簿当前正被其他人使用,它将以只读方式打开 我的问题是,我的电脑上是否打开了这些工作簿(B、C、D和E)。代码将尝试重新打开这些工作簿,这将触发一条消息,提示: “B.xlsm已打开。重新打开将导致放弃您所做的任何更改。是否要重新打开B.xlsm?” 单击“是”将关闭现有工作簿(B)而不保存并重新打开。 单击“否”将弹出此运行时错误“1004”:方法“打开对象工作簿”失败 如何更改此代码,以便如果在我的计算机上打开工作簿(B、C、D和E)(由我打开,而不是只读),它将继续执行代码而不重新打开它 你们这些天才能帮我解决这个问题吗 我的代码:Vba 需要有关在网络上打开另一个工作簿的帮助吗,vba,excel,Vba,Excel,你能帮我弄清楚吗 我需要在工作簿(a)上运行一些代码,以便在网络上打开其他两个工作簿(B、C、D和E)。这些其他的工作手册经常被其他人使用。所以我打开这些其他的工作簿没有问题。。。如果这些工作簿当前正被其他人使用,它将以只读方式打开 我的问题是,我的电脑上是否打开了这些工作簿(B、C、D和E)。代码将尝试重新打开这些工作簿,这将触发一条消息,提示: “B.xlsm已打开。重新打开将导致放弃您所做的任何更改。是否要重新打开B.xlsm?” 单击“是”将关闭现有工作簿(B)而不保存并重新打开。 单击
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Sub test2()
Dim FolderPath As String
Dim filePath As String
Dim wBook As String
FolderPath = Application.ActiveWorkbook.Path
filePath = Left(FolderPath, InStrRev(FolderPath, "\") - 1)
wBook = filePath & "\Appeals 01.xlsm"
'If Workbook is Opened
If IsWorkBookOpen(filePath & "\Appeals 01.xlsm") Then
If MsgBox("Appeal 01 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _
"Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub
Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm"
Else
Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm"
End If
MsgBox ("Continue Code")
End Sub
希望你们能帮我…谢谢你们:)
更新:多亏了Tbizness,我将代码修改为:
Function IsWorkBookOpen(FileName As String)
Dim ff As Long, ErrNo As Long
On Error Resume Next
ff = FreeFile()
Open FileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0
Select Case ErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function
Sub test2()
Dim FolderPath As String
Dim filePath As String
Dim wBook As String
FolderPath = Application.ActiveWorkbook.Path
filePath = Left(FolderPath, InStrRev(FolderPath, "\") - 1)
wBook = filePath & "\Appeals 01.xlsm"
'Set Boolean to True if it's open on my computer
For Each WB1 In Application.Workbooks
If WB1.Name = "Appeals 01.xlsm" Then
Appeal01bool = True
ElseIf WB1.Name = "Appeals 02.xlsm" Then
Appeal02bool = True
End If
Next
'If Appeal 01.xlsm is not open on my computer
If Appeal01bool = False Then
'Then is it opened by others
If IsWorkBookOpen(filePath & "\Appeals 01.xlsm") Then
'If it is opened by others, do you want to open as Read-only?
If MsgBox("Appeal 01 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _
"Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub
'Yes to open as read-only
Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm"
Else
Workbooks.Open FileName:=filePath & "\Appeals 01.xlsm"
End If
'Save workbbook first if it is opened on this computer
Workbooks("Appeals 01.xlsm").Save
End If
'If Appeal 02.xlsm is not open on my computer
If Appeal02bool = False Then
'Then is it opened by others
If IsWorkBookOpen(filePath & "\Appeals 02.xlsm") Then
'If it is opened by others, do you want to open as Read-only?
If MsgBox("Appeal 02 is Opened. Do you want to open workbook as Read only?" & vbNewLine & vbNewLine & _
"Warning!!! Running numbers on Read-only mode can cause report not total correctly", vbYesNo, "Already Opened") = vbNo Then Exit Sub
'Yes to open as read-only
Workbooks.Open FileName:=filePath & "\Appeals 02.xlsm"
Else
Workbooks.Open FileName:=filePath & "\Appeals 02.xlsm"
End If
'Save workbbook first if it is opened on this computer
Workbooks("Appeals 02.xlsm").Save
End If
MsgBox ("Continue Code")
End Sub
我会使用一个简单的for look来检查打开的工作簿的所有标题,如果它是打开的,则将布尔值设置为true,然后在打开任何工作簿之前检查布尔值:
for each wb in application.workbooks
if wb.name = b then
bbool = True
elseif wb.name = c then
cbool = True
elseif wb.name = d then
dbool = True
elseif wb.name = e then
ebool = True
end if
Next
if bbool = false then application.workbooks.open(b)
if cbool = false then application.workbooks.open(c)
if dbool = false then application.workbooks.open(d)
if ebool = false then application.workbooks.open(e)
这对我来说太好了。谢谢Tbizness。我是VBA的初学者,今天刚学布尔运算。谢谢。:)