You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用VBA在表格列B中精确查找列A的IP地址(多IP单元格场景)

VBA函数实现IP精确匹配

功能说明

  • 针对shcheck工作表,判断A列的单个IP是否精确存在于B列(B列单元格可能包含分号分隔的多个IP)
  • 执行后返回True/False

代码实现

打开Excel按Alt+F11打开VBA编辑器,插入模块后粘贴以下代码:

Function IsIPExists(targetIP As String) As Boolean
    Dim ws As Worksheet
    Dim cell As Range
    Dim ipArr As Variant
    Dim i As Integer
    
    ' 指定目标工作表
    Set ws = ThisWorkbook.Worksheets("shcheck")
    
    ' 遍历B列所有非空单元格
    For Each cell In ws.Range("B:B").SpecialCells(xlCellTypeConstants)
        ' 拆分分号分隔的IP,同时去除前后空格
        ipArr = Split(Trim(cell.Value), ";")
        For i = LBound(ipArr) To UBound(ipArr)
            ' 精确匹配IP(去除每个IP前后的空格)
            If Trim(ipArr(i)) = targetIP Then
                IsIPExists = True
                Exit Function
            End If
        Next i
    Next cell
    
    ' 遍历完未找到则返回False
    IsIPExists = False
End Function

使用方法

在工作表的空白单元格(比如C2)输入公式:=IsIPExists(A2),下拉填充即可得到对应结果。

注意事项

  • 自动忽略IP前后的空格(比如10.198.80.71 ; 10.198.81.71这类格式也能正确匹配)
  • 仅遍历B列非空单元格,提升运行效率
  • 确保shcheck工作表名称正确,若名称不同需修改代码中对应部分

内容的提问来源于stack exchange,提问作者ninasagova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:52:32