在工作表中查找数组数据 – 没有选取某些值

我一直在改变“偏移”中的数字,试图将名字和数字移到我想要的位置,但是David和Andrea的电话号码不会转移。

Private Sub CommandButton1_Click() Dim ws As Worksheet, bFound As Boolean, rFound As Range Dim a As Long, aNames As Variant aNames = Array("David", "Andrea", "Caroline") With Worksheets("Sheet1").Range("A1:E30").Cells For a = LBound(aNames) To UBound(aNames) Set rFound = .Find(What:=aNames(a), MatchCase:=False, LookAt:=xlWhole, SearchFormat:=False) If Not rFound Is Nothing Then bFound = True Worksheets("Report").Cells(Worksheets("Report").Rows.Count, 5).End(xlUp).Offset(3) = rFound.Value Worksheets("Report").Cells(Worksheets("Report").Rows.Count, 6).End(xlUp).Offset(3, 1) = rFound.Offset(, 1).Value End If Next a End With End If Not bFound Then MsgBox "None of the sheets contains the names " & Chr(10) & _ "'" & Join(aNames, "', '") & "' in cells A1:E30.", vbInformation, "Not Found" End If End Sub 

Worksheets("Report").Cells(Worksheets("Report").Rows.Count, 5).End(xlUp).Offset(3) = rFound.Value语句正常工作,如果你的目标是每个发现的名字“报告”表第4行从列E开始的第一个空单元格在最后一个空单元之后

Worksheets("Report").Cells(Worksheets("Report").Rows.Count, 6).End(xlUp).Offset(3, 1) ...语句总是返回相同的单元格地址,因为它总是findF列第一个空的单元格后,最后不是空的一个,你不写任何“新”的价值沿该列

此外, End陈述是可以避免的,因为它会带来意想不到的行为

最后,从一个纯粹的代码逻辑的angular度来看,我把Worksheets("Report")放在With语句而不是Worksheets("Sheet1") ,这样就不必多次访问(重复)每一个循环,而后者只能在一个循环中访问一次,我最终将后者设置为一个范围variables

对于所有以上我会代码如下:

 Private Sub CommandButton1_Click() Dim ws As Worksheet Dim bFound As Boolean Dim rFound As Range, rangeToBeSaearchedInRng As Range Dim a As Long, aNames As Variant aNames = Array("David", "Andrea", "Caroline") Set rangeToBeSaearchedInRng = Worksheets("Sheet1").Range("A1:E30") '<--| set your range to be searched in and exploit it inside the loop With Worksheets("Report") '<--| reference "Report" worksheet For a = LBound(aNames) To UBound(aNames) Set rFound = rangeToBeSaearchedInRng.Find(What:=aNames(a), MatchCase:=False, LookAt:=xlWhole, SearchFormat:=False) If Not rFound Is Nothing Then bFound = True With .Cells(.Rows.Count, 5).End(xlUp).Offset(3) '<--| reference referenced worksheet column E first empty cell after last not empty one .Value = rFound.Value '<--| set referenced cell value .Offset(, 1).Value = rFound.Offset(, 1).Value '<--| set the cell value 1 column to the right of referenced cell End With End If Next a End With If Not bFound Then MsgBox "None of the sheets contains the names " & Chr(10) & _ "'" & Join(aNames, "', '") & "' in cells A1:E30.", vbInformation, "Not Found" End If End Sub 

End(xlUp)函数有一些问题,无论是逻辑上还是系统上(也就是说,Excel可能无法像更改那样快地计算最后一行的更改)。 如果你find了这个问题的答案,你仍然会遇到这样的问题:你可能会把电话号码写在与姓名不同的行中。 所以为什么试试? 简化(意思是澄清)代码更好。 例如,

  If Not rFound Is Nothing Then bFound = True With Worksheets("Report") R = .Cells(.Rows.Count, 5).End(xlUp).Row + 3 .Cells(R, 5).Value = rFound.Value .Cells(R, 6).Value = rFound.Offset(0, 1).Value End With End If 

有一个更可读的代码,顺带一提,还透露,你的设置bFound是错误的,因为它没有设置为False任何地方。 在上面引用的代码的第一行之前,我build议进行以下修改。

  Set rFound = .Find(What:=aNames(a), MatchCase:=False, LookAt:=xlWhole, SearchFormat:=False) bFound = (Not rFound Is Nothing) If bFound Then