VBA在Excel中搜索特定列
我试图使用下面的VBA代码搜索Excel中的特定列(列K),但当我运行宏时,它会搜索整个工作表,而不是指定的列 问题是它首先在前面的一列中找到'mycell 1',即在C列中,而不是我不希望它这样做的K列中 我还尝试在“Searchorder”中使用“xlByRows”,它也有同样的问题 非常感谢您的帮助 谢谢VBA在Excel中搜索特定列,vba,excel,Vba,Excel,我试图使用下面的VBA代码搜索Excel中的特定列(列K),但当我运行宏时,它会搜索整个工作表,而不是指定的列 问题是它首先在前面的一列中找到'mycell 1',即在C列中,而不是我不希望它这样做的K列中 我还尝试在“Searchorder”中使用“xlByRows”,它也有同样的问题 非常感谢您的帮助 谢谢 Range("K:K").Select Set foundcell1a = Selection.Cells.Find(What:=mycell1, After:=ActiveCell,
Range("K:K").Select
Set foundcell1a = Selection.Cells.Find(What:=mycell1, After:=ActiveCell, LookIn:=xlValues, LookAt _
:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
Set foundcell1a = Cells.FindNext
If Not foundcell1a Is Nothing Then
foundcell1a.Activate
End If
试试这个:
With Range("K:K")
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell1a = Range("K:K").Find(mycell1, LastCell)
If Not FoundCell1a Is Nothing Then
FirstAddr = FoundCell1a.Address
Range(FirstAddr).Activate
End If
希望这有帮助 试试这个:
With Range("K:K")
Set LastCell = .Cells(.Cells.Count)
End With
Set FoundCell1a = Range("K:K").Find(mycell1, LastCell)
If Not FoundCell1a Is Nothing Then
FirstAddr = FoundCell1a.Address
Range(FirstAddr).Activate
End If
希望这有帮助 不要使用
。如果可以避免,请选择,大多数情况下可以
尝试使用With…End With
构造。对要操作的对象尽可能具体
Sub SearchK()
Dim mycell1
Dim foundcell1a As Range
mycell1 = 1
With ActiveWorkbook.Worksheets("Sheet1").Range("K:K")
Set foundcell1a = .Find(mycell1, .Cells(.Rows.Count, 1))
Set foundcell1a = .FindNext(foundcell1a)
If Not foundcell1a Is Nothing Then
foundcell1a.Activate
End If
End With
End Sub
如果没有带…结尾的,则必须重复所有对象标识符,以便:
Set foundcell1a = .Find(mycell1, .Cells(.Rows.Count, 1))
必须表示为:
Set foundcell1a = ActiveWorkbook.Worksheets("Sheet1").Range("K:K").Find(mycell1, .Cells(.Rows.Count, 1))
VBA在计算命令时,需要在每次遇到句点(.)之前计算每个属性。使用ActiveWorkbook.Worksheets(“Sheet1”).Range(“K:K”)
可以去掉4个句点,因此运行速度也更快
Set foundcell1a=.Find(mycell1.Cells(.Rows.Count,1))
在列K中最后一个使用的单元格之后显示Findmycell1
,因此它会返回到列K中查找第一个实例,而不考虑活动单元格。不要使用。如果可以避免,请选择,大多数情况下可以
尝试使用With…End With
构造。对要操作的对象尽可能具体
Sub SearchK()
Dim mycell1
Dim foundcell1a As Range
mycell1 = 1
With ActiveWorkbook.Worksheets("Sheet1").Range("K:K")
Set foundcell1a = .Find(mycell1, .Cells(.Rows.Count, 1))
Set foundcell1a = .FindNext(foundcell1a)
If Not foundcell1a Is Nothing Then
foundcell1a.Activate
End If
End With
End Sub
如果没有带…结尾的,则必须重复所有对象标识符,以便:
Set foundcell1a = .Find(mycell1, .Cells(.Rows.Count, 1))
必须表示为:
Set foundcell1a = ActiveWorkbook.Worksheets("Sheet1").Range("K:K").Find(mycell1, .Cells(.Rows.Count, 1))
VBA在计算命令时,需要在每次遇到句点(.)之前计算每个属性。使用ActiveWorkbook.Worksheets(“Sheet1”).Range(“K:K”)
可以去掉4个句点,因此运行速度也更快
Set foundcell1a=.Find(mycell1.Cells(.Rows.Count,1))
在列K中最后一个使用的单元格之后显示Findmycell1
,因此它将返回以查找列K中的第一个实例,而不考虑活动单元格。为什么不尝试Set rng=ActiveSheet.Range(“K:K”)
然后rng.Cells.Find(…
;如果用户选择其他内容,则使用Select可能会导致问题。我希望第一个查找(即查找
)会在K列中找到条目,而第二个查找(即查找文本
)会在C列中查找(因为它是Cells.FindNext
而不是Selection.FindNext
)。为什么不试试Set rng=ActiveSheet.Range(“K:K”)
然后rng.Cells.Find(…
;如果用户选择其他内容,使用Select可能会导致问题。我本来希望第一次找到(即Find
)将在K列中找到条目,并且它将仅是第二个查找(即FindNext
)将在C列中查找(因为它是Cells.FindNext
,而不是Selection.FindNext
)。