我有一个VBA宏,它会在一行中查找给定的值,并且一旦找到它就会找到并选择该值(稍后我会做更多的事情,只是在寻找/现在选择)。
问题在于它找不到它应该具有的值。我正在寻找的价值" 7/26/2014",位于Excel电子表格的单元格LS3中。找到它的代码如下:
With Sheets("Rota").Range("LS3")
Set Rng = .Find(What:="7/26/2014", _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.Goto Rng, True
Else
MsgBox "Nothing found"
End If
End With
但是,无论我发现什么都没有找到"没有找到"信息。有人可以帮忙吗?
答案 0 :(得分:1)
以下是搜索特定行中所有单元格的代码。
Sub FindIt()
Dim colNo As Long: colNo = 2 ' hardcoded to look in col 2
Dim ws As Worksheet: Set ws = ActiveSheet ' on the active sheet
Dim rgCol As Range
Set rgCol = ws.Columns(colNo) ' full col range (huge)
Set rgCol = Application.Intersect(ws.UsedRange, rgCol) ' shrink to nec size
Dim rgZeroCells As Range ' range to hold all the "0" cells (union of disjoint cells)
Dim rgCell As Range ' single cell to iterate
For Each rgCell In rgCol.Cells
If Not IsError(rgCell) Then
If rgCell.Value <> "7/26/2014" Then 'your search parameter
If rgZeroCells Is Nothing Then
Set rgZeroCells = rgCell ' found 1st one, assign
Else
Set rgZeroCells = Union(rgZeroCells, rgCell) ' found another, append
End If
End If
End If
Next rgCell
If Not rgZeroCells Is Nothing Then
' do something with rgZeriCells that you found earlier...
' rgZeroCells.EntireRow.Delete ' i.e. deletes all the target rows at once
End If
End Sub