Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
srxr9
Starting Member
15 Posts |
Posted - 2006-10-02 : 23:46:46
|
I need to combine two workbooks into one workbook with two worksheets.For example, if I have one workbook with a worksheet called "Test1" and another workbook with worksheet "Test2", I need to combine these two workbooks into one workbook with two worksheets "Test1" & "Test2".Any advice on this will be helpful.Thankssuresh |
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2006-10-03 : 01:27:44
|
huh?is this an excel question?EDIT:if it is, just right-click the worksheet and select 'Move or Copy'. Then select the workbook from the dropdown menu. btw, both workbooks should be open when doing this.-ecfirst post for ExcelTeam... |
 |
|
srxr9
Starting Member
15 Posts |
Posted - 2006-10-03 : 09:34:16
|
I need to get this done in VBThanksSuresh |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-03 : 09:36:59
|
A valuable tip.1) Start recording a MACRO2) Do everything needed, manually, to do the transition3) Stop the recording4) Copy and paste the code from the VBA code window to your VB application5) Rewrite the code so that is fits the platform in VBThere you have it.Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-03 : 09:42:46
|
If you still can't figure it out Windows("Book1").Activate Sheets("Sheet1").Select Sheets("Sheet1").Copy After:=Workbooks("Book3").Sheets(1) Windows("Book2").Activate Sheets("Sheet2").Select Sheets("Sheet2").Copy After:=Workbooks("Book3").Sheets(2) Peter LarssonHelsingborg, Sweden |
 |
|
srxr9
Starting Member
15 Posts |
Posted - 2006-10-03 : 12:16:24
|
Thanks Peter.Here is the Code I use to combine workbooks.Note: TargetWorkbook has initially a worksheet "Sheet1"But the code fails in the Copy After statementFunction Main()Dim Excel_Application, Source_WorkBook, Target_WorkBook, Source_WorkSheetDim SourcePathName, TargetFileName, SheetName, TargetSheetDim ArrayVariable, ISourcePathName = "C:\Documents and Settings\Excel_Test\" TargetFileName = "C:\Documents and Settings\Excel_Test\Test3.xls" Set Excel_Application = CreateObject("Excel.Application")Set Target_WorkBook = Excel_Application.Workbooks.Open(TargetFileName)Set TargetSheet = "Sheet1"ArrayVariable = ARRAY("Test1.XLS", "Test2.XLS")FOR I = LBOUND(ArrayVariable) TO UBOUND(ArrayVariable) Set Source_WorkBook = Excel_Application.Workbooks.Open(SourcePathName & ArrayVariable(I)) For Each Source_WorkSheet in Excel_WorkBook.WorkSheets Sheetname = Source_Worksheet.Name MsgBox (SheetName) Source_WorkBook.Worksheets(SheetName).COPY AFTER:= Target_WorkBook.Worksheets(TargetSheet) TargetSheet = SheetName Next Source_WorkBook.CLOSE NEXT Target_WorkBook.CLOSE Set Source_WorkBook = NothingSet Target_WorkBook = NothingExcel_Application.QuitSet Excel_Application = Nothing Main = DTSTaskExecResult_SuccessEnd FunctionWhat is wrong in the code?ThanksSuresh |
 |
|
srxr9
Starting Member
15 Posts |
Posted - 2006-10-03 : 15:42:23
|
Is there an syntax error in the "Copy after" statement in the code below.I ran this code as a Excel macro and it worked fine, but when I execute this as an ActiveX script in DTS Package it fails.Error Description: Statement Expected on line (line where copy after statement is)FUNCTION Main()DIM Excel_Application, Source_WorkBook, Target_WorkBook, Source_WorkSheetDIM SourcePathName, TargetFileName, SheetNameDIM ArrayVariable, ISourcePathName = "C:\Documents and Settings\SRamanathan\Excel_Test\" TargetFileName = "C:\Documents and Settings\SRamanathan\Excel_Test\Test3.xls" SET Excel_Application = CreateObject("Excel.Application")ArrayVariable = ARRAY("Test1.xls", "Test2.xls")FOR I = LBOUND(ArrayVariable) TO UBOUND(ArrayVariable) SET Target_WorkBook = Excel_Application.Workbooks.OPEN(TargetFileName) SET Source_WorkBook = Excel_Application.Workbooks.Open(SourcePathName & ArrayVariable(I)) For Each Source_WorkSheet in Source_WorkBook.WorkSheets Sheetname = Source_Worksheet.NAME Excel_Application.DisplayAlerts = False Source_WorkBook.Worksheets(SheetName).COPY AFTER:= Target_WorkBook.Worksheets(Target_WorkBook.Worksheets.Count) Target_WorkBook.Save Excel_Application.DisplayAlerts = True Target_WorkBook.CLOSE NEXT Source_WorkBook.CLOSE NEXT SET Source_WorkBook = NothingSET Target_WorkBook = NothingExcel_Application.QuitSET Excel_Application = Nothing Main = DTSTaskExecResult_SuccessEND FUNCTIONThanksSuresh |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-10-03 : 16:01:28
|
named arguments are probably not supported in VBScript. Remember that vb6 <> vbscript <> vba <> vb.net- Jeff |
 |
|
|
|
|
|
|