+ Reply to Thread
Results 1 to 7 of 7

Cells.Find not working with multiple workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    08-18-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    7

    Cells.Find not working with multiple workbooks

    I'm kind of a novice with VBA, and I'm having an issue with performing a "find" function on multiple workbooks. The goal of the macro is to look in each open workbook for the text "Trains and Planes" which will always be in column A and then clear the contents of that row and all rows below it. I salvaged most of this code from something I found on the internet. When I run it, it says "Run time error 91, Object variable or with block variable not set" and it highlights the lines Cells.Find through False).Activate in the debugger. I understand that this means it can't find the text, but I'm not sure why. If it makes any difference, this is in Personal.xlsb. Here's the code I have now:

    Sub Test1()
    Dim wb As Workbook
    Dim LastRow As Long, myCell As Range, myRange As Range
    Dim myCell1 As Range
    
    For Each wb In Workbooks
    wb.Activate
    
    LastRow = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("A" & LastRow)
    
    Cells.Find(What:="Trains and Planes", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False).Activate
    Set myCell = ActiveCell
     
    Set myRange = Range(myCell, myCell1)
    myRange.EntireRow.ClearContents
    
    Application.CutCopyMode = False
    
    Next wb
    End Sub
    Any help would be greatly appreciated!

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Cells.Find not working with multiple workbooks

    Hi DGFA,

    Welcome to the Forum!

    You need to test each sheet in each book.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    08-18-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    7

    Re: Cells.Find not working with multiple workbooks

    Hi, xladept!

    Individually, each sheet works fine.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Cells.Find not working with multiple workbooks

    Hi, DGFA,

    maybe try this code (untested):
    Sub Test1()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim LastRow As Long, myCell As Range
    Dim myCell1 As Range
    
    For Each wb In Workbooks
      For Each ws In wb.Worksheets
        LastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).Row
        Set myCell1 = ws.Range("A" & LastRow)
        
        Set myCell = ws.Columns("A:A").Find(What:="Trains and Planes", After:=myCell1, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not myCell Is Nothing Then
          ws.Range(myCell, myCell1).EntireRow.ClearContents
          Application.CutCopyMode = False
          Set myCell = Nothing
        End If
      Next ws
    Next wb
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    08-18-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    7

    Re: Cells.Find not working with multiple workbooks

    Hi, Holger!

    I ran it, but it only modified the first workbook and never touched the second one. I think it actually might've repeated itself again on the first sheet instead.

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Cells.Find not working with multiple workbooks

    Hi, DGFA,

    just to make clear: the workbooks need to be opened in the same instance of Excel to make the code work.

    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    08-18-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    7

    Re: Cells.Find not working with multiple workbooks

    Hi Holger,

    They were both open at the same time.

    Thanks,

    DGFA

+ 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. loop through workbooks to find term, find not working
    By bsapaka in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-14-2015, 06:16 PM
  2. Working with multiple workbooks
    By njadam in forum Excel General
    Replies: 1
    Last Post: 12-07-2014, 03:15 PM
  3. Working with duplicates and multiple workbooks
    By altafullahu in forum Excel General
    Replies: 3
    Last Post: 01-31-2013, 02:56 PM
  4. Working with multiple workbooks from one workbook
    By jason_lee_91 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-07-2012, 01:19 PM
  5. Copmpare/Find and replace cells in two separate workbooks across multiple sheets
    By jmarrxx in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2012, 09:53 AM
  6. Working with multiple Workbooks...
    By XIII in forum Excel General
    Replies: 4
    Last Post: 11-27-2006, 02:13 PM
  7. [SOLVED] Working on Multiple Workbooks
    By create_share in forum Excel General
    Replies: 1
    Last Post: 07-24-2006, 07:30 AM

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