使用在 Excel VBA 中查找的连续循环 [英] continuous loop using Find in Excel VBA
问题描述
我有以下代码,但遇到了问题:
I have the below code, which I am having trouble with:
Sub getAccNos()
Dim oNameRange As Range
Dim oFindRng As Range
Dim sName As String
Dim sAccNo As String
Set oNameRange = Workbooks("New Name Work.xls").Worksheets("Manual").Range("B4")
Do While Not oNameRange.Text = ""
sName = Trim(oNameRange.Text)
Workbooks("New Name Work.xls").Worksheets("sheet1").Select
Set oFindRng = Cells.Find(What:=sName, After:=activecell)
Do While Not oFindRng Is Nothing
oNameRange.Offset(0, -1).Value = oFindRng.Offset(0, 1).Text
oFindRng.Offset(1, 0).Activate
Set oFindRng = Cells.Find(What:=sName, After:=activecell)
Loop
Set oNameRange = oNameRange.Offset(1, 0)
Loop
End Sub
基本上,在工作表 sheet1 上,我有一个带有帐号的姓名列表,并且可以有多个帐号具有相同的名称.在名为手册的目标表上,我有姓名....但缺少帐号,我想获取它们.
Basically, on worksheet sheet1 I have a list of names with account number, and there can be several account numbers with the same name. On my target sheet, called Manual, I have the names .... but the account numbers are missing and I would like to get them.
我无法使用 VLOOKUP,因为有几个名称相同,我需要获取所有帐号的列表.我该怎么做?
I cannot use VLOOKUP because there are several names that are the same and I need to get a list of all the account numbers. How can I do this?
我尝试在 VBA 中使用 FIND 编写上面的代码,不幸的是,我错过了一些基本的东西,因为一旦在内部 Do Loop 它只是在它应该退出时连续循环(至于第一个只有一次发生)
I tried to write the above code using FIND in VBA, unfortunately, I am missing something elementary as once in the inside Do Loop it just loops continuously when it should be stepping out (as for the first one there is only one occurrance)
谢谢你告诉我我做错了什么,或者公式会更好?
thanks for showing me what I am doing wrong, or maybe a formula would be better?
推荐答案
这是一个简单的代码,它不会循环遍历 Sheet1 单元格来查找匹配项.它使用 .FIND
和 .FINDNEXT
.详情请此处.
Here is a simple code which doesn't loop through Sheet1 cells to find a match. It uses .FIND
and .FINDNEXT
. More about it HERE.
将此代码放在一个模块中并简单地运行它.此代码基于您的示例文件.
Place this code in a module and simply run it. This code is based on your sample file.
Sub Sample()
Dim wsI As Worksheet, wsO As Worksheet
Dim lRow As Long, i As Long
Dim sAcNo As String
Dim aCell As Range, bCell As Range
'~~> This is the sheet which has account numbers
Set wsI = ThisWorkbook.Sheets("Sheet1")
'~~> This is the sheet where we need to populate the account numbers
Set wsO = ThisWorkbook.Sheets("Sheet2")
With wsO
lRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("A1:A" & lRow).NumberFormat = "@"
For i = 2 To lRow
Set aCell = wsI.Columns(2).Find(What:=.Range("B" & i).Value, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
sAcNo = sAcNo & "," & aCell.Offset(, -1).Value
Do
Set aCell = wsI.Columns(2).FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
sAcNo = sAcNo & "," & aCell.Offset(, -1).Value
Else
Exit Do
End If
Loop
End If
If sAcNo <> "" Then
.Range("A" & i).Value = Mid(sAcNo, 2)
sAcNo = ""
End If
Next i
End With
End Sub
屏幕截图
希望这是你想要的吗?
这篇关于使用在 Excel VBA 中查找的连续循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!