Excelでランチャーソフトを作ってみよう

Step 4 登録済みファイルを開く(Excelファイル)



Homeに戻る > Excelでアプリケーションソフト > ランチャーソフト

シートイベント

下記のVBAコードに変更してください。

'コマンドボタン クリックイベント
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim sf As String
    Dim ext As String
    
    '右クリックの場合
    If Button = 2 Then
        ButtonNo = 1
        ButtonCaption = CommandButton1.Caption
        'ファイル指定フォームを開く
        UserForm1.Show
        '見出しをコマンドボタンに表示
        CommandButton1.Caption = ButtonCaption
    Else
        sf = Range("AB1")
        ext = ExGetExt(sf)
        ExFileOpen LCase(ext), sf
    End If
End Sub



シートのVBAコード

下記のVBAコードを追加してください。

'設定されているファイルを開く
Private Sub ExFileOpen(ext As String, sfina As String)
On Error GoTo ErrEnd
    Select Case ext
        Case "xls"  'Excelファイルを開く
            Workbooks.Open Filename:=sfina
    End Select
    Exit Sub
ErrEnd:
    Beep
    MsgBox "ファイルオープン時エラーが発生しました。" & vbCrLf & _
        "エラー内容: " & Err.Description
End Sub

'拡張子を取得
Private Function ExGetExt(sfina As String) As String
    Dim i As Long
    Dim s1 As String
    
    ExGetExt = ""
    'ファイル名の最後の文字から「.」を探す
    For i = Len(sfina) To 1 Step -1
        If Mid(sfina, i, 1) = "." Then
            '拡張子を取得
            ExGetExt = Mid(sfina, i + 1)
            Exit For
        End If
    Next
End Function


Homeに戻る > Excelでアプリケーションソフト > ランチャーソフト

■■■
このサイトの内容を利用して発生した、いかなる問題にも一切責任は負いませんのでご了承下さい
■■■
当ホームページに掲載されているあらゆる内容の無許可転載・転用を禁止します


Copyright (c) Excel-Excel ! All rights reserved