[英]Excel VBA Find Function - Not working with formula cells
Unable to get FIND to work with a formula cell containing text and locate the result on a sheet within a range. 无法使FIND与包含文本的公式单元格一起使用,并将结果定位在范围内的工作表上。
I have created a button that when clicked should look at a cell, take the data and then search for the result in a column on a different sheet using exact match. 我创建了一个按钮,单击该按钮时应查看单元格,获取数据,然后使用完全匹配在不同工作表的列中搜索结果。
D11 holds a formula that returns a vlookup text result When i press the button i do the following D11包含一个返回vlookup文本结果的公式当我按下按钮时,我执行以下操作
Sub Button3_Click()
Dim strSearch As String
Dim rng As Range
Dim rng1 As Range
Dim ws As Worksheet
Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Set ws = wb.Worksheets("Auspost_Data")
strSearch = Worksheets("Report_Tool").Range("D11")
wb.ws.Range("D:D").Select
Selection.Find(strSearch)
End Sub
I expect the find to take the text value and find the exact match within the column range. 我希望find能够获取文本值并在列范围内找到完全匹配。
Try This: 尝试这个:
Sub Button3_Click()
Dim strSearch As String
Dim rng As Range
Dim rng1 As Range
Dim ws As Worksheet
Dim wb As Workbook
Dim fnd As Range
Set wb = Application.ActiveWorkbook
Set ws = wb.Worksheets("Auspost_Data")
strSearch = Worksheets("Report_Tool").Range("D11")
Set fnd = ws.Range("D:D").Find(strSearch)
MsgBox fnd.Address
End Sub
Please try something like this... 请尝试这样的事情......
Sub Button3_Click()
Dim ws As Worksheet, wsCriteria As Worksheet
Dim strSearch As String
Dim rng As Range
If SheetExists("Auspost_Data") Then
Set ws = Worksheets("Auspost_Data")
Else
MsgBox "There is no Sheet called " & ws.Name & " in the ActiveWorkbook.", vbCritical, "Sheet Not Found!"
Exit Sub
End If
If SheetExists("Report_Tool") Then
strSearch = Worksheets("Report_Tool").Range("D11")
Else
MsgBox "There is no Sheet called Report_Tool in the ActiveWorkbook.", vbCritical, "Sheet Not Found!"
Exit Sub
End If
If strSearch = "" Then
MsgBox "Search string is empty, there is nothing to find.", vbExclamation, "Empty Search String!"
Exit Sub
End If
Set rng = ws.Range("D:D").Find(what:=strSearch, LookIn:=xlValues, lookat:=xlWhole)
If Not rng Is Nothing Then
MsgBox "The search string " & strSearch & " is found in the cell " & rng.Address(0, 0) & ".", vbInformation, strSearch & " Found!"
Else
MsgBox "The search string " & strSearch & " was not found.", vbExclamation, strSearch & " Found!"
End If
End Sub
Function SheetExists(shName As String) As Boolean
Dim sh As Worksheet
On Error Resume Next
Set sh = Worksheets(shName)
On Error GoTo 0
If Not sh Is Nothing Then SheetExists = True
End Function
To select the range if it is found: 如果找到,则选择范围:
Replace the last IF statement with this... 用这个替换最后的IF语句......
If Not rng Is Nothing Then
ws.Select
rng.Select
Else
MsgBox "The search string " & strSearch & " was not found.", vbExclamation, strSearch & " Found!"
End If
Notice that I have removed the MsgBox if the rng is found which I think is not needed here specially when the code selects the rng found. 请注意,如果找到rng,我已经删除了MsgBox,我认为这里不需要特别是当代码选择找到的rng时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.