| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Excel VBA ドライブ、ディレクトリを指定してファイルを開くダイアログを表示

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Excel VBA ドライブ、ディレクトリを指定してファイルを開くダイアログを表示する

Excel VBA |

1ファイルのみ選択

  1. Dim selFile As Variant
  2. Dim outPath As String
  3.  
  4. outPath = "f:\work\data"
  5.  
  6. Call ChDrive(Left$(outPath, 2))
  7. Call ChDir(outPath)
  8.  
  9. selFile = Application.GetOpenFilename("ログファイル (*.log),*.log)", , "")
  10.  
  11. If selFile = False Then
  12. Call MsgBox("ファイルが選択されませんでした", vbInformation)
  13. Exit Sub
  14. End If

複数ファイル選択

  1. Dim selFile As Variant
  2. Dim outPath As String
  3. Dim i As Integer
  4. Dim results() As String
  5. ReDim results(0)
  6.  
  7. outPath = ActiveWorkbook.Path
  8. Call ChDrive(Left$(outPath, 2))
  9. Call ChDir(outPath)
  10. selFile = Application.GetOpenFilename("Excelファイル (*.xlsx),*.xlsx)", MultiSelect:=True)
  11. If IsArray(selFile) Then
  12. ReDim results(UBound(selFile))
  13. For i = 0 To UBound(selFile) - 1
  14. results(i) = CStr(selFile(i + 1))
  15. Next
  16. Else
  17. Call MsgBox("ファイルが選択されませんでした", vbInformation)
  18. End If
  19.  
  20. ChooseFiles = results