Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 10858|回复: 5

[已解决]点击打开文件的对话框的“取消”键,如何不出现错误?

[复制链接]
发表于 2012-9-5 16:44 | 显示全部楼层 |阅读模式
本帖最后由 秋意正浓 于 2012-9-5 16:56 编辑

Set fdg = Application.FileDialog(msoFileDialogOpen)  '表示文件对话框的实例。
    With fdg
        .Filters.Clear  '清除filter,注意顺序
        .Filters.Add "EXCEL2003(*.XLS)", "*.xls"   '设置过滤器
        .Filters.Add "所有文件(*.*)", "*.*"   '设置过滤器
        .InitialFileName = ThisWorkbook.Path  '设置默认文件夹
        .AllowMultiSelect = False
        .Show
        For lngCount = 1 To .SelectedItems.Count     '循环所有选中的文件
            wbname = .SelectedItems(lngCount)    '打开所有选中的文件
            Workbooks.Open filename:=wbname
        Next lngCount
    End With
以上代码是选择打开指定文件的代码,但是,当我点弹出来的对话框的“取消”键时,提示代码错误,如何避免这个错误?
最佳答案
2012-9-5 18:26
对FILEDIALOG的返回参数要做判断。
FileDialog.Show 方法显示文件对话框并返回一个 Long 类型的值,指示用户按下的是“操作”按钮 (-1) 还是“取消”按钮 (0)。 你对SHOW的返回值要做判断才行。
  1. Sub Main()

  2.     'Declare a variable as a FileDialog object.
  3.     Dim fd As FileDialog

  4.     'Create a FileDialog object as a File Picker dialog box.
  5.     Set fd = Application.FileDialog(msoFileDialogFilePicker)

  6.     'Declare a variable to contain the path
  7.     'of each selected item. Even though the path is aString,
  8.     'the variable must be a Variant because For Each...Next
  9.     'routines only work with Variants and Objects.
  10.     Dim vrtSelectedItem As Variant

  11.     'Use a With...End With block to reference the FileDialog object.
  12.     With fd

  13.         'Use the Show method to display the File Picker dialog box and return the user's action.
  14.         'The user pressed the button.
  15.     If .Show = -1 Then

  16.             'Step through each string in the FileDialogSelectedItems collection.
  17.             For Each vrtSelectedItem In .SelectedItems

  18.                 'vrtSelectedItem is a string that contains the path of each selected item.
  19.                 'You can use any file I/O functions that you want to work with this path.
  20.                 'This example displays the path in a message box.
  21.                 MsgBox "The path is: " & vrtSelectedItem

  22.             Next vrtSelectedItem
  23.         'The user pressed Cancel.
  24.     Else
  25.         End If
  26.     End With

  27.     'Set the object variable to nothing.
  28.     Set fd = Nothing

复制代码
图片02.jpg

班表导入.zip

11.33 KB, 下载次数: 11

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-9-5 16:48 | 显示全部楼层
        If .Show = -1 Then
            For lngCount = 1 To .SelectedItems.Count     '循环所有选中的文件
            wbname = .SelectedItems(lngCount)    '打开所有选中的文件
            Workbooks.Open filename:=wbname
            Next lngCount
        End If
回复

使用道具 举报

 楼主| 发表于 2012-9-5 16:55 | 显示全部楼层
本帖最后由 秋意正浓 于 2012-9-5 16:59 编辑

回复二楼,好像不行,当我点取消的时候,工作表自动关闭了,我希望是当我点取消时,是退出对话框就行了,不要关闭工作表啊,已上传附件,供参考!
回复

使用道具 举报

发表于 2012-9-5 16:58 | 显示全部楼层
不会啊,你把 上面一行的 .show去掉,还有个问题,上面multiselect=false了,下面干嘛还用循环?
回复

使用道具 举报

 楼主| 发表于 2012-9-5 17:20 | 显示全部楼层
我也不太懂这这个,反正我的目的就是要点击按纽,然后弹出一个对话框,之后选定工作表,点确定就打开,点取消就退出对话框。
弹出的对话框所指向的默认路径要是当前文件所在的路径,谢谢各位
回复

使用道具 举报

发表于 2012-9-5 18:26 | 显示全部楼层    本楼为最佳答案   
对FILEDIALOG的返回参数要做判断。
FileDialog.Show 方法显示文件对话框并返回一个 Long 类型的值,指示用户按下的是“操作”按钮 (-1) 还是“取消”按钮 (0)。 你对SHOW的返回值要做判断才行。
  1. Sub Main()

  2.     'Declare a variable as a FileDialog object.
  3.     Dim fd As FileDialog

  4.     'Create a FileDialog object as a File Picker dialog box.
  5.     Set fd = Application.FileDialog(msoFileDialogFilePicker)

  6.     'Declare a variable to contain the path
  7.     'of each selected item. Even though the path is aString,
  8.     'the variable must be a Variant because For Each...Next
  9.     'routines only work with Variants and Objects.
  10.     Dim vrtSelectedItem As Variant

  11.     'Use a With...End With block to reference the FileDialog object.
  12.     With fd

  13.         'Use the Show method to display the File Picker dialog box and return the user's action.
  14.         'The user pressed the button.
  15.     If .Show = -1 Then

  16.             'Step through each string in the FileDialogSelectedItems collection.
  17.             For Each vrtSelectedItem In .SelectedItems

  18.                 'vrtSelectedItem is a string that contains the path of each selected item.
  19.                 'You can use any file I/O functions that you want to work with this path.
  20.                 'This example displays the path in a message box.
  21.                 MsgBox "The path is: " & vrtSelectedItem

  22.             Next vrtSelectedItem
  23.         'The user pressed Cancel.
  24.     Else
  25.         End If
  26.     End With

  27.     'Set the object variable to nothing.
  28.     Set fd = Nothing

复制代码

评分

参与人数 1 +1 收起 理由
秋意正浓 + 1 很给力!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 21:35 , Processed in 0.379640 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表