2016-04-01 29 views
-1

我在我的家庭網絡上保存了一個包含我所有IP地址的電子表格。我已經爲靜態配置設定了50個地址範圍,並從那些我爲某些類型的設備(如計算機或媒體播放器NAS等)定義了幾個「組」。因此,我在其他地方有未使用的地址,這導致了我對我的實際問題。如何在excel中編寫一個公式來確定可用地址,並且還希望它計算下一個和第三個可用地址,以及我將用小數點1或2或3分隔的地址在excel中計算未使用的IP

enter image description here

回答

0

你可以用一些非常有創意的方式做到這一點,即時通訊不是很好,但如果vba可以,我可以做到這一點。

Public Sub GetIPs() 

Dim RngArr As Variant 
Dim IPRange(0 To 255) As Boolean 'you may want to change this to account for reserved IPs 
Dim Rw As Long 
Dim Results As Worksheet 

RngArr = Sheet1.UsedRange.Columns("A:C").Value 

If Not IsArray(RngArr) Then Exit Sub 'make sure we have more than 1 cell of data 

For Rw = LBound(RngArr, 1) To UBound(RngArr, 1) 
    If RngArr(Rw, 2) Like "*.*.*.*" Then 'Just a very simple wilcard search for an IP 
     IPRange(CByte(Split(RngArr(Rw, 2), ".")(3))) = True 'set IPRange to true on the index/IP number 
    End If 
Next Rw 

Set Results = ThisWorkbook.Sheets.Add 

For Rw = LBound(IPRange, 1) To UBound(IPRange, 1) 
    Results.Cells(1 + Rw, 1).Value = "192.168.1." & Rw 
    If IPRange(Rw) = True Then 
     Results.Cells(1 + Rw, 2).Value = "Taken" 
    Else 
     Results.Cells(1 + Rw, 2).Value = "Available" 
    End If 
Next Rw 
End Sub 

這裏假設你的表是(代號)工作表Sheet1(這是它在VBA屬性窗口中的名稱)本所要求的變化。 它還假設您的IP數據在列B 它將創建一個新的工作表,其中包含所有已獲取和可用IP地址的列表,只需根據需要進行編輯即可。

+0

這就是我在看的東西,如果這可以以我可以擁有第一個第二和第三個可用IP的方式工作,那麼我會是完美的。我試圖用vlookup和if語句(單獨和一起作爲一個公式)使用它,但我無法得到一個說明第一,第二或第三個參數的參數 – Henk

+0

如果您想要使用此方法,您可以只是循環使用採取/可用列和列出可用IP,或者只修改原始代碼(最後一個循環)忽略Taken IPS並列出可用的代碼 –

0

我爲Excel創建了一些IP(IPv4和IPv6)例程。我有一個,SubnetIPv4(IPv4 As String, Bits As Integer, Offset As Long) As String

警告:我創建了這些作爲快速和骯髒的解決方案,我已經使用了好幾年。

這是其他功能的常見功能:

Function CountStr(Source As String, Target As String) As Integer 
    Dim c, i As Integer 
    c = 0 
    If Not ((Source = "") Or (Target = "")) Then 
     For i = 1 To Len(Source) 
      If Mid(Source, i, Len(Target)) = Target Then 
       c = c + 1 
      End If 
     Next 
    End If 
    CountStr = c 
End Function 

的IPv4:

注:

  1. 宏必須啓用
  2. IPv4是一個字符串表示以點分十進制形式的IPv4地址 格式
  3. 位是一個整數(0至32)表示掩模的比特數
  4. Offset是表示主機地址偏移到 子網
  5. 使用膠版0將返回子網任何IP地址的整數
  6. 使用的255.255.255.255 IPv4地址和偏移0將返回位 大小

口罩,此功能將確定STR ING是一個IPv4地址:

Function IsIPv4(IPv4 As String) As Boolean 
    Dim s As String 
    On Error GoTo InvalidIPv4 
    s = SubnetIPv4(IPv4, 32, 0) 
    IsIPv4 = True 
    Exit Function 
InvalidIPv4: 
    IsIPv4 = False 
End Function 

