Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 对XlFind的更多控制_Vb.net_Vba_Excel - Fatal编程技术网

Vb.net 对XlFind的更多控制

Vb.net 对XlFind的更多控制,vb.net,vba,excel,Vb.net,Vba,Excel,我使用脚本允许用户在excel工作表的列中查找序列号。然而,一个问题是搜索的灵活性 序列号可能出现在以下示例中:“12345678”、“1 345678”、“12 345678”如您所见,这些数字后面的空格数量不同,这会导致问题。此外,前两位(或偶尔是一位)表示序列号的年份,可以用空格分隔,也可以不用空格分隔 是否有任何方法可以解释这些变化,而不必执行多次搜索或调整数据 Private Function ExcelFind(r As Excel.Range, s As String) D

我使用脚本允许用户在excel工作表的列中查找序列号。然而,一个问题是搜索的灵活性

序列号可能出现在以下示例中:“12345678”、“1 345678”、“12 345678”如您所见,这些数字后面的空格数量不同,这会导致问题。此外,前两位(或偶尔是一位)表示序列号的年份,可以用空格分隔,也可以不用空格分隔

是否有任何方法可以解释这些变化,而不必执行多次搜索或调整数据

Private Function ExcelFind(r As Excel.Range, s As String)
    Dim currentFind As Excel.Range = Nothing
    currentFind = r.Find(s, ,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)

    If currentFind IsNot Nothing Then
        Debug.Print("foundrow " & currentFind.Row)
        Debug.Print("foundcol " & currentFind.Column)
        Return (currentFind)
    Else
        Debug.Print("not found (EF1)")
        Return Nothing
    End If
End Function
插入

在你寻找它之前。这将删除变量s中的所有空格

    For Each cell In Rng
    Str = cell.Value
    Str = Replace(Str, " ", "")
        If s = Str Then
            Debug.Print ("foundrow " & cell.Row)
            Debug.Print ("foundcol " & cell.Column)
        End If
    Next

好的,一种使用数组进行检查的方法——这将比用Maddy Nikam的答案检查每个单元格要快得多。 我对A1:AN10000进行了一次跑动,花了大约3秒钟。仍然不是最优的,但比逐个单元的全范围检查更可行

Sub test(r As Excel.Range, s As String)
Dim arr() As Variant
Dim cl as long
Dim rw as long

arr = r.value

cl = 1
Do While cl <= r.Columns.Count
    rw = 1
    Do While rw <= r.Rows.Count
        arr(rw, cl) = Replace(arr(rw, cl), " ", "")
        If arr(rw, cl) = s Then
            Debug.Print r.Cells(rw, cl).Address
        End If
        rw = rw + 1
    Loop
    cl = cl + 1
Loop
End Sub
子测试(r为Excel.Range,s为字符串)
Dim arr()作为变量
如长
变暗rw为长
arr=r.value
cl=1

在cl
范围内时执行。Find
在搜索值中支持通配符,例如
*123*
将查找
2017 1230993
。就空间而言,你可以做一个修剪+替换结构来去除这些。我不确定我是否理解你的额外评论。。。序列号YYWWxxxx的格式是否正确?通配符看起来很有用,但我知道这会导致误报,例如,在查找“16 1234567*”时,存在“16 12345678”。希望有一种方法可以忽略这些尾随空格,但仍然要寻找一个精确的匹配。trim+substitute可以用于范围吗?是的,但是您必须更改单元格中的值。另一种方法是将
r
中的所有内容转储到一个数组中,去掉其中的空格,并在其上循环以找到精确的值。因此,如果
s=“1345678”
,是否要在工作表中搜索该数字?而
“1345678”
可以表示为
“1345678”
“1345678”或
“1345678”`?一个单元格中是否只有一个数字或可以有多个数字?
s
是要搜索的字符串。问题就在他看到的范围内不不是。。。问题是空格可以在两端或中间引入
currentFind=r.Find(s,
-这意味着
s
是搜索值。这与
s
中的空格无关,但与属于
r
一部分的单元格值中的空格有关。不正确…s是搜索值…从该搜索值中删除假空格是唯一合理的答案抱歉,瑞克,我得到了它,你是对的…是搜索列有空格…同样,我的解释是,在找到excelfind函数之前,我是这样做的,遗憾的是,这需要更长的时间才能完成。这是一个问题,因为范围有数千行。这会很好地工作-只有
r
中有数千个单元格时,性能才是一个问题。@Zyzyx..c创建一个附加列,该列可以包含这些序列号而不包含空格。您还可以执行r。替换内容:=“”,替换:=“”,查看:=xlPart
Sub test(r As Excel.Range, s As String)
Dim arr() As Variant
Dim cl as long
Dim rw as long

arr = r.value

cl = 1
Do While cl <= r.Columns.Count
    rw = 1
    Do While rw <= r.Rows.Count
        arr(rw, cl) = Replace(arr(rw, cl), " ", "")
        If arr(rw, cl) = s Then
            Debug.Print r.Cells(rw, cl).Address
        End If
        rw = rw + 1
    Loop
    cl = cl + 1
Loop
End Sub