hiko-blog

VBA業務改善

MENU

自作関数.xlam IndexMatchとxlookup

’開発タブ¥Excelアドインから取り込み

’C:\Users\ユーザー名\AppData\Roaming\Microsoft\addins\自作関数.xlam

 

Function IndexMatch(検索値 As Variant, 検索範囲 As Range, 戻り範囲 As Range)
    Set IndexMatch = WorksheetFunction.Index(戻り範囲, WorksheetFunction.Match(検索値, 検索範囲, 0))


End Function
Function XLOOKUP(検索値, 検索列範囲, 戻り列範囲, 見つからない場合) As Variant
Dim i As Long, j As Long, master, data

XLOOKUP = 見つからない場合
master = 検索列範囲
data = 戻り列範囲

For i = LBound(master) To UBound(master)
    If master(i, 1) = 検索値 Then
    XLOOKUP = data(i, 1)
        If UBound(data, 2) > 1 Then
            For j = 2 To UBound(data, 2)
            XLOOKUP = XLOOKUP & "," & data(i, j)
            Next j
        End If
    Exit For
    End If
Next i
End Function