簡體   English   中英

搜索一列字符串並將值添加到同一行的四個不同單元格

[英]Search one column for string and add values to four different cells in same row

我在 Excel VBA 中編碼時遇到問題。 我想在所有列 f 上運行一個“杯子”字符串的查詢,然后我想向 H、I、J、K 列添加值。

例子

我知道它將需要一個 for 循環來遍歷所有列 f 以查詢“杯子”。 之后我不確定如何將值添加到三個不同的列。

偽代碼:

If column f = "mug", then write 5 to column g, 10 to column h, 15 column i
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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM