| 
    
     |  | non さん、おはようございます。 
 >開くファイルを,ダイアログボックスから選択したいのですが,
 >>  Set wb = Workbooks.Open(Filename:="C:\NFL\NFL2008.xls")
 >のところを,
 >  Set wb = Application.GetOpenFilename("CSVファイル(*.csv), *.csv")
 >とすると,うまくいきません。
 
 GetOpenFilenameメソッドは、
 ユーザーによって選択、または入力されたファイルの名前とパス名を返します。
 入力が取り消された場合には False が返されます。
 ということなので、オブジェクト変数に直接Setはできません。
 
 (案1)前回と同じくエラースキップ
 Sub test()
 Dim wb As Workbook, Ifile As Variant
 'ファイル名取得
 Ifile = Application.GetOpenFilename("CSVファイル(*.csv), *.csv")
 If TypeName(Ifile) = "Boolean" Then
 MsgBox "キャンセルしました", vbExclamation, "ファイル選択"
 Else
 'ここでセットして分岐
 On Error Resume Next 'スキップ
 Set wb = Application.Workbooks.Open(Ifile)
 On Error GoTo 0 '通常に戻す
 '結果の分岐
 If wb Is Nothing Then
 MsgBox "キャンセルしました", vbExclamation, "同じファイルを開く"
 Else
 MsgBox wb.FullName, vbInformation, "Open"
 End If
 End If
 End Sub
 
 (案2)ファイル名を取得しているのでチェック
 Sub test()
 Dim wb As Workbook, Ifile As Variant, s1 As String
 '
 Ifile = Application.GetOpenFilename("CSVファイル(*.csv), *.csv")
 If TypeName(Ifile) = "Boolean" Then
 MsgBox "キャンセルしました", vbExclamation, "ファイル選択"
 Else
 '総当たりチェック
 For Each wb In Application.Workbooks
 If wb.Name = Dir(Ifile) Then Exit For
 Next
 '分岐
 If wb Is Nothing Then
 'なかったら無条件で開く
 Set wb = Application.Workbooks.Open(Ifile): s1 = "開いた"
 Else
 'あったら分岐
 If MsgBox(Ifile & "を開きますか?", vbYesNo, "既存") = vbYes Then
 wb.Saved = True: wb.Close
 Set wb = Application.Workbooks.Open(Ifile): s1 = "開いた"
 Else
 wb.Activate: s1 = "そのまま"
 End If
 End If
 '結果を表示して終わり
 MsgBox wb.FullName, vbInformation, s1
 Set wb = Nothing
 End If
 End Sub
 
 こんな感じです。
 
 
 |  |