【VBA】 よく使う処理のメモ
VBAを使用するときによく使う処理のメモ。
- Excel 2019
処理一覧
最終行取得
Function get_last_row(sheet As Worksheet, column As Long) As Long get_last_row = sheet.Cells(Rows.Count, column).End(xlUp).Row End Function
引数に最終行を取得したいシートと列を指定すれば、その最終行を取得できる。
フォルダ作成
Sub make_directory(ByVal path As String) If Dir(path, vbDirectory) = "" Then MkDir path End If End Sub
引数にフォルダパスを指定すれば、フォルダを作成できる。
フォルダが存在すれば、処理は行なわれない。
ファイルコピー
Sub file_copy(ByVal base_file As String, ByVal copy_to As String) If Dir(copy_to) = "" Then FileCopy base_file, copy_to End If End Sub
引数に元となるファイル名とコピー先のファイル名を指定するとファイルのコピーができる。
コピー先が存在する場合は処理されない。