Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VB to create multiple worksheets

Status
Not open for further replies.

bbrendan

IS-IT--Management
Dec 13, 2001
109
GB
Hi,

I have a VB app, which creates multiple workbooks in one folder.

I would like to take each workbook (they ony have one sheet in each) and combine them into one workbook.

So far I have found this code from another site.(
It seems to work fine if I use it in excel, but I would like to use it from VB.

i have the references setup, but when it loops through the first time it then crashes out.

Any ideas? please


----------------------------------------------------------------
Dim oExcel As Excel.Application
Dim basebook As Workbook
Dim mybook As Workbook
Dim i As Long

Set oExcel = New Excel.Application

Application.ScreenUpdating = False
With Application.FileSearch
.NewSearch
.LookIn = "c:\xlsdata" ' This is the folder containing the workbooks
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks 'msoFileTypeExcelWorkbooks

If .Execute() > 0 Then

Set basebook = oExcel.Workbooks.Open("\c:\xlsdata\Summary_AllAreas.xls") 'ThisWorkbook to hold all sheets
For i = 1 To .FoundFiles.Count
Set mybook = Workbooks.Open(.FoundFiles(i))
mybook.Worksheets(1).Copy after:=basebook.Sheets(basebook.Sheets.Count)
ActiveSheet.Name = mybook.Name
mybook.Close
basebook.Close
Next i
End If

End With
Application.ScreenUpdating = True
---------------------------------------------------------------------

thank you
brendan
 
You need to use your top-level Application Object (it's not built into VB like it is excel) to access your objects. You can not say

Code:
Application.ScreenUpdating

In Your instance it would be
Code:
oExcel.ScreenUpdating
With oExcel.FileSearch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top