【问题标题】:Find all values in range and set another cell.value in the next cell查找范围内的所有值并在下一个单元格中设置另一个 cell.value
【发布时间】:2019-02-08 19:45:17
【问题描述】:

您好,我正在开发一个宏以在一系列字符串中查找 Flex 一词,但我需要设置另一个值,并在其旁边创建一个列。但我不知道如何遍历所有单元格直到最后一行,并为找到的每个值在下一个单元格和列中设置另一个值。

Sub Method2()
Dim rng1 As Range
Dim strSearch As String
strSearch = "Flex"
Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
If Not rng1 Is Nothing Then
    MsgBox "Find has matched " & strSearch & vbNewLine & "corresponding cell is " & rng1.Offset(0, 1)

Else
    MsgBox strSearch & " not found"
End If

结束子

【问题讨论】:

    标签: excel vba loops find


    【解决方案1】:

    如果您的意思是“Flex”可以多次找到,并且您希望每次都出现一个消息框,那么试试这个。如果要更改下一个单元格中的值,只需更改 Do 循环内的行。

    这使用 FindNext 并记录第一次查找的地址并一直循环直到我们回到那里,这意味着该停止了。

    Sub Method2()
    
    Dim rng1 As Range
    Dim strSearch As String, s As String
    
    strSearch = "Flex"
    Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
    
    If Not rng1 Is Nothing Then
        s = rng1.Address
        Do
            rng1.Offset(0, 1).value=1            
            MsgBox "Find has matched " & strSearch & vbNewLine & "corresponding cell is " & rng1.Offset(0, 1)
            Set rng1 = Range("A:A").FindNext(rng1)
        Loop Until rng1.Address = s
    Else
        MsgBox strSearch & " not found"
    End If
    
    End Sub
    

    【讨论】:

    • 感谢您的帮助!您能告诉我如何在下一列中设置值吗?我无法让它工作,我已经尝试使用“For”循环某些值,但我无法在下一列中粘贴任何信息,因为它指出“此对象不支持此属性或方法”
    • 你可以使用rng1.Offset(0,1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多