Hi beecher,
the UDF can be pasted into a standard module in the VBA editor.
When you need to use it you can either find it in the Insert Function
dialog that pops up when you go Insert|Function... (with "All" in the
"Or select a category:" box), or you can simply type "=INCREMENTROW("
then type in the range to search followed by a comma, then the instance
value, then close the arguments bracket and press enter.
If you want to see all instances, one in each cell of a series of
contiguous cells, then you could type this formula into a cell (I have
assumed the range to search is A1 to A100)...
=INCREMENTROW($A$1:$A$100,ROW(A1))
then keep filling that formula down until it results in a blank cell.
This works because filling ROW(A1) down a column increases by 1 each
time.
If ordinary macro is what you prefer then try this...
Public Sub WhereDoesSelectionIncrement()
Dim rngCell As Range
Dim rngSEARCH As Range
Dim K As Long
Dim lInstanceCheck As Long
Dim lCells As Long
Dim strOutput As String
Do
Set rngSEARCH = Application.InputBox( _
prompt:="From one column only, select a range to search.", _
Title:="Search for increment row.", _
Default:=Selection.Address, _
Type:=8)
If rngSEARCH.Columns.Count > 1 Then
MsgBox "Try again!" & vbNewLine _
& "Select cells from one column only."
End If
Loop While rngSEARCH.Columns.Count > 1
For K = 1 To rngSEARCH.Cells.Count - 1
If rngSEARCH.Cells(K).Value = _
rngSEARCH.Cells(K + 1).Value - 1 Then
Let strOutput = strOutput _
& rngSEARCH.Cells(K).Address(False, False) & ", "
End If
Next K
If strOutput = "" Then
Let strOutput = "No increments found."
Else: strOutput = Left(strOutput, Len(strOutput) - 2)
End If
MsgBox strOutput 'If you prefer the output to be in a cell
'then change MsgBox strOutput to something like
'Range("A1").value = strOutput
End Sub
It produces a string containing the addresses, separated by
comma+space, of the cells where incrementing by 1 occurs .
See the comment at the end that will show you how to change the output
to a worksheet cell.
I hope this helps you solve your problem.
Ken Johnson