在列中找到单元格值
单元格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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是现在对我有用的方法 - 感谢@bigben:
此代码在Combobox更改时称呼宏:
这是工作的查找代码:
Here's what works for me now - Thanks, in part, to @BigBen:
This code calls the find macro upon change in ComboBox:
This is the working find code: