我有一个与 Excel 工作表上的按钮单击事件相关联的宏。当此事件触发时,我需要查看我的工作表上的值是否存在于我的其他工作表FeedSampleForm
Range("A5:B5").Value
列中的任何位置。B
FeedSamples
任何人都可以帮助我吗?当谈到 Excel 时,我几乎不是用户,这是我第一次使用它“开发”。
编辑:
当前代码如下。这适用于不同的保存过程,具体取决于是保存新记录还是在编辑先前创建的记录后保存。
对于示例数据,假设我正在寻找“FeedSampleForm”.Range(“A5:B5”).Value 即“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