VBA - 名称为''的文档已经开放了

时间:2014-11-30 00:52:46

标签: excel vba excel-vba

我在Excel中编写了一个宏,我将其分配给工作簿中的按钮。按下它时会询问用户文件,打开该文件并将内容复制到原始工作簿中的sheet2中,然后以新名称保存原始工作簿。然后它在工作表上创建一个新按钮以运行另一个子例程。当按下按钮时会弹出一个窗口,说“已经打开了一个带有名称(文档名称)的文档。无论如何都要解决这个问题吗?我认为这种情况正在发生,因为我正在以新名称保存原始工作簿。” p>

Sub openFile()

Dim tempWB As Object, btn As Button, desktopPath As String

fileToOpen = Application.GetOpenFilename(Title:="Select transaction history export:")
If fileToOpen <> False Then
    Set tempWB = Workbooks.Open(Filename:=fileToOpen)
End If
If fileToOpen = False Then
    End
End If

ActiveSheet.Cells.Copy
Workbooks("Test.xlsm").Sheets("Sheet2").Activate
ActiveSheet.Cells.ClearContents
ActiveSheet.Cells(1, 1).Select
ActiveSheet.Paste
tempWB.Close

Set btn = ActiveSheet.Buttons.Add(650, 50, 100, 40)
With btn
    .OnAction = "action"
End With

desktopPath = (MacScript("(path to desktop from user domain as string)") & ":")

ActiveWorkbook.SaveAs Filename:=(desktopPath & "Result" & VBA.format(Date, "mm/dd/yy") & ".xlsm")

End Sub

Sub action()

Range("A1:B1").Font.Size = 14

End Sub

0 个答案:

没有答案