在“Application.GetOpenFilename”中单击退出或取消时出现错误消息

我正在将工作簿中的特定工作表导入到当前正在使用的工作簿中。 导入重新导入之前,通过删除当前工作表连续罚款。 有一件事情需要修正。 当我取消或退出GetOpenFilename应用程序时,它带有:

False.xlsx未find(…)

所以我补充说:

 filespec = Application.GetOpenFilename() If filespec = False Then Exit Sub 

Sub import_click() ,但是我不想让我问这个文件两次。 但是,如果我不包括filespec = Application.GetOpenFilename()它不起作用。 代码如下:

 Sub import_click() filespec = Application.GetOpenFilename() If filespec = False Then Exit Sub Call deletedatasheet Call import MsgBox "Data imported", vbInformation End Sub Private Sub import() Dim wsMaster As Worksheet Dim rd As Range Application.ScreenUpdating = False Application.DisplayAlerts = False If wsMaster Is Nothing Then ThisWorkbook.Sheets.Add Set wsMaster = ActiveSheet Set rd = wsMaster.Range("A1") wsMaster.Name = "Reviewed" filespec = Application.GetOpenFilename() Set wb = Workbooks.Open(Filename:=filespec) Sheets("Reviewed").Activate Cells.Copy rd wb.Close End If Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub Sub deletedatasheet() Dim ws As Worksheet Application.DisplayAlerts = False For Each ws In ThisWorkbook.Sheets If ws.Name = "Reviewed" Then ws.Delete End If Next Application.DisplayAlerts = True End Sub 

如何成功地退出或取消GetOpenFilename应用程序,只请求文件一次?

variablesfilespec必须公开,如果你想在另一个子使用它。 在“Sub import_click()”之前添加下面的行: Public filespec As Variant and delete / comment line filespec = Application.GetOpenFilename() in Sub import