+ Reply to Thread
Results 1 to 2 of 2

Loop Nesting is returning Run-Time Error '-2147221080 (800401a8)': Automation Error

  1. #1
    Registered User
    Join Date
    11-21-2014
    Location
    Dalton, GA
    MS-Off Ver
    2010
    Posts
    13

    Loop Nesting is returning Run-Time Error '-2147221080 (800401a8)': Automation Error

    Hello All,

    I have been studying VBA intently for a few weeks now but needing it for a practical purpose as soon as possible and am needing help. I get several emails per week with Excel files that I save into a particular folder automatically through an Outlook Macro. Once a week I would like to take all of the workbooks and worksheets within them, and save them into a specific single workbook on different worksheets. The problem is when I added in the portion of code that copies and pastes the individual worksheets (Not just the workbooks). I assume it is a problem with my Variables as I only have a basic understand of variables.

    Basically what I need this code to do is:
    1. Allow me to choose which workbooks I want to open and loop through them. (Seems to be working)
    2. Loop through each Worksheet in each Workbook selected (Unsure if Works)
    3. Copy Each worksheet (Cell Data Only), into a new worksheet on my "Master Pull Emailed Files.xlsm" workbook (Does Not Work)
    4. Name Each Worksheet tab with the value of Cell A4 on that particular worksheet. (Optional)


    Here is what I currently have:

    Sub ImportData()

    Dim Filenames As Variant
    Dim i As Integer
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws As Worksheet
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks("Master Pull Emailed Files.xlsm")
    Application.ScreenUpdating = False
    Filenames = Application.GetOpenFilename(Title:="Open File(s)", MultiSelect:=True)

    For i = 1 To UBound(Filenames)
    Workbooks.Open Filenames(i)
    '*************************Line Below is where I am getting the error (on the very first time it runs through)
    For Each ws In wb1.Worksheets
    If ws.Visible = True Then
    ws.Copy After:=wb2.Sheets(wb2.Sheets.Count)
    End If
    Next ws
    Workbooks.Open Filenames(i)
    ActiveWorkbook.Close SaveChanges:=False
    Worksheets.Add

    Next i

    End Sub



    If anyone can explain what I seem to be missing or have mis-recorded, I would love to learn so I can avoid this problem both now and in the future. Thank you.
    Last edited by ChristopherBrandonKi; 11-21-2014 at 02:04 PM. Reason: To point out where the error is occuring

  2. #2
    Registered User
    Join Date
    11-21-2014
    Location
    Dalton, GA
    MS-Off Ver
    2010
    Posts
    13

    Re: Loop Nesting is returning Run-Time Error '-2147221080 (800401a8)': Automation Error

    Never Mind, I fixed my own Problem. it turns out I was qualifying the current worksheet incorrectly when I didn't even need to because the active workbook was being activated by the previous loop. Here is what I did to correct my own problem.



    Sub ImportData()

    Dim Filenames As Variant
    Dim i As Integer
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws As Worksheet
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks("Master Pull Emailed Files.xlsm")
    Application.ScreenUpdating = False
    Filenames = Application.GetOpenFilename(Title:="Open File(s)", MultiSelect:=True)

    For i = 1 To UBound(Filenames)
    Workbooks.Open Filenames(i)
    For Each ws In Worksheets
    If ws.Visible = True Then
    ws.Copy After:=wb2.Sheets(wb2.Sheets.Count)
    End If
    Next ws
    Workbooks.Open Filenames(i)
    ActiveWorkbook.Close SaveChanges:=False

    Next i

    End Sub

+ 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. [SOLVED] Run-time error'-2147221080 (800401a8)': Automation error
    By baig123 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-28-2014, 05:12 AM
  2. Replies: 7
    Last Post: 05-15-2013, 09:02 AM
  3. run-time error ;2147023179 (800706b5) time automation error interface unknown
    By karthik72 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-02-2012, 09:31 AM
  4. -2147221080 800401a8 Automation error
    By JoanExcel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-10-2009, 05:46 AM
  5. [SOLVED] Run-Time Error'-2147221080(800401a8)': Automation Error
    By BEEJAY in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-18-2006, 10:15 AM

Tags for this Thread

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