EXCEL VBA - 如何使用 .FIND 查找不同格式的相同数字
EXCEL VBA - How to find the same number, in different formats, using .FIND
此代码来自另一个 post 我曾帮助过...
1)
2) Second edit
我发现的一个新问题是 .FIND 在搜索 9100 时找不到 9,100,这是由于不同的数字格式,例如数字与一般。
我尝试了几件事,破坏了代码。看起来很简单,但我在网上找不到答案。
1) 如何让它在一种格式中找到相同的数字?
代码的作用总结:
当通过在 col G 中输入 "y" 触发时,代码将检索 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) 你能告诉我 with
之后注释掉的 For
循环的值吗?有人建议将其作为使整个代码正常工作的解决方案,但没有它也能正常工作。
3) 我怎样才能让它更简洁?看起来很复杂。例如,为什么我需要一个 with
语句,而没有 with
语句的 Columns(K:K).Find(...
应该根据我读过的内容工作(但它不在这个模块中)。
将以下内容更改为 LookIn:=xlFormulas per below。
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlValues, _
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlFormulas, _
当我这样做时,对我来说效果很好。
或者使用 MATCH
而不是 FIND。
此代码来自另一个 post 我曾帮助过...
1)
2) Second edit
我发现的一个新问题是 .FIND 在搜索 9100 时找不到 9,100,这是由于不同的数字格式,例如数字与一般。
我尝试了几件事,破坏了代码。看起来很简单,但我在网上找不到答案。
1) 如何让它在一种格式中找到相同的数字?
代码的作用总结:
当通过在 col G 中输入 "y" 触发时,代码将检索 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) 你能告诉我 with
之后注释掉的 For
循环的值吗?有人建议将其作为使整个代码正常工作的解决方案,但没有它也能正常工作。
3) 我怎样才能让它更简洁?看起来很复杂。例如,为什么我需要一个 with
语句,而没有 with
语句的 Columns(K:K).Find(...
应该根据我读过的内容工作(但它不在这个模块中)。
将以下内容更改为 LookIn:=xlFormulas per below。
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlValues, _
Set cResult = .Find(What:=cSearch.Value, _
After:=cResult, _
LookIn:=xlFormulas, _
当我这样做时,对我来说效果很好。
或者使用 MATCH
而不是 FIND。