查找其他工作表上是否存在值(Excel)

时间:2013-06-05 18:25:14

标签: excel vba excel-vba excel-formula vlookup

我的Excel工作表上有一个按钮点击事件的宏。触发此事件时,我需要查看工作表FeedSampleForm Range("A5:B5").Value上的值是否存在于我的其他工作表B的列FeedSamples中的任何位置。

任何人都可以帮助我吗?对于Excel来说,我几乎不是用户,这是我第一次“开发”它。

修改

下面的当前代码。这适用于不同的保存程序,具体取决于是在保存新记录还是在编辑以前创建的记录后保存。

对于样本数据,请说我正在寻找“FeedSampleForm”.Range(“A5:B5”)。值为“SR0238”,我需要查看“FeedSamples”工作表中B列中是否存在“SR0238” ,目前包含“SR0237” - “SR0252”。如果它不存在,我可以使用与我的添加记录功能相同的代码,但如果确实如此,我必须在保存时写入该确切的行。

Sub SaveInspection()
    If modeAdd = True Then
    'Labeler Reg. No.
    Worksheets("FeedSamples").Range("A1").End(xlDown).Offset(1, 0).value = Range("L3:M3").value
    'Feed Report No.
    Worksheets("FeedSamples").Range("B1").End(xlDown).Offset(1, 0).value = Range("A5:B5").value
    'Product No. / Class No.
    Worksheets("FeedSamples").Range("C1").End(xlDown).Offset(1, 0).value = Range("C5").value
    Worksheets("FeedSamples").Range("E1").End(xlDown).Offset(1, 0).value = Range("D5").value
    Worksheets("FeedSamples").Range("F1").End(xlDown).Offset(1, 0).value = Range("E5").value
    'Description No.
    Worksheets("FeedSamples").Range("H5").End(xlDown).Offset(1, 0).value = Range("F5").value
    Worksheets("FeedSamples").Range("I5").End(xlDown).Offset(1, 0).value = Range("G5").value
    Worksheets("FeedSamples").Range("J5").End(xlDown).Offset(1, 0).value = Range("H5").value
    Worksheets("FeedSamples").Range("K5").End(xlDown).Offset(1, 0).value = Range("I5").value
    'Possessor No.
    Worksheets("FeedSamples").Range("L1").End(xlDown).Offset(1, 0).value = Range("J5:K5").value
    'Date
    Worksheets("FeedSamples").Range("M").End(xlDown).Offset(1, 0).value = Range("L5:M5").value
    'Possessor Name
    Worksheets("FeedSamples").Range("AB1").End(xlDown).Offset(1, 0).value = Range("A8:F8").value
    'Possessor Address
    Worksheets("FeedSamples").Range("AC1").End(xlDown).Offset(1, 0).value = Range("A10:F10").value
    'Possessor City/St
    Worksheets("FeedSamples").Range("AD1").End(xlDown).Offset(1, 0).value = Range("A11:E11").value
    'POssessor Zipcode
    Worksheets("FeedSamples").Range("AE1").End(xlDown).Offset(1, 0).value = Range("F11").value
    'Labeler Name
    Worksheets("FeedSamples").Range("AF1").End(xlDown).Offset(1, 0).value = Range("H8:M8").value
    'Labeler Address
    Worksheets("FeedSamples").Range("AG1").End(xlDown).Offset(1, 0).value = Range("H10:M10").value
    'Labeler City/St
    Worksheets("FeedSamples").Range("AH1").End(xlDown).Offset(1, 0).value = Range("H11:L11").value
    'Labeler Zipcode
    Worksheets("FeedSamples").Range("AI1").End(xlDown).Offset(1, 0).value = Range("M11").value
    'Product Name
    Worksheets("FeedSamples").Range("AJ1").End(xlDown).Offset(1, 0).value = Range("A13:I13").value
    '1. Med
    Worksheets("FeedSamples").Range("AK1").End(xlDown).Offset(1, 0).value = Range("J13:K13").value
    '2. Non-Med
    Worksheets("FeedSamples").Range("AL1").End(xlDown).Offset(1, 0).value = Range("L13:M13").value