該函數將返回IPv4地址:

Function SubnetIPv4(IPv4 As String, Bits As Integer, Offset As Long) As String 
    Dim a() As String 
    Dim c, d, i As Integer 
    Dim m As Long 
    Dim s As String 
    If IPv4 = "" Then 
     GoTo InvalidIPv4 
    End If 
    c = CountStr(IPv4, ".") 
    If c <> 3 Then 
     GoTo InvalidIPv4 
    End If 
    c = CountStr(IPv4, "..") 
    If c > 1 Then 
     GoTo InvalidIPv4 
    End If 
    If (Left(IPv4, 1) = ".") Or (Right(IPv4, 1) = ".") Then 
     GoTo InvalidIPv4 
    End If 
    a = Split(IPv4, ".") 
    If UBound(a) <> 3 Then 
     GoTo InvalidIPv4 
    End If 
    On Error GoTo InvalidIPv4 
    For i = 0 To 3 
     If (Len(a(i)) > 0) And (Len(a(i)) < 4) Then 
      a(i) = CInt(a(i)) 
      If (a(i) < 0) Or (a(i) > 255) Then 
       GoTo InvalidIPv4 
      End If 
     Else 
      GoTo InvalidIPv4 
     End If 
    Next 
    If (Bits < 0) Or (Bits > 32) Then 
     GoTo InvalidIPv4 
    End If 
    c = Bits Mod 8 
    d = Bits \ 8 
    If (Bits <> 0) And (c = 0) Then 
     c = 8 
     d = d - 1 
    End If 
    m = 0 
    For i = 0 To 7 
     m = m * 2 
     If c > 0 Then 
      m = m + 1 
      c = c - 1 
     End If 
    Next 
    a(d) = CStr(CLng(a(d)) And m) 
    For i = d + 1 To 3 
     a(i) = "0" 
    Next 
    If Offset < 0 Then 
     GoTo InvalidIPv4 
    End If 
    m = 0 
    For i = 1 To (32 - Bits) 
     m = m * 2 
     m = m + 1 
    Next 
    If Offset > m Then 
     GoTo InvalidIPv4 
    End If 
    m = Offset 
    For i = 3 To 0 Step -1 
     a(i) = a(i) + (m Mod 256) 
     m = m \ 256 
    Next 
    s = "" 
    For i = 0 To 3 
     s = s + CStr(a(i)) + "." 
    Next 
    s = Left(s, Len(s) - 1) 
    SubnetIPv4 = s 
    Exit Function 
InvalidIPv4: 
    Error (3) 
End Function 

的IPv6:

注:

  1. 宏必須啓用
  2. IPv6是一個字符串表示標準格式 (前導0是任選的並且::作品)
  3. 比特的IPv6地址被表示屏蔽位
  4. 偏移的數目的整數(0至128)表示主機地址的字符串中的偏移量 子網標準格式(前導零是可選的,::作品)
  5. 使用膠印相當於0::::00:0:0:0:0:0:0:0等)將 返回一個子網的任何IP地址
  6. 使用0的IPv6地址和偏移 相當於0將返回位大小的掩模

此功能將確定該字符串是否是IPv6地址:

Function IsIPv6(IPv6 As String) As Boolean 
    Dim s As String 
    On Error GoTo InvalidIPv6 
    s = SubnetIPv6(IPv6, 128, "::") 
    IsIPv6 = True 
    Exit Function 
InvalidIPv6: 
    IsIPv6 = False 
End Function 

該函數將返回一個IPv6地址:

