Getting Row Number

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,950
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I can do this the long way but there may be a clever Excel method
I want the worksheet row number where Col F is "AA" and Col C is not "ABCD"
I'm using VBA but could maybe use Application.WorksheetFunction ?
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
There's gotta be better !
VBA Code:
Private Function rowforOtherTitle(LC, Title, Sheet) As Long
    Dim i As Integer
    Dim Dat As Variant
    With Worksheets(Sheet)
        For i = 1 To .Cells(1, "T") 'Number of Rows in Sheet
            If .Cells(i, "F") = LC Then
                Dat = Dat & ";" & i & "@" & .Cells(i, "C")
            End If
        Next
    End With
    Dat = Split(Dat, ";")
    If UBound(Dat) = 2 Then 'Only first two expected / needed
        If Title = Split(Dat(1), "@")(1) Then
                rowforOtherTitle = Val(Dat(2))
            Else
                rowforOtherTitle = Val(Dat(1))
        End If
    End If
End Function
 
Upvote 0
Try
VBA Code:
Sub Macro1()
Dim Cnt&
Cnt = Application.WorksheetFunction.CountIfs(Range("F:F"), "AA", Range("C:C"), "<>ABCD")

End Sub
 
Upvote 0
IF row numbers are required, M gives array of required row numbers.
VBA Code:
Sub rownumbers()
Dim rng1 As Range, rng2 As Range, cel As Range
Dim cri1$, cri2$
Dim M
Set rng1 = Range("F2:F20"): Set rng2 = Range("C2:C20")
cri1 = "AA": cri2 = "ABCD"
M = Filter(Evaluate("transpose(If((" & rng1.Address & "=""" & cri1 & """)*(" & rng2.Address & "<>""" & cri2 & """),Row(" & rng1.Address & "),false))"), False, False)
End Sub
 
Upvote 0
Thanks for reply. If adapted correctly I have
VBA Code:
Sub rownumbers(LC, Title, Sheet)
Dim rng1 As Range, rng2 As Range, cel As Range
Dim cri1$, cri2$
Dim M
Dim cnt
With Worksheets(Sheet)

    cnt = Application.WorksheetFunction.CountIfs(.Range("F:F"), LC, .Range("C:C"), "<>" & Title)
    
    Set rng1 = .Range("F1:F" & .Cells(1, "T")): Set rng2 = .Range("C1:C" & .Cells(1, "T"))
    cri1 = LC: cri2 = Title
    M = Filter(Evaluate("transpose(If((" & rng1.Address & "=""" & cri1 & """)*(" & rng2.Address & "<>""" & cri2 & """),Row(" & rng1.Address & "),false))"), False, False)
End With
End Sub
LC is "AA" and Title is "ABCD"
cnt is I and that's right, but M is never anything. I've done something wrong?
 
Upvote 0
Returns comma separated row numbers if multiple.
You can add more range, criteria.
Code:
Private Function rowforOtherTitle(ParamArray x())
    Dim a, s(1), i&, ii&, n&, temp, w, flg As Boolean
    ReDim a(1 To (UBound(x) + 1) / 2)
    For i = 0 To UBound(x) Step 2
        Select Case True
            Case x(i + 1) Like "<>*": n = 2
            Case x(i + 1) Like "[<>=]*": n = 1
            Case x(i + 1) Like "[<>]=*": n = 3
            Case Else: n = 0
        End Select
        s(0) = IIf(n = 0, "=", Left$(x(i + 1), n))
        s(1) = Mid$(x(i + 1), n + 1)
        If Not IsNumeric(s(1)) Then s(1) = Chr(34) & s(1) & Chr(34)
        a(i / 2 + 1) = Filter(x(i).Parent.Evaluate("transpose(if(" & _
        x(i).Address & s(0) & s(1) & ",row(" & x(i).Address & ")))"), False, 0)
    Next
    For i = 0 To UBound(a(1))
        For ii = 2 To UBound(a)
            temp = Application.Match(a(1)(i), a(ii), 0)
            If IsError(temp) Then flg = True: Exit For
        Next
        If Not flg Then w = w & "," & a(1)(i)
        flg = False
    Next
    If Len(w) Then rowforOtherTitle = Mid(w, 2)
End Function
Book1.xlsm
ABCDEFGHI
1
25AA2,5,7,8,10
32ABCD
45ABCDAA2,5,8
54AA
64ABCD2,4,5,7,8,9,10
72AA
85AA
93ABCDAA
103AA
11
Sheet1
Cell Formulas
RangeFormula
H2H2=rowforOtherTitle(F2:F10,"AA",D2:D10,"<>ABCD")
H4H4=rowforOtherTitle(F2:F10,"AA",C2:C10,">3",D2:D10,"<>ABCD")
H6H6=rowforOtherTitle(F1:F10,"AA")
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top