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

时间:2015-10-23 09:36:17

标签: excel vba import-from-excel getopenfilename

我正在将工作簿中的特定工作表导入到我正在使用的当前工作簿中。通过在再次导入之前删除当前工作表,导入工作正常。有一件小事需要修复。当我取消或退出filespec = Application.GetOpenFilename() If filespec = False Then Exit Sub 应用程序时,它附带:

  

未找到False.xlsx(...)

所以我补充说:

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

如何成功退出或取消<div id="title"> <input type="text" name="title"/> </div> <div id="revision"> <select name="posts"> <option>Posts Here</option> </select> </div> <script> $('#revision').hide(); $('#title').hide(); $('input[name="post_type"]').change(function() { var isRevision = $('input:checked[name="post_type"]').val() == "revision"; $('#title').toggle(!isRevision); $('#revision').toggle(isRevision); }); </script> 应用程序并仅询问该文件一次?

1 个答案:

答案 0 :(得分:2)

变量filespec必须是公共的,如果你想在另一个sub中使用它。 在&#34; Sub import_click()&#34;之前添加此行。 :Public filespec As Variant 并在子导入

中删除/注释行filespec = Application.GetOpenFilename()