【问题标题】:Search one column for string and add values to four different cells in same row在一列中搜索字符串并将值添加到同一行中的四个不同单元格
【发布时间】:2015-11-17 05:19:18
【问题描述】:

我在 Excel VBA 中编码时遇到问题。我想在所有列 f 上运行一个查询以获取一个字符串“mug”,然后我想将值添加到列 H、I、J、K。

我知道这将需要一个 for 循环来遍历所有列 f 以查询“mug”。之后我不确定如何将值添加到三个不同的列。

伪编码:

If column f = "mug", then write 5 to column g, 10 to column h, 15 column i

【问题讨论】:

  • 既然可以使用公式,为什么还要使用 VBA?
  • =IFERROR(IF(FIND or SEARCH("mug",f1),"***5/10/15****"),"")

标签: vba excel find


【解决方案1】:
Sub test_wordisbarn()
Dim FirstAddress As String, cF As Range

ActiveSheet.Range("F1").Activate
With ActiveSheet.Range("F:F")
    'First, define properly the Find method
    Set cF = .Find(What:="mug", _
                After:=ActiveCell, _
                LookIn:=xlValues, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)

    'If there is a result, keep looking with FindNext method
    If Not cF Is Nothing Then
        FirstAddress = cF.Address
        Do
            'Add your value in other columns
            cF.Offset(0, 1).Value = 5
            cF.Offset(0, 2).Value = 10
            cF.Offset(0, 3).Value = 15

            Set cF = .FindNext(cF)
        'Look until you find again the first result
        Loop While Not cF Is Nothing And cF.Address <> FirstAddress
    End If
End With

End Sub

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多