我想搜索数据目录(包含在“目录”中,并且如果该数据与“ D3”单元格中的项目匹配,我想将值“ Mobile Phone”输出到“ Sheet1”中的“ H3”单元格中。
这就是我正在使用的东西:
Sub VBAScanner()
Dim productname As String
Dim finalrow As Integer
Dim i As Integer
finalrow = Sheets("Catalogue").Range("J3000").End(x1UP).Row
productname = Sheets("Sheet1").Range("D3").Value
For i = 5 To finalrow
If Sheets("Catalogue").Cells(i, 5) = productname Then
Sheets("Sheet1").Cell("H3").Value = "Mobile Phone"
End If
Next i
End Sub
谢谢
答案 0 :(得分:0)
您可以为此使用“查找”,也可以为xlUp
进行更正。另外,添加一些未找到的基本测试,最后一行小于5,或者productname
为空白。
Option Explicit
Public Sub VBAScanner()
Dim productname As String, finalRow As Long, found As Range, i As Long
productname = ThisWorkbook.Worksheets("Sheet1").Range("D3").Value
If productname = vbNullString Then Exit Sub
With ThisWorkbook.Worksheets("Catalogue")
finalRow = .Range("J3000").End(xlUp).Row
If finalRow < 5 Then Exit Sub
Set found = .Range("J5:J" & finalRow).Find(productname)
End With
If Not found Is Nothing Then
ThisWorkbook.Worksheets("Sheet1").Range("H3").Value = "Mobile Phone"
End If
End Sub