Option Explicit
Sub クリア()
Dim lastRow As Integer
With ThisWorkbook.Sheets("マスタ")
lastRow = .Cells(Rows.Count, 4).End(xlUp).Row
.Range(Cells(7, 5), Cells(lastRow, 9)).ClearContents
.Range("D2:D4").ClearContents
.Range("E3:F3").ClearContents
End With
End Sub
Sub ファイルパス選択()
Dim FilePath As String
Dim strType As String
strType = "ファイル"
FilePath = getFilePath(strType)
ThisWorkbook.Sheets("マスタ").Cells(3, 4) = FilePath
End Sub
Sub フォルダパス選択()
Dim FilePath As String
Dim strType As String
strType = "フォルダ"
FilePath = getFilePath(strType)
ThisWorkbook.Sheets("マスタ").Cells(4, 4) = FilePath
End Sub
Function getFilePath(strType As String) As String
Dim objDialog As FileDialog
If strType = "ファイル" Then
Set objDialog = Application.FileDialog(msoFileDialogFilePicker)
Else
Set objDialog = Application.FileDialog(msoFileDialogFolderPicker)
End If
If strType = "ファイル" Then
objDialog.InitialFileName = "C:\Users\cryst\OneDrive\デスクトップ\テスト"
objDialog.Filters.Clear
objDialog.Filters.Add "エクセル", "*.xlsx", 1
objDialog.Filters.Add "すべてのファイル", "*.*", 2
objDialog.FilterIndex = 1
End If
If objDialog.Show Then
getFilePath = objDialog.SelectedItems(1)
Else
If strType = "ファイル" Then
MsgBox "ファイル選択をキャンセルしました"
Else
MsgBox "フォルダ選択をキャンセルしました"
End If
End If
Set objDialog = Nothing
End Function
ray88.hatenablog.com
ray88.hatenablog.com