+ Reply to Thread
Results 1 to 14 of 14

Multitude of random errors when cycling through workbooks in a folder to copy a range

Hybrid View

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Multitude of random errors when cycling through workbooks in a folder to copy a range

    I've spent all day trying to make this code work, and I have gotten dozens of errors, some of which I can fix, and some I can't.

    Option Explicit
    
    Sub OpenFiles()
    
    Dim MyFolder As String
    Dim MyFile As String
    Dim wbold As Object
    Dim wbnew As Object
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationManual
    
    MyFolder = "C:\Users\ALRH\Desktop\Batches"
    
    MyFile = Dir(MyFolder & "\*.xl*")
    
    Set wbold = ThisWorkbook
    
    Do While MyFile <> ""
        Workbooks.Open Filename:=MyFolder & "\" & MyFile
          
        Set wbnew = ThisWorkbook
          
        Sheets(1).Select
        Range("H25:H90").Copy
        Workbooks(wbold).Sheets("Shore Tank Report").Range("I25:I90").PasteSpecial xlPasteValues
        
        Workbooks(wbnew).Range("C7:H7").Copy
        Workbooks(wbold).Cells("D7:I7").PasteSpecial xlPasteValues
                                  
       Workbooks(wbnew).Close SaveChanges:=False
                        
        On Error GoTo 0
    
        MyFile = Dir
    Loop
        
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
        
    End Sub
    Right now I'm getting a "Type Mismatch" on the row:

    
    "Workbooks(wbold).Sheets("Shore Tank Report").Range("I25:I90").PasteSpecial xlPasteValues"
    Ask if you need more information, and thanks for any help.
    Last edited by LXN; 07-22-2013 at 04:34 PM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    LXN,
    Welcome to the forum!
    Please use code tags with your code as per forum's rule.
    You have few errors and I guessed some of the lines

    Option Explicit
    
     Sub OpenFiles()
    
     Dim MyFolder As String
     Dim MyFile As String
     Dim wbold As Object
     Dim wbnew As Object
    
     Application.ScreenUpdating = True
     Application.DisplayAlerts = True
     Application.EnableEvents = True
     Application.Calculation = xlCalculationManual
    
     MyFolder = "C:\Users\ALRH\Desktop\Batches"
    
     MyFile = Dir(MyFolder & "\*.xl*")
    
     Set wbold = ThisWorkbook
    
     Do While MyFile <> ""
     Workbooks.Open Filename:=MyFolder & "\" & MyFile
    
     Set wbnew = ThisWorkbook
    
     Sheets(1).Range("H25:H90").Copy
     wbold.Sheets("Shore Tank Report").Range("I25").PasteSpecial xlPasteValues
    
      Sheets(1).Range("C7:H7").Copy
      wbold.Cells("D7").PasteSpecial xlPasteValues
    
     Workbooks(wbnew).Close SaveChanges:=False
    
     On Error GoTo 0
    
     MyFile = Dir
     Loop
    
     Application.ScreenUpdating = True
     Application.DisplayAlerts = True
     Application.EnableEvents = True
     Application.Calculation = xlCalculationAutomatic
    
     End Sub

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    You have two
    Set wbold = ThisWorkbook
    
    
     Set wbnew = ThisWorkbook
    You need to separate between the source and destination books. For ease of reading, you should assign the source book as sourcebook=workbook.open......

  4. #4
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    Thanks of the response! I have edited my post, and changed wbold/wbnew to destinationbook/sourcebook, but I'm not sure how you're telling me to assign the variables. Could you clarify? (I made a reply to my own post by accident in stead of yours. How do I delete that?)

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    Second line

    MyFolder = "C:\Users\ALRH\Desktop\Batches"
         MyFile = Dir(MyFolder & "*.xl")  'Excel 2003 file types to import
       
        Do Until MyFile = ""
        
            Set sourceBook = Workbooks.Open(MyFolder & MyFile)
            
             With sourceBook.Worksheets(1)
    'Then the rest of the code

  6. #6
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    Thanks for the help!

    I made the changes you listed,Also, you linked me to a website. Is that required for this to work, or is it simply an easier way?
    Last edited by LXN; 07-23-2013 at 10:15 AM.

  7. #7
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    I got a 'type mismatch' here:

    Workbooks(wb).Sheets("Shore Tank Report").Range("I25:I90").PasteSpecial xlPasteValues
    Why could that be?

  8. #8
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    Thanks of the response! I have edited my post, and changed wbold/wbnew to destinationbook/sourcebook, but I'm not sure how you're telling me to assign the variables. Could you clarify?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    You start with this line

     Set wb = ThisWorkbook
        Set shtMy = Sheets("Master") 'Imports the data into the activesheet. Change to suit
    This is where the code will be copied in to

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range


  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    There are many lines that need to be changed, so I thought if you follow Ron's code, it will give you a good start, instead of doing by piecemeal.

  12. #12
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    But that link says it's an add-in, not a code?

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    Okay!
    When you click the link, click again the excel tips tab, then 3rd from the list(Topics)-choose copy/paste/merge examples

  14. #14
    Registered User
    Join Date
    07-10-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Multitude of random errors when cycling through workbooks in a folder to copy a range

    I just got the code running actually! VBA didn't like that I had

    
    workbooks(variable).open
    When the variable was a workbook, and not a name.

    Changing it to

    
    variable.open
    in all such places fixed the problem, along with a few other minor changes.

+ 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. create a compressed folder or zipped folder then copy workbooks to it.
    By Ironman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-04-2012, 03:56 PM
  2. cycling through all sub-folders in a folder
    By dyesol in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2011, 10:31 AM
  3. Replies: 3
    Last Post: 02-06-2011, 05:29 AM
  4. Cycling through files in a folder
    By KateMolloy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-28-2009, 04:18 PM
  5. Cycling through all worbooks in a folder
    By Ben in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-15-2006, 06:40 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