+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: cells.Find not working

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

    Re: cells.Find not working

    No, I removed the protection from one of them just to be sure.
    I also tried Sheets("Prime Recipient").Range("B33").Value = FTE

    but nothing works.

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

    Re: cells.Find not working

    Just wanted to confirm whether
    Sheets("Prime Recipient").Range("B33").Value = FTE

    is a valid command.

    FTE is a variable that gets some value through the earlier part of the code. I can see in the watch window that the code is assigning correct values to this variables.

    Essentially just updating the cell with this value is not working.

  3. #18
    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

    ActiveSheet.Range("B33").Select
    ActiveCell.Value = FTE
    ...is not the same as
    Sheets("Prime Recipient").Range("B33").Value = FTE
    Both are valid constructs, though the selecting in the first case is unnecessary. If the sheet is protected and B33 is locked, the first will fail on the Select, and the second will fail on the assignment.

    If you want to post a useful amount of code and some context, I'm happy to help. The little fragments are not much use.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

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

    Re: cells.Find not working

    I have unprotected the entire sheet. Besides, even originally, these cells are input cells which means user has to enter text or numbers here.

    Here is some useful amount of code :

    With Application.FileSearch
            .LookIn = Directory
            .FileType = msoFileTypeExcelWorkbooks
            .Filename = code
    
             If .Execute > 0 Then
                
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & ".xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "s.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "Contr.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "sContr.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRI.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRIs.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "SubOnly.xls")
                    
                    
                                   
                    mybookname.Sheets("Prime Recipient").Select
                    mybookname.Sheets("Prime Recipient").Activate
                    
                    Y = ActiveWorkbook.name
                                      
                    If Right(Y, 10) = "sContr.xls" Or Right(Y, 9) = "Contr.xls" Then
                        
                        ActiveSheet.Range("F31").Select
                        ActiveCell.Value = FTE
                        
                        ActiveSheet.Range("B33").Select
                        ActiveCell.Value = JobString
                        ActiveWorkbook.Close savechanges:=True
                        
                        Application.Goto Reference:="Bookmark"
                        ActiveCell.Offset(blankcount, 0).Select
                        Pointer = ActiveCell.Value
                        
                    Else                        
                                                    
                            If Right (Y, 11) <> "SubOnly.xls" Then                        
                               
                                Sheets("Prime Recipient").Range("b33").Value = FTE
                                
                                Sheets("Prime Recipient").Range("d33").Value = JobString
                                
                                ActiveWorkbook.Close savechanges:=True
                                
                                Application.Goto Reference:="Bookmark"
                                ActiveCell.Offset(blankcount, 0).Select
                                Pointer = ActiveCell.Value
                            
                            Else
                            
                                ActiveWorkbook.Close savechanges:=True
                                Application.Goto Reference:="Bookmark"
                                ActiveCell.Offset(blankcount, 0).Select
                                Pointer = ActiveCell.Value
                            
                            End If
                    
                    End If
                                    
            Else
        
                Sheets("ERROR").Activate
                Range("A1").Activate
                
                ActiveCell.Offset(cntr, 0).Select
                ActiveCell.Value = code
                ActiveCell.Offset(0, 1).Value = "Code exists in table but file doesnt exist"
                cntr = cntr + 1
                
                Application.Goto Reference:="Bookmark"
                ActiveCell.Offset(blankcount, 0).Select
                
                Pointer = ActiveCell.Value
        
            End If
            
        End With
    Certain parts of the code pertain to logic in the earlier part of the program and can be overlooked for this discussion e.g.

    Application.Goto Reference:="Bookmark"
                                ActiveCell.Offset(blankcount, 0).Select
                                Pointer = ActiveCell.Value

  5. #20
    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

    Comments appear above the code they refer to.
        With Application.FileSearch
            .LookIn = Directory
            .FileType = msoFileTypeExcelWorkbooks
            .Filename = code
    
            If .Execute > 0 Then
    ? Why keep assigning the same object variable to a series of workbook?
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & ".xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "s.xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "Contr.xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "sContr.xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRI.xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRIs.xls")
                Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "SubOnly.xls")
    
    It is true, but obscured by method, that mybookname 
    refers to the last workbook opened above. The second 
    statement below is redundant. 
                mybookname.Sheets("Prime Recipient").Select
                mybookname.Sheets("Prime Recipient").Activate
    
    This should be just Y=mybookname.name
                Y = ActiveWorkbook.Name
    
    There's no need to test this; look at the Open statements 
    above. If you need a different object variable for each 
    workbook, assign them to the different workbooks in the 
    Open Statements. Also, the first test is subsumed by the 
    second.
                If Right(Y, 10) = "sContr.xls" Or Right(Y, 9) = "Contr.xls" Then
    
                    ActiveSheet.Range("F31").Select
                    ActiveCell.Value = FTE
    
                    ActiveSheet.Range("B33").Select
                    ActiveCell.Value = JobString
                    ActiveWorkbook.Close savechanges:=True
    
                    Application.Goto Reference:="Bookmark"
                    ActiveCell.Offset(blankcount, 0).Select
                    Pointer = ActiveCell.Value
    
                Else
                    If Right(Y, 11) <> "SubOnly.xls" Then
                        Sheets("Prime Recipient").Range("b33").Value = FTE
                        Sheets("Prime Recipient").Range("d33").Value = JobString
                        ActiveWorkbook.Close savechanges:=True
                        Application.Goto Reference:="Bookmark"
                        ActiveCell.Offset(blankcount, 0).Select
                        Pointer = ActiveCell.Value
    
                    Else
                        ActiveWorkbook.Close savechanges:=True
                        Application.Goto Reference:="Bookmark"
                        ActiveCell.Offset(blankcount, 0).Select
                        Pointer = ActiveCell.Value
                    End If
                End If
    
            Else
                Sheets("ERROR").Activate
                Range("A1").Activate
    
                ActiveCell.Offset(cntr, 0).Select
                ActiveCell.Value = code
                ActiveCell.Offset(0, 1).Value = "Code exists in table but file doesnt exist"
                cntr = cntr + 1
    
                Application.Goto Reference:="Bookmark"
                ActiveCell.Offset(blankcount, 0).Select
    
                Pointer = ActiveCell.Value
    
            End If
        End With
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

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

    Re: cells.Find not working

    There are multiple file types and each code could have any file type. So code is appended to the file type. E.g

    2010q3-203.xls is one valid file, similarly
    2010q3-303s.xls is another valid file.

    Moreover, there will always be only 1 file associated with a code. So what I am doing is trying a code with each file type possible. If there is any error I have the

    On Error Resume Next
    given at the start of the program. This answers your first question.

    Coming to the second one. Whichever workbook gets opened, I am not sure which worksheet gets activated, I have to ensure that the worksheet "Prime Recipient" is the one that gets activated. It is better to be safe. That was my thought for including :

    mybookname.Sheets("Prime Recipient").Select
    mybookname.Sheets("Prime Recipient").Activate
    Finally, I am assigning the value of myworkbookname to Y just to use it in the following IF statement. I could do away with it, but I dont see that to be a problem or issue.

    Currently I want to focus on the update part. Once that is resolved, I can tidy up the code.

    Thanks

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

    Re: cells.Find not working

    Regarding the IF statement and "first test is subsumed by the second".

    Thanks for pointing it out, I will incorporate it.

    I think I can just keep the second part.

    If Right(Y, 9) = "Contr.xls" Then
    Thanks

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

    Re: cells.Find not working

    This might be surprising.

    I changed the code as follows:

    With Application.FileSearch
            .LookIn = Directory
            .FileType = msoFileTypeExcelWorkbooks
            .Filename = code
    
             If .Execute > 0 Then
                
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & ".xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "s.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "Contr.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "sContr.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRI.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "-DCRIs.xls")
                    Set mybookname = Workbooks.Open(Filename:=Directory & "\2010q3-" & code & "SubOnly.xls")
                    
                    
                                   
                    mybookname.Sheets("Prime Recipient").Select
                    mybookname.Sheets("Prime Recipient").Activate
                    
                    ' Worksheets("Prime Recipient").EnableSelection = xlNoRestrictions
                    
                    Y = ActiveWorkbook.name
                                      
                    If Right(Y, 11) <> "SubOnly.xls" Then
                            
                                Cells.Find(What:="Number of Jobs*", LookIn:=xlValues, MatchCase:=False).Activate
                                ActiveCell.Offset(1, 0).Value = FTE
                                
                                Cells.Find(What:="Description of Jobs Created*", LookIn:=xlValues, MatchCase:=False).Activate
                                ActiveCell.Offset(1, 0).Value = JobString
                                
                                ActiveWorkbook.Close savechanges:=True
                                
                                Application.Goto Reference:="Bookmark"
                                ActiveCell.Offset(blankcount, 0).Select
                                Pointer = ActiveCell.Value
                            
                    Else
                            
                                ActiveWorkbook.Close savechanges:=True
                                Application.Goto Reference:="Bookmark"
                                ActiveCell.Offset(blankcount, 0).Select
                                Pointer = ActiveCell.Value
                            
                    End If
                    
                                    
            Else
        
                Sheets("ERROR").Activate
                Range("A1").Activate
                
                ActiveCell.Offset(cntr, 0).Select
                ActiveCell.Value = code
                ActiveCell.Offset(0, 1).Value = "Code exists in table but file doesnt exist"
                cntr = cntr + 1
                
                Application.Goto Reference:="Bookmark"
                ActiveCell.Offset(blankcount, 0).Select
                
                Pointer = ActiveCell.Value
        
            End If
            
        End With
    Essentially, first I manually edited the EnableSelection option of the worksheet to xlNoRestrictions and checked whether cells.find is working. It did work.

    So I thought of putting it in the code as

    Worksheets("Prime Recipient").EnableSelection = xlNoRestrictions
    Then things were working the way it should. So I just wanted to confirm whether it was this change itself that caused the program to work properly. Hence I removed all the files, brought in new ones from backup. In these files I had not altered anything manually. Then I commented the
    Worksheets("Prime Recipient").EnableSelection = xlNoRestrictions
    .

    Even then the program was working and behaving normally. Now my concern is what if the next time, during actual run, the cells.find misbehaves. Any guesses ?

    Thanks

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

    Re: cells.Find not working

    Just to elaborate and emphasize,

    after commenting the

    '   Worksheets("Prime Recipient").EnableSelection = xlNoRestrictions
    it appears that I have made no changes and suddenly the cells.find started working.

    This is what is not clear to me.

    Thanks

+ 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