Function SubnetIPv6(IPv6 As String, Bits As Integer, Offset As String) As String 
    Dim a() As String 
    Dim c, d, i As Integer 
    Dim m As Long 
    Dim s, t As String 
    If IPv6 = "" Then 
     GoTo InvalidIPv6 
    End If 
    c = CountStr(IPv6, ":") 
    If (c < 2) Or (c > 8) Then 
     GoTo InvalidIPv6 
    End If 
    d = CountStr(IPv6, "::") 
    If d > 1 Then 
     GoTo InvalidIPv6 
    End If 
    If (Left(IPv6, 1) = ":") And (Not (Left(IPv6, 2) = "::")) Then 
     GoTo InvalidIPv6 
    End If 
    If (Right(IPv6, 1) = ":") And (Not (Right(IPv6, 2) = "::")) Then 
     GoTo InvalidIPv6 
    End If 
    s = IPv6 
    If d = 1 Then 
     If Left(s, 2) = "::" Then 
      s = "0" + s 
     End If 
     If Right(s, 2) = "::" Then 
      s = s + "0" 
     End If 
     t = ":" 
     For i = c To 7 
      t = t + "0:" 
     Next 
     s = Replace(s, "::", t) 
    End If 
    a = Split(s, ":") 
    If UBound(a) <> 7 Then 
     GoTo InvalidIPv6 
    End If 
    On Error GoTo InvalidIPv6 
    For i = 0 To 7 
     If (Len(a(i)) > 0) And (Len(a(i)) < 5) Then 
      a(i) = WorksheetFunction.Hex2Dec(a(i)) 
     Else 
      GoTo InvalidIPv6 
     End If 
    Next 
    If (Bits < 0) Or (Bits > 128) Then 
     GoTo InvalidIPv6 
    End If 
    c = Bits Mod 16 
    d = Bits \ 16 
    If (Bits <> 0) And (c = 0) Then 
     c = 16 
     d = d - 1 
    End If 
    m = 0 
    For i = 0 To 15 
     m = m * 2 
     If c > 0 Then 
      m = m + 1 
      c = c - 1 
     End If 
    Next 
    a(d) = CStr(CLng(a(d)) And m) 
    For i = d + 1 To 7 
     a(i) = "0" 
    Next 
    If Offset = "" Then 
     GoTo InvalidIPv6 
    End If 
    c = CountStr(Offset, ":") 
    If (c < 2) Or (c > 8) Then 
     GoTo InvalidIPv6 
    End If 
    d = CountStr(Offset, "::") 
    If d > 1 Then 
     GoTo InvalidIPv6 
    End If 
    If (Left(Offset, 1) = ":") And (Not (Left(Offset, 2) = "::")) Then 
     GoTo InvalidIPv6 
    End If 
    If (Right(Offset, 1) = ":") And (Not (Right(Offset, 2) = "::")) Then 
     GoTo InvalidIPv6 
    End If 
    s = Offset 
    If d = 1 Then 
     If Left(s, 2) = "::" Then 
      s = "0" + s 
     End If 
     If Right(s, 2) = "::" Then 
      s = s + "0" 
     End If 
     t = ":" 
     For i = c To 7 
      t = t + "0:" 
     Next 
     s = Replace(s, "::", t) 
    End If 
    b = Split(s, ":") 
    If UBound(b) <> 7 Then 
     GoTo InvalidIPv6 
    End If 
    On Error GoTo InvalidIPv6 
    For i = 0 To 7 
     If (Len(b(i)) > 0) And (Len(b(i)) < 5) Then 
      b(i) = WorksheetFunction.Hex2Dec(b(i)) 
     Else 
      GoTo InvalidIPv6 
     End If 
    Next 
    c = Bits Mod 16 
    d = Bits \ 16 
    If (Bits <> 0) And (c = 0) Then 
     c = 16 
     d = d - 1 
    End If 
    m = 0 
    For i = 0 To 15 
     m = m * 2 
     If c > 0 Then 
      m = m + 1 
      c = c - 1 
     End If 
    Next 
    For i = 0 To d - 1 
     If b(i) <> "0" Then 
      GoTo InvalidIPv6 
     End If 
    Next 
    If b(d) <> CStr(CLng(b(d)) And m) Then 
     GoTo InvalidIPv6 
    End If 
    For i = 7 To d Step -1 
     a(i) = CStr(CLng(a(i)) + CLng(b(i))) 
    Next 
    s = "" 
    For i = 0 To 7 
     s = s + WorksheetFunction.Dec2Hex(a(i)) + ":" 
    Next 
    s = Left(s, Len(s) - 1) 
    SubnetIPv6 = s 
    Exit Function 
InvalidIPv6: 
    Error (3) 
End Function