'No. Bags/Loc. Sampled

    'Total No. Guarantees
    Worksheets("FeedSamples").Range("P").End(xlDown).Offset(1, 0).value = Range("C15:E15").value
    'Flag Sample
    Worksheets("FeedSamples").Range("Q").End(xlDown).Offset(1, 0).value = Range("F15:G15").value
    'Sample Def.
    Worksheets("FeedSamples").Range("R").End(xlDown).Offset(1, 0).value = Range("H15:I15").value
    'Compliance

    'Duplicate

    'Bag Tag Mark or Code
    Worksheets("FeedSamples").Range("U").End(xlDown).Offset(1, 0).value = Range("A17:H17").value
    'On Hand
    Worksheets("FeedSamples").Range("V").End(xlDown).Offset(1, 0).value = Range("I17:K17").value
    'Approx. Wt/Lbs
    Worksheets("FeedSamples").Range("W").End(xlDown).Offset(1, 0).value = Range("L17:M17").value
    'Remarks
    Worksheets("FeedSamples").Range("AA").End(xlDown).Offset(1, 0).value = Range("A19:M19").value
    'Sample Taken From

    'Sample Method

    'Form

    'Probe Size

    'Product No./Class No.
    Worksheets("FeedSamples").Range("D").End(xlDown).Offset(1, 0).value = Range("A23:C23").value

    modeAdd = False
End If

If modeEdit = True Then
    'find the record in "datatable" and save over fields.
    Dim result As Variant
    Dim sheet As Worksheet
    Set sheet = ActiveWorkbook.Sheets("FeedSamples")

    'Range("O3").Formula = "=IF(ISERROR(MATCH(Range("A5:B5").Value, sheet.Range("B:B"), 0)), "Not Found", "Value found on row " & MATCH(Range("A5:B5").Value, sheet.Range("B:B"), 0))"
    'Range("O3").Formula = "=IF(ISERROR(MATCH(12345,A:A,0)),"Not Found","Value found on row " & MATCH(12345,A:A,0)))"

    result = Application.WorksheetFunction.VLookup(Range("A5:B5").value, sheet.Range("B2:B25000"), 2, False)
    'Throws Object Required Error
    MsgBox result
    modeEdit = False
    allowNav = True

End If


End Sub

1 个答案:

答案 0 :(得分:8)

一种非常简单的方法是声明要搜索的范围和要查找的值。

Sub findValue()

    Dim xlRange As Range
    Dim xlCell As Range
    Dim xlSheet As Worksheet
    Dim valueToFind

    valueToFind = "MyValue"
    Set xlSheet = ActiveWorkbook.Worksheets("Sheet2")
    Set xlRange = xlSheet.Range("B1:B10")

    For Each xlCell In xlRange
        If xlCell.Value = valueToFind Then
            'Do Something
        End If
    Next xlCell

End Sub

我假设您的范围(“A5:B5”)是合并的单元格,因为您指出它包含单个值。合并的单元格可以由合并中的“左上角”单元格引用(或者至少是我如何看待它)。所以你的合并范围(“A5:B5”)可以简称为“(A5”)。无论如何,这里是上述方法的修改版本,更适合您的需要。

Sub findValue(ByVal valueToFind As String)

    Dim xlRange As Range
    Dim xlCell As Range
    Dim xlFormSheet As Worksheet
    Dim xlSamplesSheet As Worksheet
    Dim iLastRow As Integer
    Dim iRow As Integer
    Dim bFound As Boolean

    bFound = False
    Set xlFormSheet = ActiveWorkbook.Worksheets("FeedSampleForm")
    Set xlSamplesSheet = ActiveWorkbook.Worksheets("FeedSamples")

    iLastRow = xlSamplesSheet.Range("B1").End(xlDown).Row

    Set xlRange = xlsamplesheet.Range("B1:B" & iLastRow)

    For Each xlCell In xlRange
        If xlCell.value = valueToFind Then
            bFound = True '<-- The value was found
            iRow = xlCell.Row '<-- Here is the row that the value was found on
        End If

        If bFound Then Exit For '<-- Optional: Exit the for loop once the value is found the first time
    Next xlCell

End Sub