Vba Excel宏,用于搜索列中的值,并从消息框中的另一列返回同一行中的值
我正在尝试编写一个宏,该宏将提示用户输入值并执行以下操作:Vba Excel宏,用于搜索列中的值,并从消息框中的另一列返回同一行中的值,vba,excel,find,Vba,Excel,Find,我正在尝试编写一个宏,该宏将提示用户输入值并执行以下操作: - Search for the value in column B and select the first cell where the value is found - Return the correspondong value in column L and M of the selected cell's row within a message box - Then once the user hits "ok", the
- Search for the value in column B and select the first cell where the value is found
- Return the correspondong value in column L and M of the selected cell's row within a message box
- Then once the user hits "ok", the macro will find and select the next cell in column B with the search criteria, and repeat the above steps
- Once all of the cells with the search criteria in column B have been searched and found, a message box will communicate that all matches have been found and close loop
下面是我开始使用的代码,作为VB的初学者,我不明白为什么我的循环不能正常工作。。。请帮忙
Sub Macro1()
Dim response As String, FndRow As Long, NoMatch As Boolean, LastRow As Long
response = InputBox("Please enter the Column Name to find matching Source File Field Name.")
If response = "" Then Exit Sub
On Error Resume Next
Range("B5").Select
NoMatch = False
LastRow = 0
Do Until NoMatch = True
FndRow = Cells.Find(What:=response, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
If FndRow = 0 Then
MsgBox response & " could not be found."
NoMatch = True
ElseIf FndRow < LastRow Then
MsgBox "All " & response & " matches have been found."
NoMatch = True
Else
Range("B" & FndRow).Select
MsgBox "Source File Name: " & Range("L" & FndRow).Value & vbNewLine & "File Column Name: " & Range("M" & FndRow).Value
LastRow = FndRow
End If
Loop
End Sub
Sub宏1()
Dim响应为字符串,FndRow为长,NoMatch为布尔值,LastRow为长
response=InputBox(“请输入列名以查找匹配的源文件字段名。”)
如果响应=”,则退出子系统
出错时继续下一步
范围(“B5”)。选择
NoMatch=False
LastRow=0
直到NoMatch=True为止
FndRow=Cells.Find(What:=response,After:=ActiveCell,LookIn:=xlFormulas_
查看:=xlPart,搜索顺序:=xlByRows,搜索方向:=xlNext_
MatchCase:=False,SearchFormat:=False)。行
如果FndRow=0,则
找不到MsgBox响应&”
NoMatch=True
ElseIf FndRow<最后一行
MsgBox“全部”&响应&“已找到匹配项。”
NoMatch=True
其他的
范围(“B”和FndRow)。选择
MsgBox“源文件名:”&Range(“L”)和FndRow.Value&vbNewLine&“文件列名:”&Range(“M”)和FndRow.Value
LastRow=FndRow
如果结束
环
端接头
您的发现异常,因为您正在“水平”查找匹配项。您需要使用SearchOrder:=xlByColumns
FndRow = Cells.Find(What:=response, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
我将使用过滤器而不是查找循环:
Sub tgr()
Dim rngVis As Range
Dim VisCell As Range
Dim sFind As String
sFind = InputBox("Please enter the Column Name to find matching Source File Field Name.")
If Len(Trim(sFind)) = 0 Then Exit Sub 'Pressed cancel
Application.ScreenUpdating = False
With Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns("B"))
.AutoFilter 1, sFind
On Error Resume Next
Set rngVis = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilter
End With
Application.ScreenUpdating = True
If rngVis Is Nothing Then
MsgBox sFind & " could not be found."
Else
For Each VisCell In rngVis.Cells
MsgBox "Source File Name: " & VisCell.Worksheet.Cells(VisCell.Row, "L").Text & vbNewLine & _
"File Column Name: " & VisCell.Worksheet.Cells(VisCell.Row, "M").Text
Next VisCell
End If
End Sub