+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: cells.Find not working

  1. #1
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    cells.Find not working

    Hi,

    I have a particular text to search for ("Description of Jobs Created") in a worksheet.
    I am sure this string is present but on debugging I can see that cells.find is not working.

    I have 3 checks out of which the first and last is going to the correct cell positions however, the check after cells.find is not searching and going to the cell. Please help.

    name.Sheets("Prime Recipient").Select
                    name.Sheets("Prime Recipient").Activate
                    ActiveSheet.Range("B4").Activate
                    Y = ActiveCell.Value  ' Check 1
                                   
                    lookstrg = "Description of Jobs Created*"
                    Cells.Find(What:=lookstrg, LookIn:=xlValues, MatchCase:=False).Activate
                    
                    Y = ActiveCell.Value  ' Check 2
                    
                    ActiveSheet.Range("B33").Select
                    Y = ActiveCell.Value   ' Check 3

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    Whatever Name is, name it something else. That's a reserved word in VBA.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    Before the first sentence of the code, I have
    Set name = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "sContr.xls")

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    Don't do that. Name is a reserved word in the language.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    I have done that. Another issue that I am facing is that of a LIKE operator.

    I have a string variable and I want to use a statement as follows:

    If Y like ("*abc.xls") Or ("def.xls") Then

    However, I think the like is also a reserved word that means something different in macro.

    How to perform the above task (LIKE operator) ?

    Thanks

  6. #6
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    Like is assuredly a reserved word -- it's a comparson operator. Look it up in Help.

    But there's no need for Like in those examples.

    =if lcase(right(myString, 7)) = "abc.xls" or lcase(right(myString, 7)) = "def.xls" then
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    Can I use wildcard characters in your formula

    =if lcase(right(myString, 7)) = "abc.xls" or lcase(right(myString, 7)) = "def.xls" then

    Instead of

    ="abc.xls"

    can I use

    ="*abc.xls"

    so that tomabc.xls and samabc.xls both are covered

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    That's true, you can, but there's no need for it in your examples, and Like is a very heavy operator.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    coming back to my earlier question regarding cells.find not working,

    Here is what I am using:

    ActiveSheet.Range("B4").Activate
                                Y = ActiveCell.Value
                                                        
                                lookstrg = "Description of Jobs Created*"
                                Cells.Find(What:=lookstrg, LookIn:=xlValues, MatchCase:=False).Activate
                                
                                Y = ActiveCell.Value
    Now after first 2 commands, I see the variable Y value in watch window and it gives me the correct value. So I am sure I am in the correct worksheet and so on.

    After the cells.find command, the value of Y doesnt change. Ideally it should point to a different cell and give me Y=Description of Jobs Created*

    Can you tell me what I am doing wrong ?

    Thanks
    Last edited by ncduke; 10-18-2010 at 07:18 PM.

  10. #10
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    As a matter of practice, you shouldn't follow Find with Activate -- it will error if the sought value is not found.

    That said, does it find the value and activate the new cell?
    Last edited by shg; 10-19-2010 at 11:45 AM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    Thanks for giving me the recommendation. I will keep it in mind.

    But it doesn't activate the correct cell.

    My plan is to find the cell containing the text => "Description of Jobs Created*"
    Go to the cell underneath this text.
    Update the cell with a variable value that I have calculated earlier in the code.

    Currently I have a work around where the cells are hard coded and although I know that this is not a good practice, in this case it wont hurt because the template wont change.

    I am preparing for the eventuality where the template does change and then the program would still run had I used the cells.find function. Hope this explains.

    Additional question => One of the worksheets is acting strange. I bet this one is protected differently. Certain columns are uneditable. Others are where I have to input stuff. Even the Ctrl +F for regular find doesnt work. It is only editable in certain places where we are expected to enter values. Can a macro cells.find work properly and find a cell text which is locked for editing ?

  12. #12
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    Post a sanitized workbook with the code.

    Can a macro cells.find work properly and find a cell text which is locked for editing ?
    A macro can unprotect the sheet to operate, or protect a sheet with the UserInterfaceOnly option to enable it to operate.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  13. #13
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    Due to work security limitations I cant upload the worksheet !

    Did you figure out my error in the cells.find statement that I had asked earlier ?

    Thanks

  14. #14
    Registered User
    Join Date
    10-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    21

    Re: cells.Find not working

    You might find this interesting:

    I have the following code:

    ActiveSheet.Range("B33").Select
                                ActiveCell.Value = FTE
                                
                                ActiveSheet.Range("D33").Select
                                ActiveCell.Value = JobString
                                ActiveWorkbook.Close savechanges:=True
    Now what happens is that the macro correctly find the cell B33 and selects and updates it with the value of the FTE variable.

    However, after doing that, it is unable to go and select the cell D33. Instead it updates the same cell B33 with the value of the JobString variable.

    Furthermore, for some instances, it selects a totally different cell (not even B33) and starts updating it.

    Is there a known issue or a problem with my code. This is excel 2003.

    Thanks

  15. #15
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: cells.Find not working

    Protected worksheet?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

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