+ Reply to Thread
Results 1 to 9 of 9

Use .Cells.Find(What:=) to find values in Multiple Cells

Hybrid View

s2jrchoi Use .Cells.Find(What:=) to... 05-13-2014, 08:21 AM
AlphaFrog Re: Use .Cells.Find(What:=)... 05-13-2014, 08:27 AM
MenacingBanjo Re: Use .Cells.Find(What:=)... 05-13-2014, 08:58 AM
s2jrchoi Re: Use .Cells.Find(What:=)... 05-13-2014, 09:35 AM
MenacingBanjo Re: Use .Cells.Find(What:=)... 05-13-2014, 10:53 AM
MenacingBanjo Re: Use .Cells.Find(What:=)... 05-13-2014, 08:35 AM
s2jrchoi Re: Use .Cells.Find(What:=)... 05-13-2014, 09:03 AM
AlphaFrog Re: Use .Cells.Find(What:=)... 05-13-2014, 11:13 AM
s2jrchoi Re: Use .Cells.Find(What:=)... 05-13-2014, 02:39 PM
  1. #1
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Use .Cells.Find(What:=) to find values in Multiple Cells

    All,

    I am currently using the .Cells.Find method to put values into my userform. It does well finding one specific value in a cell but I can't figure out how to get this method to look at two values in two different cells. For example, I would like it to do the following:

    If column B = sFind value and column C = sFind2 value, then pull the values from that row into the userform.

    Can anyone please help? I've looked everywhere and can't seem to find the solution that I am looking for.

    Thank you in advance for your help.


    With dataSheet
                On Error Resume Next
                lRowFnd = .Cells.Find(What:=sFind, After:=Cells(1, 2), LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlDown, MatchCase:=False, _
                SearchFormat:=False).Row
    
                'Required
                changeNum = .Cells(lRowFnd, 2)
                lineNum = .Cells(lRowFnd, 3)
                requestBy = .Cells(lRowFnd, 4)
                dateSub = .Cells(lRowFnd, 5)
                changeType = .Cells(lRowFnd, 6)
    End With

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    This link explains it well. See the "What if I want to search for multiple occurrences of an item" section

    The ultimate search
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-02-2012
    Location
    Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    Quote Originally Posted by AlphaFrog View Post
    This link explains it well. See the "What if I want to search for multiple occurrences of an item" section

    The ultimate search
    Using information from the article that AlphaFrog referred to, we can create a much more efficient search loop than the one I initially suggested. The below set of code uses the FindNext method to loop through all instances of strFind in Column 2 and for each one tests whether the cell in Column 3 of that same row also contains strFind.

    Dim strFirstAddress as String
    Dim rngFind as Range
    With dataSheet
        Set rngFind = .Cells(1, 2).EntireColumn.Find(strFind) is Nothing Then
        If Not rngFind is Nothing Then
            strFirstAddress = rngFind.Address
            Do Until rngFind is Nothing Or rngFind.Address = strFirstAddress
                If .Cells(rngFind.Row, 3).Value Like "*" & strFind & "*" Then
                    lRowFnd = rngFind.Row
                End If
                Set rngFind = .Cells(1, 2).EntireColumn.FindNext(rngFind)
            Loop
        End If
    
    'Required
        changeNum = .Cells(lRowFnd, 2)
        lineNum = .Cells(lRowFnd, 3)
        requestBy = .Cells(lRowFnd, 4)
        dateSub = .Cells(lRowFnd, 5)
        changeType = .Cells(lRowFnd, 6)
    End With

  4. #4
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    Thank you again for your help with this issue. I just noticed that the code is looking for strFind in column 2 and in column 3. I think I should have explained my situation more clearly but what I am looking for is to look up strFind in column B and then in column C, look for integers 1 - 6. For example, column B is the change form # which is generated by taking the user's First and Last Name Initials + Today's Date and Time ex: JH051320140933 and in column C, the values would be 1 - 6 depending on the line item. The first line item is 1 which every record should have but if there are more line items, then the next row would have the same change form # (JH051320140933) in column B but in column C, the line item would be 2. The max line items is 6.

    Please let me know your thoughts on this. Can I still apply the .Cells.Find method?

    Thank you again!
    Last edited by s2jrchoi; 05-13-2014 at 10:06 AM.

  5. #5
    Registered User
    Join Date
    08-02-2012
    Location
    Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    Quote Originally Posted by s2jrchoi View Post
    I just noticed that the code is looking for strFind in column 2 and in column 3. I think I should have explained my situation more clearly but what I am looking for is to look up strFind in column B and then in column C...
    If I'm not mistaken Column 2 and Column 3 are the same columns as B and C.
    If you need to find "JH051320140933" in Column B, and a certain integer in Column C, then use the same code as above except instead of looking for strFind in Column 3, you look for whatever integer you need. In the example below, I used the number 1.

    Dim strFirstAddress as String
    Dim rngFind as Range
    strFind = "JH051320140933"   'Or however you're pulling these strings
    With dataSheet
        Set rngFind = .Cells(1, 2).EntireColumn.Find(strFind) is Nothing Then
        If Not rngFind is Nothing Then
            strFirstAddress = rngFind.Address
            Do Until rngFind is Nothing Or rngFind.Address = strFirstAddress
                If .Cells(rngFind.Row, 3).Value = 1 Then    'Test if the value of the cell in Column C of this row is 1.
                    lRowFnd = rngFind.Row
                End If
                Set rngFind = .Cells(1, 2).EntireColumn.FindNext(rngFind)
            Loop
        End If
    
    'Required
        changeNum = .Cells(lRowFnd, 2)
        lineNum = .Cells(lRowFnd, 3)
        requestBy = .Cells(lRowFnd, 4)
        dateSub = .Cells(lRowFnd, 5)
        changeType = .Cells(lRowFnd, 6)
    End With

  6. #6
    Registered User
    Join Date
    08-02-2012
    Location
    Michigan
    MS-Off Ver
    2010
    Posts
    11

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    If you need a certain value to be in two different cells in the same row, I would start at the top and move down one row at a time until you get to the row you need.

    With dataSheet
        lRowFnd = 2
        Do Until lRowFnd > .Cells.SpecialCells(xlCellTypeLastCell).Row
            'Check if the value in lRowFind and Column 2 contains strFind and the value in lRowFind and Column 3 contains strFind.
            If .Cells(lRowFnd, 2).Value Like "*" & strFind & "*" And .Cells(lRowFnd, 3).Value Like "*" & strFind & "*" Then
                Exit Do
            End If
            lRowFnd = lRowFnd + 1
        Loop
    'Required
        changeNum = .Cells(lRowFnd, 2)
        lineNum = .Cells(lRowFnd, 3)
        requestBy = .Cells(lRowFnd, 4)
        dateSub = .Cells(lRowFnd, 5)
        changeType = .Cells(lRowFnd, 6)
    End With
    This is just what I would try, but someone else might have a better suggestion.

  7. #7
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    Thank you so much AlphaFrog and MenacingBanjo! I will take a look and get back to you with my results. Thank you again!!

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    I don't mean to be overly critical MenacingBanjo, but there are a couple of flaws in your code.

    Wrong syntax
    Set rngFind = .Cells(1, 2).EntireColumn.Find(strFind) is Nothing Then

    Do Until rngFind is Nothing Or rngFind.Address = strFirstAddress
    Before the Do-Loop begins, rngFind.Address does equal strFirstAddress so the loop never runs

    If the loop were to run, the "Required" variables are set after all the .Find matches are found. So the variables will only equal the last match. s2jrchoi has not explained what to do with each .Find match.

    With dataSheet

    dataSheet is not declared or defined. Maybe your assuming s2jrchoi is doing that earlier in the code?

  9. #9
    Forum Contributor
    Join Date
    12-19-2012
    Location
    Woodbridge, Virginia
    MS-Off Ver
    Excel 2010
    Posts
    111

    Re: Use .Cells.Find(What:=) to find values in Multiple Cells

    All,

    Thank you for your help. I got it to work but I got the help from AlphaFrog's link because I did notice a few flaws in MB's code.

    Option Explicit
    Sub loadChanges()
    
    Dim projName As String, dateSub As Variant, requestBy As String, changeNum As String, 
    Dim changeType As String, changeReq As Variant, projDesc as variant, projName as variant, origCost as currency, lineItem As Integer, FindAddress As String, x As Long, sFind As Variant, lRowFnd As Variant
    
    Dim dataSheet As Worksheet
    Set dataSheet = ActiveWorkbook.Worksheets("Database")
    
    sFind = requestForm.tbID.Value
       
    With dataSheet
        Set R = .Cells(1, 2).EntireColumn.Find(sFind)
        If Not R Is Nothing Then
            FindAddress = R.Address
            x = 1
            Do
                lineItem = x
                lRowFnd = R.Row
                'Required
                changeNum = .Cells(lRowFnd, 2)
    
                requestBy = .Cells(lRowFnd, 4)
                dateSub = .Cells(lRowFnd, 5)
                changeType = .Cells(lRowFnd, 6)
    
                'Project Information
                changeReq = .Cells(lRowFnd, 7)
                projDesc = .Cells(lRowFnd, 9)
                projName = .Cells(lRowFnd, 10)
                origCost = .Cells(lRowFnd, 11)
                
    
                If sFind = changeNum And lineItem = 1 Then
                    'Required
                    requestForm.tbChangeNum.Value = changeNum
                    requestForm.tbDate.Value = dateSub
                    requestForm.cbChangeType.Value = changeType
                    requestForm.tbUserName.Value = requestBy
                    'Project Information
                    requestForm.tbDesc.Value = projDesc
                    requestForm.tbProjName.Value = projName
                    requestForm.tbCost.Value = origCost
                    requestForm.tbCost.Value = Application.WorksheetFunction.Dollar(requestForm.tbCost.Value, 2)
                ElseIf sFind = changeNum And lineItem = 2 Then
                    requestForm.MultiPage2.Pages(1).Visible = True
                    requestForm.tbProjDescSD.Value = projDesc
                    requestForm.tbCostSD.Value = origCost
                    requestForm.tbCostSD.Value = Application.WorksheetFunction.Dollar(requestForm.tbCostSD.Value, 2)
                    requestForm.tbProjNameSD.Value = projName
                    requestForm.tbChType1.Value = changeReq
                End If
                
                x = x + 1
                Set R = .Cells(1, 2).EntireColumn.FindNext(R)
    
            Loop While Not R Is Nothing And R.Address <> FindAddress
        End If
    End With
                
    Set R = Nothing
    Thank you again!! I will mark this as solved.

+ 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. Find values from cells in multiple sheets
    By asubramaniam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 PM
  2. Find values from cells in multiple sheets
    By asubramaniam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  3. Find values from cells in multiple sheets
    By asubramaniam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  4. [SOLVED] Find values from cells in multiple sheets
    By asubramaniam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  5. Find values from cells in multiple sheets
    By asubramaniam in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 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