We use this:
With ActiveWorkbook, "Sheets" set Sheets
Method Sheets, "Add", , ActiveSheet, 1, xlWBATWorksheet
Where the first parameter is not passed (it is optional by design for this method, so we put a comma without value and M2000 interpreter to the job, to pass a specific variant which used for not sending something).
Another way to call this method using named parameters, so now we pass the optional parameter:
Method Sheets, "Add", After:=ActiveSheet, Count:=1, Type:=xlWBATWorksheetNamed parameters my have different order:
Method Sheets, "Add", Count:=1, After:=ActiveSheet, Type:=xlWBATWorksheet
Info: The After and Type identifiers are shown blue, because are known identifiers, they are command, for M2000, so the editor of M2000 show it using blue color, but aren't those commands at this context, they used as names for the parameters.
The code is a variant from the previous post. Here we see that the A3 cell at Name2 WorkSheet has a link to A1 cell at Name1 WorkSheet.
// const xlWBATChart=-4109& //Chart
// const xlWBATExcel4IntlMacroSheet=4& //Excel version 4 macro
// const xlWBATExcel4MacroSheet=3& //Excel version 4 international macro
const xlWBATWorksheet=-4167& //Worksheet
mydir$=path$(5) // Documents folder
target$="TestA.xlsx"
If exist(mydir$+target$) Then
Try ok {
dos "del "+shortdir$(mydir$+target$);
}
If error or not ok Then break
End If
Declare withevents Excel "Excel.Application"
With Excel, "Workbooks" as WorkBooks
Method WorkBooks, "Add", xlWBATWorksheet
With Excel, "ActiveWorkbook" as ActiveWorkbook
With Excel, "ActiveSheet" as ActiveSheet
With ActiveSheet, "name", "Name1"
Try {
With ActiveSheet, "Range" set Range0 ("A1")
Method Range0, "Select"
With Range0, "Value" as ThisValue, "Value" as ThisValue$
ThisValue=1000
With ActiveSheet, "Range" set Range0 ("A2")
ThisValue$="Is a String"
With ActiveSheet, "Range" set Range0 ("A3")
ThisValue$="=A1" // is a formula
}
With ActiveWorkbook, "Sheets" set Sheets
Method Sheets, "Add", , ActiveSheet, 1, xlWBATWorksheet
With ActiveSheet, "name", "Name2"
Try {
With ActiveSheet, "Range" set Range0 ("A1")
Method Range0, "Select"
With Range0, "Value" as ThisValue, "Value" as ThisValue$
ThisValue=2000
With ActiveSheet, "Range" set Range0 ("A2")
ThisValue$="Is a String too"
With ActiveSheet, "Range" set Range0 ("A3")
ThisValue$="=Name1!A1" // is a formula
}
again:
Try ok {
Method ActiveWorkbook, "SaveAs", mydir$+target$, 51
}
If error or not ok Then If ask("File is open, close it and Try again")=1 Then Goto again
Method ActiveWorkbook, "Close", SaveChanges:=True
Method Excel, "Quit"
wait 1000
Declare Excel Nothing
Win "excel",quote$(mydir$+target$)
Print "Done"
Another example (later versions of M2000 can use properties with many indexes like the cells(), and also get string or value)
try {
IF EXIST("MyTest.xlsx") THEN
DOS "DEL "+QUOTE$(DIR$+"MyTest.xlsx"), 300;
END IF
}
IF EXIST("MyTest.xlsx") THEN
PRINT "CLOSE EXCEL FILE..."
BREAK
END IF
DECLARE ExcelSheet "Excel.Sheet"
WITH ExcelSheet, "Application" set ExcelSheet.Application
WITH ExcelSheet.Application, "cells" as cells()
With ExcelSheet.Application, "ActiveSheet" as ActiveSheet
With ActiveSheet, "name", "MySheet1"
' row, line
cells(1,1)="Hello there"
s1=cells(1,1)
print s1, TYPE$(s1)="String"
cells(1,2)=TODAY ' date type
cells(1,3)=NOW ' date type
PRINT cells(1,2), TYPE$((cells(1,2)))="Date" ' need enclosed parenthesis, without these return the object under cells
cells(2,1)=100.3
cells(3, 1)=200.5
cells(4, 1)="=Sum(A2:A3)" ' type
cells(2,2)=1234& ' we place a long type 32 bit, but converted to Double
PRINT cells(4,1)=300.8 ' executed immediate
PRINT TYPE$((cells(4,1)))="Double"
PRINT TYPE$((cells(2,2)))="Double" ' this is the proof of conversion to Double
REM PRINT TYPE$(ExcelSheet.Application)
METHOD ExcelSheet, "SaveAs", dir$+"MyTest.xlsx"
DECLARE ExcelSheet NOTHING
TRY {
WIN DIR$+"MyTest.xlsx"
}