I developed a custom Visual Studio 2015 application where you can browse for excel files in your computer, then append the sheets from the first excel file to the second excel file.
The application works just fine when the first Excel file (the file whose sheets are copied to the second file) has more than one sheet. However, the application hangs when the first file only has one sheet that is trying to be added to the second file. The application works just fine regardless of the number of sheets in the second file.
Can anyone tell me why this is?
Should I use a different loop (e.g. Do Until)?
Here's a link to a photo of my application: Excel Report Merger:
Dim File1 As String = Trim(txtFileDirectory1.Text)
Dim File2 As String = Trim(txtFileDirectory2.Text)
'Opens files selected directories
Dim xlApp As Excel.Application = New Excel.ApplicationClass
Dim xlWorkBook1 As Object = xlApp.Workbooks.Open(File1)
Dim xlWorkBook2 As Object = xlApp.Workbooks.Open(File2)
Dim xlWS As Excel.Worksheet
xlApp.Visible = False
xlApp.DisplayAlerts = True
'Append sheets from first report to second report
Dim i As Integer = 1
For Each xlWS In xlWorkBook1.worksheets
xlWorkBook1.Worksheets(i).Copy(After:=xlWorkBook2.Sheets(xlWorkBook2.worksheets.count))
i = i + 1
Next xlWS
'close first report
xlWorkBook1.close
'show second report with appended workbooks
xlApp.Visible = True