在列中找到单元格值

发布于 2025-02-13 22:08:34 字数 1261 浏览 0 评论 0原文

单元格A3具有左公式,该公式可提取另一个单元格的前3个字符。 需要VBA来搜索A列中A3公式的结果,然后选择该单元格。

获取编译错误:

“ strsearch =”的对象在代码下方的3D行中。

    Sub findAA()
    
        Dim rng1 As Range
        Dim strSearch As String
        Set strSearch = Range(A3).Value
        Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlWhole)
        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
    
    End Sub

更新:避免范围问题(请参阅评论),我将A3移至B4。宏在运行时运行时工作,但使用Active X Control的组合框时不起作用,该组合框会更改现在的B4值。这个想法是,当组合框更改时,它会触发B4的变化,这应该触发代码以在A列中选择所得的单元格。代码现在看起来像这样:

        Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "B4" Then
            Dim rng1 As Range
            Dim strSearch As String
             strSearch = Range("b4").Value
            Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
                If Not rng1 Is Nothing Then
                    rng1.Select
                End If
        End If
    End Sub

Cell A3 has a LEFT formula that extracts first 3 characters of another cell with a dropdown.
Need VBA to search the result of A3 formula in column A, and select that cell.

Getting a Compile error:

Object required on "strSearch =" in 3d line of below code.

    Sub findAA()
    
        Dim rng1 As Range
        Dim strSearch As String
        Set strSearch = Range(A3).Value
        Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlWhole)
        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
    
    End Sub

UPDATE: to avoid the range issue (see comments), I moved A3 to B4. Macro worked when run by itself, but does not work when using the Active X Control's Combo Box, which changes the value of now B4. The idea is that when Combo Box changes, it triggers change in B4, which should trigger the code to select the resulting cell in column A. Code now looks like this:

        Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "B4" Then
            Dim rng1 As Range
            Dim strSearch As String
             strSearch = Range("b4").Value
            Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
                If Not rng1 Is Nothing Then
                    rng1.Select
                End If
        End If
    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半岛未凉 2025-02-20 22:08:35

这是现在对我有用的方法 - 感谢@bigben:

此代码在Combobox更改时称呼宏:

Private Sub ComboBox1_Change()
Call findAA
End Sub

这是工作的查找代码:

Sub findAA()

Dim rng1 As Range
Dim strSearch As String
 strSearch = Range("b4").Value
Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
If Not rng1 Is Nothing Then
rng1.Select
End If
End Sub

Here's what works for me now - Thanks, in part, to @BigBen:

This code calls the find macro upon change in ComboBox:

Private Sub ComboBox1_Change()
Call findAA
End Sub

This is the working find code:

Sub findAA()

Dim rng1 As Range
Dim strSearch As String
 strSearch = Range("b4").Value
Set rng1 = Range("A:A").Find(strSearch, , xlValues, xlPart)
If Not rng1 Is Nothing Then
rng1.Select
End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文