此代码来自我帮助过的另一篇文章...
1)Original
2)Second edit
我发现的一个新问题是,由于数字格式不同,.FIND在搜索9100时找不到9,100。数字与一般。
我尝试了一些破坏代码的东西。看似简单,但我没有在网上找到答案。
1)如何在格式中找到相同的数字?
代码的作用摘要:
当通过输入" y"触发时在col G中,代码将检索col d或E中同一行的值。然后,它将搜索col K的相同值,如果找到,则使该单元格成为活动单元格。
这是基本的工作代码。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rgG As Range
Dim rgK As Range
Dim cSearch As Range
Dim lLoop As Long
Dim cResult As Range
Dim rgXsct As Range
'Find the last used rows in Cols G & K
Dim lrowG As Long
Dim lrowK As Long
lrowG = Columns("G:G").Find(What:="*", _
After:=Range("G1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Debug.Print lrowG
lrowK = Columns("K:K").Find(What:="*", _
After:=Range("K1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
'Debug.Print lrowK
Set rgG = Range("g5:g" & lrowG)
Set rgK = Range("k6:k" & lrowK)
Set rgXsct = Intersect(Target, rgG) '"Intersect" will ensure the current cell lies on the correct column.
'*********** IF CONDITIONS to run. ****************
'1) Columns.count & Row.count = 1 confirms only one cell selected (not a range of cells). -- I think
'2) LCase(Target.Text) = "y" makes the Target lowcase and compares it.
'3) Intersect makes sure the Target is in the relevant range.
'4)
If Target.Columns.Count = 1 And Target.Rows.Count = 1 And _
LCase(Target.Text) = "y" And _
Not rgXsct Is Nothing Then
Application.ScreenUpdating = False
Set cSearch = Target.Offset(0, -3) 'Returns value in Col D
If cSearch = 0 Or cSearch = "" Then Set cSearch = Target.Offset(0, -2) 'If nothing in D, check E.
If cSearch = 0 Or cSearch = "" Then GoTo end_search 'If nothing in E, end.
'Debug.Print "Target "; Target
'Debug.Print "cSearch.value "; cSearch.Value
'This will loop through the column to search, to find the value identified above, and return its cell address.
With rgK
Set cResult = .Cells(1, 1)
'For lLoop = 1 To WorksheetFunction.CountIf(.Cells, cSearch.Value)
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
'Debug.Print "cResult " & cResult
If Not cResult Is Nothing Then
MsgBox "val: " & cSearch.Value & " Matching Cell: " & cResult.Address
'Use the cell address identified above to move the active cell to that address.
Cells(Range(cResult.Address).Row, Range(cResult.Address).Column).Select 'Have to convert the address to row/column to use in Cell.Select.
Else
MsgBox (cSearch.Value & " not found.")
GoTo end_search
End If
'Next lLoop
End With
End If
end_search:
'Application.ScreenUpdating = True
'End Sub
2)你能告诉我在For
之后注释掉with
循环的价值吗?它被建议作为使整个代码工作的解决方案,但它没有它。
3)我怎样才能使这更简洁?这似乎很复杂。例如,为什么我需要一个with
语句,如果Columns(K:K).Find(...
没有with
语句应该根据我读过的内容工作(但在本单元中没有)。
答案 0 :(得分:0)
将以下内容更改为LookIn:= xlFormulas。
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlValues, _
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlFormulas, _
当我这样做时,它对我来说很好。
答案 1 :(得分:0)
或者使用 MATCH
代替 FIND。