はじめに
しょっちゅう使いまわすVBA関数をまとめておく
気まぐれに増えます。たぶん。
シート関連
シート有無確認
' シートの有無を確認
Function IsExistSheet(targetName As String) As Boolean
Dim ws As Worksheet
IsExistSheet = False
For Each ws In Worksheets
If ws.Name = targetName Then
IsExistSheet = True
End If
Next
End Function
指定したシートを削除
' シートを削除
Function DeleteSheet(targetName As String)
Application.DisplayAlerts = False
Worksheets(targetName).Delete
Application.DisplayAlerts = True
End Function
図形関係
アクティブシートの図形をすべて削除
' 図形をすべて削除
Function DeleteAllShapes()
ActiveSheet.Shapes.SelectAll
Selection.Delete
End Function
ファイル関係
フォルダ選択
Function GetFolderPath() As String
'フォルダ選択のダイアログボックスを開く
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
If dlg.Show = False Then Exit Function
'選択したフォルダのパス名を取得
GetFolderPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
End Function
ファイル選択
Function GetFilePath() As String
'ファイル選択のダイアログボックスを開く
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
If dlg.Show = False Then Exit Function
'選択したファイルのパス名を取得
GetFolderPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
End Function