Attribute VB_Name = "Module1" Option Explicit '====================================================================== ' ' 《機能》 ' https://software77.net/geo-ip/ から入手できるデータを用いて、 ' IPv4のIPアドレスから国名、ネットワーク名を取得する ' ' ■前準備 ' ' 1) https://software77.net/geo-ip/ からIP to Country Databaseを取得する ' 2) 適当なExcelブックを作成し『IpToCountry』シートを作成する ' 3) 取得したCSV形式のファイルより先頭部分のコメント行を取り除いたデータ ' 部分(Ver. 5.9.7の場合、341行目以降のデータ)を『IpToCountry』シート ' 2行目以降に貼り付ける ' ' データが下記のように並んでいること ' A列: IP FROM ' B列: IP TO ' C列: REGISTRY ' D列: ASSIGNED ' E列: CTRY ' F列: CNTRY ' G列: COUNTRY ' ' ■使い方(VBA) ' CountryName = IP2COUNTRY("202.226.xxx.xxx", COUNTRY) ' ' ■使い方(Excelシート中) ' = IP2COUNTRY(A2, COUNTRY) ' ' This software is distributed under the license of NYSL. ' ( http://www.kmonos.net/nysl/ ) ' '====================================================================== '---------------------------------------------------------------------- ' IP2COUNTRYで取得するフィールド名 '---------------------------------------------------------------------- Public Enum Ip2CountryFields ' レジストリ REGISTRY = 3 ' IP/ブロックが割り当てられた日付 ASSIGNED = 4 ' 国名(略々称) CTRY = 5 ' 国名(略称) CNTRY = 6 ' 国名 COUNTRY = 7 End Enum '---------------------------------------------------------------------- ' IPv4のIPアドレスから国名、ネットワーク名を取得する ' ' - 引数 - ' ip : IPv4のxxx.xxx.xxx.xxx形式のIPアドレス ' fields: 取得するフィールド(Ip2CountryFieldsを参照) ' ' - 引数 - ' 国名またはレジストリ、IP/ブロックが割り当てられた日付 ' '---------------------------------------------------------------------- Public Function IP2COUNTRY(ip As String, Optional field As Ip2CountryFields = COUNTRY) As String On Error Resume Next Dim tokens() As String Dim numericIP As Variant ' IPアドレスを.で分解して数値化 tokens = Split(ip, ".") If UBound(tokens) < 3 Then IP2COUNTRY = "" Exit Function End If numericIP = tokens(0) * 256 ^ 3 + tokens(1) * 256 ^ 2 + tokens(2) * 256 ^ 1 + tokens(3) * 256 ^ 0 ' 予約済みのネットワークに該当するかチェック ' Private = 10.0.0.0 - 10.255.255.255/172.16.0.0 - 172.31.255.255/192.168.0.0 - 192.168.255.255 If (numericIP >= 167772160# And numericIP <= 184549375#) _ Or (numericIP >= 2886729728# And numericIP <= 2886795263#) _ Or (numericIP >= 3232235520# And numericIP <= 3232301055#) Then IP2COUNTRY = IIf(field >= CTRY, "Private", "") Exit Function End If ' Software = 0.0.0.0 - 0.255.255.255 If (numericIP >= 0 And numericIP <= 16777215#) Then IP2COUNTRY = IIf(field >= CTRY, "Software", "") Exit Function End If ' Private = 100.64.0.0 - 100.127.255.255 If (numericIP >= 1681915904# And numericIP <= 1686110207#) Then IP2COUNTRY = IIf(field >= CTRY, "Private(Shared address space)", "") Exit Function End If ' Host = 127.0.0.0 - 127.255.255.255 If (numericIP >= 2130706432# And numericIP <= 2147483647#) Then IP2COUNTRY = IIf(field >= CTRY, "Host(Loopback addresses)", "") Exit Function End If ' Subnet = 169.254.0.0 - 169.254.255.255 If (numericIP >= 2851995648# And numericIP <= 2852061183#) Then IP2COUNTRY = IIf(field >= CTRY, "Subnet(Link-local addresses)", "") Exit Function End If ' Private = 192.0.0.0 - 192.0.0.255 If (numericIP >= 3221225472# And numericIP <= 3221225727#) Then IP2COUNTRY = IIf(field >= CTRY, "Private(IETF Protocol Assignments)", "") Exit Function End If ' Documentation = 192.0.2.0 - 192.0.2.255 If (numericIP >= 3221225984# And numericIP <= 3221226239#) Then IP2COUNTRY = IIf(field >= CTRY, "Documentation", "") Exit Function End If ' Internet = 192.88.99.0 - 192.88.99.255 If (numericIP >= 3227017984# And numericIP <= 3227018239#) Then IP2COUNTRY = IIf(field >= CTRY, "Internet(Reserved)", "") Exit Function End If ' Private = 198.18.0.0 - 198.19.255.255 If (numericIP >= 3323068416# And numericIP <= 3323199487#) Then IP2COUNTRY = IIf(field >= CTRY, "Private", "") Exit Function End If ' Documentation = 198.51.100.0 - 198.51.100.255 If (numericIP >= 3325256704# And numericIP <= 3325256959#) Then IP2COUNTRY = IIf(field >= CTRY, "Documentation", "") Exit Function End If ' Documentation = 203.0.113.0 - 203.0.113.255 If (numericIP >= 3405803776# And numericIP <= 3405804031#) Then IP2COUNTRY = IIf(field >= CTRY, "Documentation", "") Exit Function End If ' Internet = 224.0.0.0 - 239.255.255.255 If (numericIP >= 3758096384# And numericIP <= 4026531839#) Then IP2COUNTRY = IIf(field >= CTRY, "Internet(Multicast)", "") Exit Function End If ' Internet = 240.0.0.0 - 255.255.255.254 If (numericIP >= 4026531840# And numericIP <= 4294967294#) Then IP2COUNTRY = IIf(field >= CTRY, "Internet(Reserved)", "") Exit Function End If ' Subnet = 255.255.255.255 If (numericIP = 4294967295#) Then IP2COUNTRY = IIf(field >= CTRY, "Subnet", "") Exit Function End If ' 『IpToCountry』シートからデータを拾う Dim xlSheet As Excel.Worksheet Set xlSheet = ThisWorkbook.Worksheets("IpToCountry") ' データが増えたときはG300000を適当な行数に変更する IP2COUNTRY = Application.WorksheetFunction.VLookup(numericIP, xlSheet.Range("A2:G300000"), field, True) End Function