如果满足条件,查找文本并显示值

时间:2017-12-12 17:05:49

标签: excel vba excel-vba

有人可以帮助我一点点。图片已附上。 我需要的是一个能够找到" TOTAL PURCHASE"有抵消(-9)=" USD"然后在单元格D1中显示值100。

这就是我所拥有的

Sub vbavjezba()
    Dim total As Range
    Dim rng As Range
    Dim cell As Object
    Dim usd As Range

    Set rng = Application.Range("A1:A21")
    Set total = Worksheets(1).Range("A1:A21").Find(what:="TOTAL PURCHASE")
    Set usd = Worksheets(1).Range("A1:A21").Find(what:="USD")`

    For Each cell In rng
        If total.Offset(-9) = usd Then
            Worksheet(1).Range.("D1").value=total.offset(,2)
        Else
        End If
    Next cell
End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

我有一个答案,会找到USD的第一个实例并检查TOTAL PURCHASE是否在它下方9行,如果找到则将相邻的单元格复制到D1,如果找不到,则搜索下一个USD并重复检查:

Option Explicit

Sub vbavjezba()
Dim total As Variant
Dim usd As Variant
Dim rng As Range

Set rng = Worksheets(1).Range("A1:A21")
Set usd = Worksheets(1).Range("A1:A21").Find(what:="USD") 'Find the first instance of USD

repeatcheck:
If usd.Offset(9, 0).Value = "TOTAL PURCHASE" Then
    Worksheets(1).Range("D1").Value = usd.Offset(9, 1).Value
Else
    Set usd = rng.FindNext(After:=usd) 'Find the next instance of USD
    If Not usd Is Nothing Then 'IF found
        GoTo repeatcheck 'go back to repeatcheck to see if TOTAL PURCHASE is 9 rows below it
    Else
        MsgBox "Not Found!"
    End If
End If
End Sub