Excel VBA搜索功能未找到数字

时间:2011-02-23 14:58:33

标签: excel-vba vba excel

我正在使用搜索功能比较数据,以查看工作表中是否已存在标识符,但如果它是一个数字,则无法通过VB脚本找到它。如果我手动找到它就会发现它很好......这令人难以置信。

以下代码位于循环中,该循环遍历一张表中的每个SKU ID,以查看它是否存在于另一张表中。 (如果有更好的方法,请告诉我。) 如果没有找到sku它会将它添加到工作表中,我总是想出重复的数据,因为即使已经存在它也无法通过vb脚本找到它...让我知道如果这没有意义,我我会重申一下。 sku永远是独一无二的

Sheets(productsheet).Select
        On Error Resume Next
        Cells.Find(What:=sku, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate

        If Err.Number <> 0 Then
            newSKU = newSKU + 1
            Range("A" & CurrentRow).Select
            ActiveCell.Value = trimmedSku
            ActiveCell.Font.Color = red
            With Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With

            Range("B" & CurrentRow).Select
            ActiveCell.Value = skudescription
            With Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With

            Range("D" & CurrentRow).Select
            ActiveCell.Value = sku
            With Selection
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlTop
                .WrapText = True
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With

            Range("I" & CurrentRow).Select
            ActiveCell.Value = 1

            Range("K" & CurrentRow).Select
            ActiveCell.Value = cost

            Range("L" & CurrentRow).Select
            ActiveCell.Value = price

            Range("M" & CurrentRow).Select
            ActiveCell.Value = price

            Range("O" & CurrentRow).Select
            ActiveCell.Value = "Y"

            Range("P" & CurrentRow).Select
            ActiveCell.Value = "N"

            Range("Q" & CurrentRow).Select
            ActiveCell.Value = "Y"

            Range("AI" & CurrentRow).Select
            ActiveCell.Value = 1

            CurrentRow = CurrentRow + 1
        Else
            'Skip because it was found
        End If  

1 个答案:

答案 0 :(得分:0)

没有必要选择或激活任何东西来做你想做的事情。由于某些查找参数,您可能找不到该值,但我会先重写代码以避免更改工作表。像

这样的东西
Dim sh As Worksheet
Dim rFound As Range

Set sh = ThisWorkbook.Sheets("productsheet")

Set rFound = sh.Cells.Find(sku, , xlValues, xlPart)

If rFound Is Nothing Then
    Set rNext = sh.Cells(sh.Rows.Count, 1).End(xlUp).Offset(1, 0)
    rNext.Value = trimmedsku
    rNext.Offset(0, 1).Value = skudescription
    rNext.Offset(0, 3).Value = sku
    rNext.Offset(0, 8).Value = 1
    rNext.Offset(0, 10).Value = cost
    rNext.Offset(0, 11).Value = price
    rNext.Offset(0, 12).Value = price
    rNext.Offset(0, 14).Value = "Y"
    rNext.Offset(0, 15).Value = "N"
    rNext.Offset(0, 16).Value = "Y"
    rNext.Offset(0, 34).Value = 1
End If

如果rFound即将出现(sku未找到)并且它不应该出现,那么请确保Find的参数是正确的。你想看看xlFormulas还是xlValues?全部还是部分?

我在Find参数中没有看到任何会使其更具限制性的内容,所以我不能就改变哪些参数给出任何具体建议。也许你可以制作一个类似于我发布的新测试程序,看看是否没有更换工作表帮助。