我已经完成了一些工作簿的摘要工作簿,这些工作簿位于一些不同的驱动器中,我想通过简单地打开文件来更新(因为它们依赖于我的工作簿中的INDIRECT),并且在扩展中更新所有值(希望如此) 。我尝试过的是通过Developer选项卡和ActiveX控件实现CommandButton。然后我右键单击此按钮和"查看代码"我只是得到了#34; Private Sub CommandButton21_Click"和"结束子"我收集的意思是"按钮的名称"是CommandButton21。
我的代码如下所示:
Private Sub CommandButton21_Click()
Dim WbookCheck As Workbook
Dim sPath As String, sFile As String
On Error Resume Next
sPath = "C:\Pathtofile"
sFile = sPath & "filename.xlsx"
Set WbookCheck = Workbooks(sFile)
On Error GoTo 0
If CommandButton21.Value = True Then
If WbookCheck Is Nothing Then 'Closed
Workbooks.Open Filename:=sFile, ReadOnly:=True
ElseIf Application.ActiveWorkbook.Name = WbookCheck.Name Then
WbookCheck.Close SaveChanges:=True
Else
WbookCheck.Activate
End If
Else
WbookCheck.Close True
End If
End Sub
有人对此有所了解吗?
答案 0 :(得分:1)
我不知道为什么要尝试测试命令按钮的值,但它将为False,因此尝试关闭从未分配的工作簿变量将失败。您应该跳过Value测试并使用:
Private Sub CommandButton21_Click()
Dim WbookCheck As Workbook
Dim sPath As String, sFile As String
On Error Resume Next
sPath = "C:\Pathtofile\"
sFile = sPath & "filename.xlsx"
Set WbookCheck = Workbooks(sFile)
On Error GoTo 0
If WbookCheck Is Nothing Then 'Closed
Workbooks.Open Filename:=sFile, ReadOnly:=True
ElseIf Application.ActiveWorkbook.Name = WbookCheck.Name Then
WbookCheck.Close SaveChanges:=True
Else
WbookCheck.Activate
End If
End Sub