Issue copying sheets in excel - excel

i am writting a code to copy a sheet in excel from one workbook to another and I'm having the following issue :
Set xlsAppSource = CreateObject("Excel.Application")
Set xlsWBSource = xlsAppSource.Workbooks.Open(strWBSource)
Set xlsAppCible = CreateObject("Excel.Application")
Set xlsWBCible = xlsAppCible.Workbooks.Open(strWBCible)
xlsWBSource.Sheets(1).Copy before:= xlsWBSource.Sheets(1)
And I get ERROR 1004.
I know the error comes from :
xlsWBSource.Sheets(1) and xlsWBSource.Sheets(1)
because when change the last statement to :
xlsWBSource.activate
ActiveWorkbook.Sheets(strSheetSource).Copy before:= [any working sheet]
it works.
But As I cannot activate the 2 workbooks it's not a way to solve my problem.
Any ideas why it doesnt work ?
thanks

If you are tring to copy from from xlsWBSource to xlsWBCible then you should only use one instance of Excel , you are currently opening two separate versions with your two CreateObjects - the two separate instances can't "talk", hence the error
Whereas this will work:
Set xlsAppSource = CreateObject("Excel.Application")
Set xlsWBSource = xlsAppSource.Workbooks.Open(strWbsource)
Set xlsWBcible = xlsAppSource.Workbooks.Open(strWBcible)
xlsWBSource.Sheets(1).Copy before:=xlsWBcible.Sheets(1)

Related

VBA - Using Average function in Sheets.Range

I've tried searching a few different threads but wasn't able to get a solution.
My macro is going to transfer data from one sheet to another. I need to use the AVERAGE function to get the average of a range (on the other sheet)
I'm getting a RUN TIME ERROR 438 error and can't figure out why. I tried using Application.WorksheetFunction but this doesn't bear the correct result and works on the MAX function but not the AVERAGE. I saw a few solutions that involve creating a loop and variables, but I assumed a simple solution should be possible.
Code below:
Option Explicit
Sub Step8CopytoLean()
Dim wblean As Workbook
Dim wbmaster As Workbook
Set wblean = Workbooks("SLA Reporting Lean.xlsx")
Set wbmaster = Workbooks("SLA Reporting MasterFile.xlsx")
Workbooks("Lean.xlsx").Activate
Worksheets("Data").Delete
Workbooks("MasterFile.xlsx").Activate
Worksheets("Data").Copy After:=Workbooks("Lean.xlsx").Sheets("Summary")
Workbooks("Lean.xlsx").Sheets("Summary").Activate
wblean.Sheets("Summary").Range("E4").Value = wbmaster.Sheets("Summary").Range("K20")
wblean.Sheets("Summary").Range("F4").Value = wbmaster.Sheets("Summary").Range("M20")
wblean.Sheets("Summary").Range("E5:E6").Value = wbmaster.Sheets("Summary").Average(Range("K9:K11")) 'line with error
wblean.Sheets("Summary").Range("F5").Value = wbmaster.Sheets("Summary").Max(Range("M8:M11")) 'line with error
Modify the below and try:
Application.WorksheetFunction.Average(ThisWorkbook.Worksheets("Sheet1").Range("A1:A10"))
Something like this:
wblean.Sheets("Summary").Range("E5:E6").Value = Application.WorksheetFunction.Average(wbmaster.Worksheets("Summary").Range("K9:K11"))
wblean.Sheets("Summary").Range("F5").Value = Application.WorksheetFunction.Max(wbmaster.Worksheets("Summary").Range("M8:M11"))

VBS, when opening an Excel file, changes number format, destroying data

when try to open an excel file using VBScript, the values/numbers format gets change, destryoing the values.
For example: "0.0" is changed to "__0.0" (with spaces befores), "9000.0" is turned into "__9000.0" (with spaces befores) and "288" is replaced by "288000".
Some knows how to solve this?
Here is my code to open the file:
Dim ExcelFile
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set ExcelFile = objExcel.Workbooks.Open("File Directory")
I need the data as original (functional) in order to be used by another file.

SAPgui script with variables values from Excel

I need to populate dates on fields in SAP which if manually entered is properly captured by the script recorder.
Is it possible to update the script dates using a cell link in Excel?
session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = "2"
session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = "2"
session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = "2016"
session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = "29.02.2016"
I plan to copy the recorded SAP script and incorporate it in an Excel macro as a button.
You may try like this:
Set app = CreateObject("Excel.Application")
Set wbook = app.Workbooks.Open("c:\tmp\prices.xls")
set sheet = wbook.Sheets("Tabelle1")
session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = sheet.Cells(1,2).Value
session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = sheet.Cells(1,3).Value
session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = sheet.Cells(1,4).Value
session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = sheet.Cells(1,5).Value
Get App object, then get workbook and worksheet objects, and then assign your script cell values.
Everything is well discussed here including all the comments asking for different scenarios. https://scn.sap.com/thread/1699675

programming for excel with foxpro (or any suggestions)

I have a macro in excel that works.
Sub jim()
Range("$A$1").CurrentRegion.Select
Selection.RowHeight = 13
Selection.Font.Bold = True
…etc.
If I open a csv and run this macro it selects the seen data and then later does stuff to it.
So I made a foxpro program to do the same.
When I try to run in foxpro, I get a variable not found error.
local oExcel, oSheet
oExcel = CreateObject([Excel.Application])
oExcel.Visible = .T.
k:\wellcarestuff\All Data Files\"+ALLTRIM(xlfiles(1,1))
osheet = oexcel.workbooks.open("k:\wellcarestuff\all data files\20131210_CT_TransportationAddresses.csv") <<<=== it opens and shows the sheet here
Range("$A$1").CurrentRegion.Select <<<?=== get an error here?
Is the var it wants the current osheet?
On the line with the path, it looks like you are missing the beginning quote marks so it things that k:... is a variable. Should be:
"k:\wellcarestuff\All Data Files\"+ALLTRIM(xlfiles(1,1))
Try that and see if it works.

How to add more than 3 sheets to an excel workbook from within MATLAB

How do I add more sheets to an excel workbook from within matlab?
I set up the workbook like so (based on code I got from someone else's post in this forum):
%# create Excel COM Server
Excel = actxserver('Excel.Application');
Excel.Visible = true;
%# create new XLS file
wb = Excel.Workbooks.Add();
wsheet=1;
wb.Sheets.Item(wsheet).Activate();
That's fine. Then later on inside the loop I open a new sheet after so many loops:
...
if loop==sheetlimit,
wsheet=wsheet+1;
wb.Sheets.Item(wsheet).Activate();
end
This works up to sheet 3. But when wsheet=4 I get this error message:
??? Invoke Error, Dispatch Exception: Invalid index.
Error in ==> filename at 97
wb.Sheets.Item(wsheet).Activate();
Appreciate any help. Thanks.
I don't know Matlab but I would be surprised if wb.Sheets.Item(wsheet).Activate(); is actually adding any new worksheets. Most likely it is selecting / activating each worksheet in your wb workbook and your default Excel template has three worksheets. Hence why it errors when it gets to more than three.
Something like this might add a new Excel worksheet:
wb.sheets.Add();
Aargh - comment formatting completely messed up - I'll re-enter it as an new answer
Yes wb.sheets.Add(); will work. You can query the available methods of an interface like this:
methods(wb.sheets)
which gives:
Methods for class Interface.000208D7_0000_0000_C000_000000000046:
Add FillAcrossSheets PrintOut addproperty events loadobj set
Copy Item PrintPreview delete get release
Delete Move Select deleteproperty invoke saveobj

Resources