使用certian数字格式在VBA中搜索

时间:2017-10-26 19:46:47

标签: excel vba excel-vba

我正在尝试创建一个使用一大块代码作为搜索词的子,并在另一行中找到它。我正在尝试搜索四位数代码,例如0032,但无论我尝试什么,它都会搜索32并最终找到11321321等部分和3211,我不想要。这就是我现在要解决的问题。

    For i = 1 To frng.Count
crng(i, 1).Value = Application.WorksheetFunction.CountIf(mfrng, cfrng(i, 1).Value)
Next i
'puts the number of times it appears next to the filtered version of it

'to find the row number
'use numbers from cfrng to search in mcode range and find rownumbers. Then add one to the rownumber and use it to copy and paste the other columns. Organize by lcode + 40000
Dim wrrow As Long
Dim wdrow As Long
Dim flist As Range
    rangevalue = ("R2:R" & flastrow)
    Set flist = Range(rangevalue)
Dim nlist As Range
    rangevalue = ("S2:S" & flastrow)
    Set nlist = Range(rangevalue)
Dim Search As Variant
Dim j As Integer
Dim n As Integer
Dim found As Object

For j = 1 To flist.Count
    Search = flist(j, 1)
    n = nlist(j, 1)
    Set found = mfrng.Find(What:=Search, LookIn:=xlValues, lookat:=xlPart, searchformat:=True)
        Dim s As Integer
        For s = 1 To n
        If s = 1 Then GoTo First Else GoTo Second

First:
        wrrow = found.Row
        wdrow = (wrrow + 1)
         wd.Activate
            Dim high As Range
            rangevalue = ("V" & wdrow & ", AA" & wdrow & ", D" & wdrow & ", E" & wdrow & ", L" & wdrow & ", M" & wdrow & ", U" & wdrow)
            Set high = Range(rangevalue)
        high.Select
        Selection.Copy
        wr.Activate
        Dim r As Integer
        Dim v As Integer
            r = wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
            v = (r + 1)
        wr.Range("B" & v).Select
        ActiveSheet.Paste
        wr.Range("A" & v).Value = j
        GoTo Third

Second:
        Set found = mfrng.FindNext(found)
        wrrow = found.Row
        wdrow = (wrrow + 1)
            wd.Activate
            rangevalue = ("V" & wdrow & ",AA" & wdrow & ",D" & wdrow & ",E" & wdrow & ",L" & wdrow & ",M" & wdrow & ",U" & wdrow)
            Set high = Range(rangevalue)
        high.Select
        Selection.Copy
            r = wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
            v = (r + 1)
        wr.Activate
        Range("B" & v).Select
        ActiveSheet.Paste
        wr.Range("A" & v).Value = j

Third:
        Next s
Next j

flist中的值显示为0032,但当我尝试使用搜索时,它代表32,而不是0032。当我尝试数字格式时,它返回一个对象所需的错误。

我已将搜索设置为变体。

我正在比较搜索内容的一些示例是0032,这是我想要的,还有21323225等。我只想让它找到行0032

我有一个搜索词列表,这是j变量的变化。我也有多次出现某个变量,这就是nlist。

1 个答案:

答案 0 :(得分:0)

根据每个人的建议,我能够弄明白。

这是我的最终代码。

Dim wrrow As Long
Dim wdrow As Long
Dim flist As Range
    rangevalue = ("R2:R" & flastrow)
    Set flist = Range(rangevalue)
Dim nlist As Range
    rangevalue = ("S2:S" & flastrow)
    Set nlist = Range(rangevalue)
Dim Search As String
Dim codenum As Integer
Dim seriesnum As Integer
Dim found As Range
Dim current As Integer
Dim high As Range
Dim blastcell As Integer
Dim bemptycell As Integer
Dim findrng As Range
    rangevalue = ("O1:O" & rlastrow)
    Set findrng = Range(rangevalue)

For codenum = 1 To flist.Count
    Search = flist(codenum, 1)
    seriesnum = nlist(codenum, 1)

        For current = 1 To seriesnum

            If current = 1 Then
                Set found = findrng.Find(What:=Search, LookAt:=xlWhole, searchformat:=True)
                wrrow = found.Row
                wdrow = (wrrow + 1)
                wd.Activate
                rangevalue = ("V" & wdrow & ", AA" & wdrow & ", D" & wdrow & ", E" & wdrow & ", L" & wdrow & ", M" & wdrow & ", U" & wdrow)
                Set high = Range(rangevalue)
                high.Select
                Selection.Copy
                wr.Activate
                blastcell = wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
                bemptycell = (blastcell + 1)
                wr.Range("B" & bemptycell).Select
                ActiveSheet.Paste
                wr.Range("A" & bemptycell).Value = codenum
                GoTo Next_item
            End If
                Set found = mfrng.FindNext(found)
                wrrow = found.Row
                wdrow = (wrrow + 1)
                wd.Activate
                rangevalue = ("V" & wdrow & ",AA" & wdrow & ",D" & wdrow & ",E" & wdrow & ",L" & wdrow & ",M" & wdrow & ",U" & wdrow)
                Set high = Range(rangevalue)
                high.Select
                Selection.Copy
                wr.Activate
                blastcell = wr.Cells(wr.Rows.Count, "B").End(xlUp).Row
                bemptycell = (blastcell + 1)
                wr.Range("B" & bemptycell).Select
                ActiveSheet.Paste
                wr.Range("A" & bemptycell).Value = codenum
Next_item:
    Next current
    Next codenum

它现在完全符合我的要求,只返回代码与密码完全匹配的单元格。