这可能是东西,我每天做几次。因此,我为它建立了自定义功能,我可以轻松分享。如果你有改进的想法 - 我愿意听取他们的意见。
这是函数:
Public Function fnLngLocateValueCol(ByVal strTarget As String,
ByRef wksTarget As Worksheet, _
Optional lngRow As Long = 1, _
Optional lngMoreValuesFound As Long = 1, _
Optional blnLookForPart = False, _
Optional blnLookUpToBottom = True) As Long
Dim lngValuesFound As Long
Dim rngLocal As Range
Dim rngMyCell As Range
fnLngLocateValueCol = -999
lngValuesFound = lngMoreValuesFound
With wksTarget
Set rngLocal = .Range(.Cells(lngRow, 1), .Cells(lngRow, Columns.Count))
End With
For Each rngMyCell In rngLocal
If blnLookForPart Then
If strTarget = Left(rngMyCell, Len(strTarget)) Then
If lngValuesFound = 1 Then
fnLngLocateValueCol = rngMyCell.Column
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
Else
If strTarget = Trim(rngMyCell) Then
If lngValuesFound = 1 Then
fnLngLocateValueCol = rngMyCell.Column
If blnLookUpToBottom Then Exit Function
Else
Call Decrement(lngValuesFound)
End If
End If
End If
Next rngMyCell
End Function
因此,如果你想在活动表第1行的第一个值,你可以调用像这样:
fnLngLocateValueCol("valueToSearchFor",ActiveSheet)
对于第二个值,你这样调用:
?fnLngLocateValueCol("valueToSearchFor",ActiveSheet,lngMoreValuesFound:=2)
对于你这样调用的最后一个值:
?fnLngLocateValueCol("valueToSearchFor",ActiveSheet,blnLookUpToBottom:=false)
如果你有ValueToSearchFor在列,可以通过寻找任何与价值开始找到它。就像这样:
?fnLngLocateValueCol("Value",ActiveSheet,blnLookForPart:=True)
该行上,您正在寻找的也是一个可选参数(lngRow
),以数值1
还有用于lngRow
可选参数(当它是不顶行)或blnLookForPart
,当你正在寻找的一部分。 -999是未找到值的代码。
到目前为止,它在一些VBA应用程序中的工作时间超过6个月。
例程Decrement
其在代码称为如下:
Public Sub Decrement(ByRef value_to_decrement As Variant, Optional l_minus As Double = 1)
value_to_decrement = value_to_decrement - l_minus
End Sub
是头部都在一个特定的列或行? –
那么你的实际产出是多少?你期望什么?你有什么问题,你没有问过吗?你有没有看过文档的例子[Range.FindNext](https://msdn.microsoft。com/en-us/vba/excel-vba/articles/range-findnext-method-excel)能正常工作吗?有一个完整的例子来说明如何实现它。 –
@ L.Dutch它的一个特定列 – Ashok