Excel 单击“退出”或“取消”时出现错误消息;Application.GetOpenFilename";
我正在将工作簿中的特定工作表导入当前正在使用的工作簿。通过在再次导入之前删除当前图纸,导入可以连续工作。有一件小事需要解决。当我取消或退出Excel 单击“退出”或“取消”时出现错误消息;Application.GetOpenFilename";,excel,vba,import-from-excel,getopenfilename,Excel,Vba,Import From Excel,Getopenfilename,我正在将工作簿中的特定工作表导入当前正在使用的工作簿。通过在再次导入之前删除当前图纸,导入可以连续工作。有一件小事需要解决。当我取消或退出GetOpenFilename应用程序时,它附带: 找不到False.xlsx(…) 所以我补充说: filespec = Application.GetOpenFilename() If filespec = False Then Exit Sub 在子导入中单击()。但是,如果我不包括filespec=Application.GetOpenFilena
GetOpenFilename
应用程序时,它附带:
找不到False.xlsx(…)
所以我补充说:
filespec = Application.GetOpenFilename()
If filespec = False Then Exit Sub
在子导入中单击()。但是,如果我不包括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
应用程序,并只请求一次文件 如果要在另一个子系统中使用变量filespec,它必须是公共的。
在“Sub import_click()”之前添加此行:Public filespec As Variant
并在子导入中删除/comment行filespec=Application.GetOpenFilename()