我写了下面的代码来获取行号。从range(“ C:C”)中,我找到了与cell(3,3)中的值/文本匹配的值。只要cell(3,3)具有纯数字,该代码就可以正常工作。或纯文本。但是,一旦cell(3,3)具有特殊字符,例如〜 (如MBGH3345〜123),代码就会返回错误。当我运行此代码时,它将返回运行时错误400。请帮助解决此问题。
Public Sub Find_Row_Number()
Dim wb As Workbook
Dim ws As Worksheet
Dim FindCell As Range
Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set FindCell = ws.Range("C:C").Find(cells(3,3))
'value to be searched is in cells(3,3)
If Not FindCell Is Nothing Then
Msgbox (FindCell.Row)
Else
Msgbox ("Error")
End If
End Sub
答案 0 :(得分:1)
我看不到如何给您带来运行时错误,但是:
波浪号~
用于转义通配符,例如?
和*
,因此,如果要匹配 actual 波浪号,则需要使用...另一个波浪号逃避搜索值中的波浪号。
例如,C3中的MBGH3345~~123
将与MBGH3345~123
相匹配。
如果您的搜索词可能包含波浪号字符(并且不打算将其用作通配符转义符),则可以执行以下操作:
Set FindCell = ws.Range("B:B").Find(Replace(Cells(3, 3).Value, "~", "~~"), _
LookIn:=xlValues, lookat:=xlWhole)
编辑-这是我要做的事情:Match
比这里的Find
-
Sub CheckData()
Dim ws As Worksheet, c As Range, v, m
Set ws = ActiveSheet
For Each c In ws.Range(ws.Range("L3"), ws.Cells(Rows.Count, "L").End(xlUp)).Cells
v = c.Value
If Len(v) > 0 Then
v = Replace(v, "~", "~~") 'escape any tilde
m = Application.Match(v, ws.Range("C:C"), 0)
If IsError(m) Then
c.Offset(0, -1).Value = "New"
'add this to the master list...
ws.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0).Value = c.Value
Else
c.Offset(0, -1).Value = "Line " & m
End If
End If
Next c
End Sub