20251025 自作ライブラリ勉強会

目次

① 最終行を取得する関数

Function Get最終行(ByVal シート As Worksheet, ByVal 列番号 As Long) As Long
    
    Get最終行 = シート.Cells(シート.Rows.Count, 列番号).End(xlUp).Row
    
End Function

② 最終列を取得する関数

Function Get最終列(ByVal シート As Worksheet, ByVal 行番号 As Long) As Long

    Get最終列 = シート.Cells(行番号, シート.Columns.Count).End(xlToLeft).Column
    
End Function

③ シートが存在するかチェックする関数

Function Isシートが存在する場合True(ByVal 判定シート名 As String, ByVal 指定ブック As Workbook) As Boolean
    
    '変数宣言
    Dim ws As Worksheet
    
    'シート内検索
    For Each ws In 指定ブック.Worksheets
        
        If ws.Name = 判定シート名 Then
            
            '存在する
            Isシートが存在する場合True = True
            Exit Function
        
        End If
    Next
    
    '存在しない
    Isシートが存在する場合True = False
    
End Function

④ OneDriveパスをローカルパスに変換する関数

Function ConvOneDriveパス⇒Localパス(ByVal パス As String) As String
    
    '「https」が含まれていなかったら、変換しない
    If Not パス Like "https*" Then
        ConvOneDriveパス⇒Localパス = パス
        Exit Function
    End If
    
    '個人用OneDrive用フォルダーのパスを取得
    Dim zOneDriveパス As String
    zOneDriveパス = Environ("OneDrive")
    If zOneDriveパス = "" Then
        zOneDriveパス = Environ("OneDriveConsumer")
    End If
    
    '個人用のクラウド側のパターン文字列指定
    Dim zクラウドパターン As String
    zクラウドパターン = "https://d.docs.live.net/[^/$]+"
    
    'パターン文字列の範囲をOneDrive用フォルダのパスに変換
    With CreateObject("VBScript.RegExp")
        .Pattern = zクラウドパターン
        .IgnoreCase = True
        .Global = False
        ConvOneDriveパス⇒Localパス = .Replace(パス, zOneDriveパス)
    End With
    
    '残る「/」をセパレータに変換
    ConvOneDriveパス⇒Localパス = Replace(ConvOneDriveパス⇒Localパス, "/", Application.PathSeparator)
    
 End Function

⑤ デスクトップのパスを取得する関数

Function Getデスクトップパス() As String

    Dim wsh As Object
    Set wsh = CreateObject("WScript.shell")
    
    Getデスクトップパス = wsh.SpecialFolders("Desktop")
    
End Function

⑥ ドキュメントのパスを取得する関数

Function Getドキュメントパス() As String

    Dim wsh As Object
    Set wsh = CreateObject("WScript.shell")
    
    Getドキュメントパス = wsh.SpecialFolders("MyDocuments")
    
End Function

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次