2014-06-12 104 views
0

我有一系列的值在一张纸上Excel的VBA .Find()行为异常无法找到字符串

E11: “名义”

E12: “罢工”

E13: “优惠券”

等等

在我的代码,使用工作表名称

Function AddInput(name As String, strInput As String, Optional suffix = "") As String 
Dim inputVal As Variant 
On Error GoTo ERROR_FUNCTION 
    With Worksheets(name) 
     If .Cells.Find(what:=strInput, LookAt:=xlWhole,searchorder:=xlByRows).Offset(0, 1).Value <> "" Then 
      inputVal = Trim(Cells.Find(what:=strInput, LookAt:=xlWhole,  searchorder:=xlByRows).Offset(0, 1).Value) 
      If TypeName(inputVal) = "Date" Then inputVal = Format(inputVal, "YYYYMMDD") 
     AddInput = Replace(strInput, " ", "") & "=" & inputVal & "&" 
      If suffix <> "" Then AddInput = AddInput & suffix 
     Else 
      AddInput = "" 
     End If 
    End With 

Exit Function 
ERROR_FUNCTION: 
    Debug.Print strInput & ": input not found" 
    MsgBox strInput & ": input not found" 
End Function 

我能够找到什么在单元格E12,但不是E11。
我做了以下几点:

1)我将单元格值直接复制到搜索功能(没有机会胖手指它)。

2)我将E11中的值复制到1(如果由于某种原因,它找不到该范围等等......它只是返回E12)。

我仍然找不到那个单元格,它适用于我通过它的每个其他值。

有没有人遇到过这个问题,你是如何解决它的?

谢谢!

+0

注意,仅查找发现在可见单元格的事情。 – Siphor

+0

这是一个可见的单元格。 – Kelvin

+0

@Kelvin你发布的代码片段确实没有做任何事情。你的第二行不完整。再加上你的代码不起作用,我会说我们没有足够的信息来说明原因,但这可能是由于你尚未发布的代码存在问题。 –

回答

0

确保您正常启动每个搜索:

Sub dural() 
    Dim r As Range 
    strSheetname = ActiveSheet.Name 
    MyInputString = Application.InputBox(Prompt:="Enter value", Type:=2) 
    With Sheets(strSheetname) 
     Set r = .Cells.Find(what:=MyInputString, After:=Range("A1")) 
    End With 
    MsgBox r.Address(0, 0) 
End Sub