schnabs
Technical User
- Jan 21, 2009
- 50
Hey everyone,
I am using some code to combine two workbooks in Excel into one. I am using the DIR command to run through directories of the original files. My problem is, the first Dir seems to work, but the second one doesn't. My combined file has each of the first directories file, but only the first of the second directory.
Here is my code:
Public Sub button1_click() Handles Button1.Click
Dim oxl As Microsoft.Office.Interop.Excel.Application
Dim oxlBook1 As Microsoft.Office.Interop.Excel.Workbook
Dim oxlBook2 As Microsoft.Office.Interop.Excel.Workbook
Dim newName As String
'Dim summary As String
'Dim detailed As String
Dim path As String
Dim path2 As String
path = Dir("Z:\2009 Combined Delinquencies\March\")
path2 = Dir("Z:\2009 Combined Delinquencies\Detailed\")
Do While path <> ""
newName = "Z:\2009 Combined Delinquencies\" + path
oxl = New Microsoft.Office.Interop.Excel.Application
oxlBook1 = oxl.Workbooks.Open("Z:\2009 Combined Delinquencies\March\" + path)
'For i = 2
oxlBook2 = oxl.Workbooks.Open("Z:\2009 Combined Delinquencies\Detailed\" + path2)
oxlBook2.Sheets().Copy(After:=oxlBook1.Sheets(oxlBook1.Sheets.Count))
oxlBook2.Close(False)
'Next
oxlBook2 = Nothing
oxlBook1.SaveAs(newName)
oxlBook1.Close()
oxlBook1 = Nothing
oxl = Nothing
path = Dir()
Loop
oxl.Quit()
End Sub
I swear it worked before, but I haven't run it in a while. I cannot figure out why path2 is not moving on to the next file.
Thanks in advance.
I am using some code to combine two workbooks in Excel into one. I am using the DIR command to run through directories of the original files. My problem is, the first Dir seems to work, but the second one doesn't. My combined file has each of the first directories file, but only the first of the second directory.
Here is my code:
Public Sub button1_click() Handles Button1.Click
Dim oxl As Microsoft.Office.Interop.Excel.Application
Dim oxlBook1 As Microsoft.Office.Interop.Excel.Workbook
Dim oxlBook2 As Microsoft.Office.Interop.Excel.Workbook
Dim newName As String
'Dim summary As String
'Dim detailed As String
Dim path As String
Dim path2 As String
path = Dir("Z:\2009 Combined Delinquencies\March\")
path2 = Dir("Z:\2009 Combined Delinquencies\Detailed\")
Do While path <> ""
newName = "Z:\2009 Combined Delinquencies\" + path
oxl = New Microsoft.Office.Interop.Excel.Application
oxlBook1 = oxl.Workbooks.Open("Z:\2009 Combined Delinquencies\March\" + path)
'For i = 2
oxlBook2 = oxl.Workbooks.Open("Z:\2009 Combined Delinquencies\Detailed\" + path2)
oxlBook2.Sheets().Copy(After:=oxlBook1.Sheets(oxlBook1.Sheets.Count))
oxlBook2.Close(False)
'Next
oxlBook2 = Nothing
oxlBook1.SaveAs(newName)
oxlBook1.Close()
oxlBook1 = Nothing
oxl = Nothing
path = Dir()
Loop
oxl.Quit()
End Sub
I swear it worked before, but I haven't run it in a while. I cannot figure out why path2 is not moving on to the next file.
Thanks in advance.