PDA

View Full Version : Solved: Code Running Sequentially



jo15765
12-09-2011, 08:24 AM
I posted here, and had help modifying my code to custom my needs
http://www.vbaexpress.com/forum/showthread.php?t=40113


I just tested it extensively and realize I need one more mod...
The way I have it set currently is to run two separate functions (adding in the workbooks.Add will fix my problem, if we can figure out what that debug error means)

Function 1()

Workbooks.Add

Dim Varbooks As Variant
Dim wb As Workbook, wbFinal As Workbook
Dim i As Long
Const Path As String = "Location of workbooks"

Varbooks = Array("Name of Workbooks)

Set wbFinal = ActiveWorkbook
Application.EnableEvents = False
For i = LBound(Varbooks, 1) To UBound(Varbooks, 1)
Set wb = Workbooks.Open(Path & Varbooks(i))
wb.Worksheets(2).Copy wbFinal.Worksheets(1)
wb.Close False
Next

wb.Sheets("Sheet1").Select
wb.SelectedSheets.Delete

Set wb = ActiveWorkbook
wb.SaveAs Filename:="location to save" & "Filename"
wb.Close
End Function

Function 2()

Workbooks.Add

Dim Varbooks As Variant
Dim wb As Workbook, wbFinal As Workbook
Dim i As Long
Const Path As String = "Location of workbooks"

Varbooks = Array("Workbook Names")

Set wbFinal = ActiveWorkbook
Application.EnableEvents = False
For i = LBound(Varbooks, 1) To UBound(Varbooks, 1)
Set wb = Workbooks.Open(Path & Varbooks(i))
wb.Worksheets(2).Copy wbFinal.Worksheets(1)
wb.Close False
Next

wb.Sheets("Sheet1").Select
wb.SelectedSheets.Delete

Set wb = ActiveWorkbook
wb.SaveAs Filename:="File Name" & "Location"
wb.Close

End Function

Which throws a VBA error of
System Error &H800401A8 (-2147221080)

jo15765
12-09-2011, 09:28 AM
Ignore my stupidity, all I had to do was modify the delete line of code to this:

Activeworkbook.Sheets("Sheet1").Delete