首页 > 解决方案 > 搜索整列中的行

问题描述

我正在编写一个简单的 Excel VBA 程序来搜索整个客户端数据库,寻找特定的记录。在执行此操作时,我遇到了一个问题 - 在遇到第一场比赛后,它会很好地执行指令并停止。

该数据库由 500 多行组成,如下所示:

A 列 B 列 C 列 D 列

姓名 xxxx yyy zzzz

这是一些简化的代码

Sub Analizuj_1_Click()

Dim SearchName As String
Dim CColumn As Integer
Dim Match As Boolean
Dim CRow As Integer
Dim CRowPaste As Integer

On Error GoTo Err_Execute


LDate = Range("NazwaKlienta").Value
Sheets("2019").Select

'Starting in Column A, Row 2'
LColumn = 1
LRow = 2
LRowPaste = 2

LFound = False
While LFound = False
  'Found a blank cell -> terminate'
  If Len(Cells(CRow, 1)) = 0 Then
     MsgBox "Klient nie ma zaległości"
     Exit Sub
  'Found Match
Szukaj:   ElseIf Cells(CRow, 1) = SearchName Then
     Cells(CRow, 1).EntireRow.Select
     Selection.Copy
     Sheets("test").Select
     Cells(CRowPaste, 1).Select
     Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
     False, Transpose:=False
     LFound = True
     CRowPaste = CRowPaste + 1
     Sheets("2019").Select
     'Continuation"
     ElseIf Cells(CRow, 1).Value > 0 Then
        CRow = CRow + 1
        GoTo Szukaj
     End If

   Wend

   Exit Sub

Err_Execute:
  MsgBox "Blad."

End Sub

即使我尝试通过 Start 语句继续搜索,它也会在第一个找到的匹配项处停止。我尝试尝试其他方法,但仍然是同样的问题。

Inb4 我知道,选择不是最有效的方法

标签: excelvbasearchrows

解决方案


推荐阅读