+ Reply to Thread
Results 1 to 9 of 9

VBA find row for double conditions

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2017
    Location
    Paris
    MS-Off Ver
    2013
    Posts
    35

    VBA find row for double conditions

    Dear all,

    I would once again really appreciate your help on the following below:

    I need to find the row number when two conditions are met:

    1. First, find in column 23 the value x
    2. Second, find in column 91 the value shop

    Wheb I run the code, it doesnt work yet. Do you have any tips?

    Thanks!!

    If wscalc.Columns(23).Find(x, , , xlWhole)), , , xlWhole) And wscalc.Columns(91) = shop Then
    
    r = activecell.row
    Last edited by jeffreybrown; 02-17-2018 at 06:51 PM. Reason: Please use code tags!

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: VBA find row for double conditions

    I am not quite sure of the requirement, but this code will find where "x" is found in column 23, and then starting at that row, look for "shop" in column 91.
    Dim wscalc As Workbook
    Dim rng As Range
    
    Set rng = wscalc.Columns(23).Find("x", LookIn:=xlValues, lookat:=xlWhole)
    
    Set rng = wscalc.Columns(91).Find("shop", after:=wscalc.Cells(rng.Row - 1, 91), LookIn:=xlValues, lookat:=xlWhole)
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    01-03-2018
    Location
    Fukuoka, Japan
    MS-Off Ver
    Excel 2013
    Posts
    123

    Re: VBA find row for double conditions

    I guess Bundi wants the row where column 23 has "x" and column 91 has "shop".
    Then, try the code below.

    Sub Macro1()
        Dim found As Range, firstFound As Range
        Dim col1 As Long, col2 As Long
        Dim str1 As String, str2 As String
        Dim r As Long
        
        col1 = 23: str1 = "x"
        col2 = 91: str2 = "shop"
        
        With wscalc.Columns(col1)
            Set found = .Find(What:=str1, LookAt:=xlWhole)
            If Not found Is Nothing Then
                If Cells(found.Row, col2) = str2 Then
                    r = found.Row
                Else
                    Set firstFound = found
                    Do
                        Set found = .FindNext(found)
                        If Not found Is Nothing Then
                            If Cells(found.Row, col2) = str2 Then
                                r = found.Row
                                Exit Do
                            End If
                        End If
                    Loop Until found.Address = firstFound.Address
                End If
            End If
        End With
    
        MsgBox r
    End Sub

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA find row for double conditions

    Try
    Sub test()
        Dim x, shop, a, b
        x = "x"
        shop = "shop"
        If Not IsNumeric(x) Then x = Chr(34) & x & Chr(34)
        If Not IsNumeric(shop) Then shop = Chr(34) & shop & Chr(34)
        With ActiveSheet
            a = Intersect(.UsedRange, .Columns(23)).Address
            b = Intersect(.UsedRange, .Columns(91)).Address
            a = Filter(.Evaluate("transpose(if((" & a & "=" & x & ")*(" & b & "=" & shop & "),row(" & a & ")))"), False, 0)
            If UBound(a) > -1 Then
                MsgBox "Found in" & vbLf & Join(a, vbLf)
            Else
                MsgBox "Not found"
            End If
        End With
    End Sub
    Last edited by jindon; 02-18-2018 at 01:45 AM.

  5. #5
    Registered User
    Join Date
    05-08-2017
    Location
    Paris
    MS-Off Ver
    2013
    Posts
    35

    Re: VBA find row for double conditions

    Dear Jindon,

    Indeed, your code works superfast with the same principle, very smart indeed.

    I just have one stupid question; iso returning a message box with the line row, can a just be assigned with the row found and stop therafter?

    Thanks!!!!!

  6. #6
    Registered User
    Join Date
    05-08-2017
    Location
    Paris
    MS-Off Ver
    2013
    Posts
    35

    Re: VBA find row for double conditions

    yujin, your code works great! THank you all for your help!

  7. #7
    Forum Contributor
    Join Date
    01-03-2018
    Location
    Fukuoka, Japan
    MS-Off Ver
    Excel 2013
    Posts
    123

    Re: VBA find row for double conditions

    You are welcome, Bundi. Thank you for your feedback.
    Personally, jindon's code, using an array, interests me.

  8. #8
    Registered User
    Join Date
    05-08-2017
    Location
    Paris
    MS-Off Ver
    2013
    Posts
    35

    Re: VBA find row for double conditions

    Your help would be highly appreciated!

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA find row for double conditions

    You mean like this?
    Sub test()
        Dim x, shop, a, b, myRow As Long
        x = "x"
        shop = "shop"
        If Not IsNumeric(x) Then x = Chr(34) & x & Chr(34)
        If Not IsNumeric(shop) Then shop = Chr(34) & shop & Chr(34)
        With ActiveSheet
            a = Intersect(.UsedRange, .Columns(23)).Address
            b = Intersect(.UsedRange, .Columns(91)).Address
            myRow = .Evaluate("min(if((" & a & "=" & x & ")*(" & b & "=" & shop & "),row(" & a & ")))")
            If myRow > 0 Then
                Application.Goto .Cells(myRow, 23), True
            Else
                MsgBox "Not found"
            End If
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Double if conditions to match then add to an existing dictionary
    By KAYABRFR in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-11-2018, 11:48 AM
  2. selection on double conditions
    By petenc in forum Excel General
    Replies: 2
    Last Post: 05-12-2017, 03:26 PM
  3. [SOLVED] Using find replace to first find two double quotes and replace with a single double quote
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-17-2015, 10:13 AM
  4. Replies: 0
    Last Post: 08-26-2014, 08:04 AM
  5. [SOLVED] Double find?
    By PyhaWaim in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-21-2014, 10:49 AM
  6. Change cells back to original conditions by double clicking another cell
    By Bonnister in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-14-2013, 10:21 AM
  7. double conditions for CountIf
    By Knud Gentz in forum Excel General
    Replies: 3
    Last Post: 04-03-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1