如果单元格包含文本,则插入HLOOKUP公式

时间:2018-06-11 07:21:49

标签: excel vba excel-vba

这就是我的电子表格的样子:

enter image description here

如果单元格包含58DV,我想在58DV的右边单元格中插入HLOOKUP公式。如果没有数据,则无需进行任何操作。我还是VBA的新手,所以我不知道如何使用VBA中的公式。感谢

Sub sitelookup()

With Application
    .ScreenUpdating = False
End With

Dim SrchRng As Range, cel As Range

Set SrchRng = Range("C4:C1299")

For Each cel In SrchRng
    If cel > 0 Then
        cel.Offset(0,1).value = Application.WorksheetFunction.HLOOKUP(F4,'Raw G'!2:5,2,0)

    End If
Next cel

End Sub

1 个答案:

答案 0 :(得分:0)

尝试,

with worksheets("sheet1")
    Set SrchRng = .Range(.cells(4, "B"), .cells(rows.count, "B").end(xlup))

    For Each cel In SrchRng
        If cel.value2 = "58DV" Then
           'to put the formula's value into the neighboring cell
            cel.Offset(0, 1).value = _
               Application.HLOOKUP(.cells(cel.row, "F"), worksheets("Raw G").range("2:5"), 2, 0)
           'to put the formula into the neighboring cell
            'cel.Offset(0, 1).formula = _
               "=HLOOKUP(F" & cel.row & ",'Raw G'!2:5, 2, 0)"
        End If
    Next cel
end with