+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Find and Replace with multiple conditions problem.

    Hey all,
    Hopefully someone can help me with this problem I'm having, as I can't seem to find a solution anywhere online or in the help files that works for me. I'm currently trying to write some code that will search a row for a specific string, and once found, will then look at the cell to the immediate left for a different specific string. If it finds both, it will then copy the contents of the cell 2 rows to the left into a different cell on a seperate sheet. This is what I've come up with so far:

    Code:
    Sub Find_MH()
        Dim FindString As String
        Dim Rng As Range
        FindString = "MH"
        If Trim(FindString) <> "" Then
            With Sheets("Data").Range("D:D")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not Rng Is Nothing And Rng.Offset(0, -1).Value = "Available" Then
                    Sheet1.Range("B29").Value = Rng.Offset(0, -2).Value
                    Rng.Offset(0, -1).Value = "Allocated"
                    Else
                    MsgBox "No MH Found"
                End If
            End With
        End If
    End Sub
    That code will search down the D column for the string "MH" and then check for the string "Available" in the adjacent cell. What I can't figure out is how to properly use the FindNext command to keep searching if it doesn't find the "Available" string in the adjacent cell, until both conditions are met, and then execute the other code.

    Hopefully I've explained it so that other people can understand what I'm trying to do.
    Any help would be greatly appreciated

    Chris
    Last edited by Benisato; 03-12-2010 at 06:06 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    Try this:

    Code:
    Option Explicit
    
    Sub Find_MH()
    Dim FindString As String
    Dim strFind As Range, strFirst As Range
        
        FindString = "MH"
        If Trim(FindString) <> "" Then
            With Sheets("Data").Range("D:D")
                Set strFind = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                    If Not strFind Is Nothing Then
                        Do
                            If strFirst Is Nothing Then Set strFirst = strFind
                            If strFind.Offset(0, -1).Value = "Available" Then
                                Sheet1.Range("B29").Value = strFind.Offset(0, -2).Value
                                strFind.Offset(0, -1).Value = "Allocated"
                                Exit Do
                            Else
                                Set strFind = .FindNext(After:=strFind)
                            End If
                        Loop Until strFind.Address = strFirst.Address
                    Else
                        MsgBox "No MH Found"
                    End If
            End With
        End If
    End Sub
    Last edited by JBeaucaire; 03-11-2010 at 04:11 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Hrm, I tried that code in my sheet, and it still always only returns the first MH it finds, even if the adjacent cell doesn't say "Available". It looks like it should work, not sure whats wrong with it.

    Chris

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    Post up a workbook, make sure it's clear what you want as "results".

    This is set to find and transfer one match. You want it to run and find them all at once?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Basically what I'm trying to do is create a Schedule Generator. It works like this, when you run it, it pops up a userform that lists all the employees that work in my department, and it has a dropdown box beside each one. The dropdown box defaults to "Available" to signify that they're available to be slotted into the schedule. If you change the dropdown box to something other than available, it changes the corresponding box in the data sheet. The code then uses the data sheet to find available people and slot them into positions they're trained to do. I'm sure my workbook wont look too pretty to some of you pro's, I'm still new to excel, and trying to push myself to learn more with challenging tasks.

    The part I'm trying to get working now is to get it to fill in the "MH" position on the schedule, with someone who is trained as an "MH", But if the first "MH" isn't available, I want it to select the 2nd one and put that person in that position instead.

    Hope that clarifies things a bit.

    Chris
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    I consider this a minor hurdle, the major hurdle I forsee with this little project is that once I get all off the critical positions slotted into the schedule (MH, QA, and 2 Printers) I want to eventually have it slot in the rest of the available operators into random locations on the schedule

  7. #7
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Can anyone else possibly help me with this?

    Thanks,
    Chris

  8. #8
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Nevermind, I figured out the problem,
    Code:
     If strFind.Offset(0, -1).Value = "Available" Then
                                Sheet1.Range("B29").Value = strFirst.Offset(0, -2).Value
                                strFirst.Offset(0, -1).Value = "Allocated"
    Should have been

    Code:
    If strFind.Offset(0, -1).Value = "Available" Then
                                Sheet1.Range("B29").Value = strFind.Offset(0, -2).Value
                                strFind.Offset(0, -1).Value = "Allocated"
    Thanks a lot for the help, it is very much appreciated.

    Chris

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    My bad, that was a rookie error. Sorry about that. I edited the listing in post #2 so that it reads as complete and correct.

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  10. #10
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Actually, it created a new problem. The code works great as is unless it doesn't find any matches, in which case it should output a textbox that says "No MH Found" but that currently never happens, if it doesn't find anything that matches both criteria, it just does nothing and makes field "B29" blank, any ideas how I can fix that?

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    I thought that was in there. Please post an updated example book w/macro so I can see that...
    Last edited by JBeaucaire; 03-11-2010 at 03:43 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    Ok, in this workbook, if you hit the generate schedule button, it will bring up a userform with all the employees. If you change any of them from "available" to anything else, it should exclude them from the schedule. Currently if you change "Connie-Lee" and or "Shannon" from available to something else, it should skip over them and search for a different printer via the Data sheet Column D, if it can't find them, it should then search for a backup, Via the data sheet, Column E, if it can't find one there, it should output a textbox that says "No printer found" same for MH and QA, but once it completes the loop for the primary positions, it never searches for a backup, and never outputs a textbox if it can't find the appropriate people. I hope that explanation made sense.
    Attached Files Attached Files

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    There's no need for a backup-qa macro, the one macro is supposed to keep looping for you until one is found. If your address the cells directly instead of trying to offset, then you can target the "allocated" message to column C no matter where the QA is found, you know?

    example:
    Code:
    removed...see below


    I've changed all the macros accordingly to demonstrate and simplify.
    Last edited by JBeaucaire; 03-12-2010 at 12:10 AM. Reason: code and sheet removed...see below
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  14. #14
    Registered User
    Join Date
    03-10-2010
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Find and Replace with multiple conditions problem.

    When I try to run that, I get a Run-time error '6'
    Overflow

  15. #15
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    18,228

    Re: Find and Replace with multiple conditions problem.

    On the sheet I uploaded? Please provide more specifics, when an error occurs, DEBUG and report the line of code that stalled, also hover your mouse over all the variables on and near that row to note the current values of those variables. Thanks.

    Meanwhile, I got tunnelvision testing the "no QA" message, so I got that working and forgot to verify the form would find names normally, which it stopped doing. This fix should do that, forgot some periods...pesky little things.

    Example:
    Code:
    Sub Find_QA()
    Dim FindString As String
    Dim strFind As Range, strFirst As Range
        
        FindString = "QA"
        If Trim(FindString) <> "" Then
            With Sheets("Data").Cells
                Set strFind = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False)
                If Not strFind Is Nothing Then
                    Do
                        If strFirst Is Nothing Then Set strFirst = strFind
                        If .Cells(strFind.Row, "C").Value = "Available" Then
                            Sheet1.Range("B28").Value = .Cells(strFind.Row, "B").Value
                            .Cells(strFind.Row, "C") = "Allocated"
                            Exit Sub
                        Else
                            Set strFind = .FindNext(After:=strFind)
                            If strFind.Address = strFirst.Address Then
                                MsgBox "No QA Found"
                                Exit Sub
                            End If
                        End If
                    Loop Until strFind.Address = strFirst.Address
                Else
                    MsgBox "No QA Found"
                    Exit Sub
                End If
            End With
        End If
    End Sub
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

Thread Information

Users Browsing this Thread

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

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.2.0