Home > Language Reference > Excel Functions > WriteExcel

WriteExcel

The WriteExcel function writes a cell, a row or a column in the specified Excel workbook. Microsoft Excel must be installed on the PC, but does not need to be opened. The workbook must exist but MUST NOT be opened.

Usage

Used to populate columns or lines of data in an Excel file, those data can be captured on a Web site, or copied from a Windows application. As the Excel file must exist, you can use CreateExcelFile function to create it.

Syntax

ret = WriteExcel(<Excel_filename>,<range_identifier>,<tab_data [<read_password>] [,<write_password>)

Parameters

<Excel_filename>, string, Excel filename to write to. If the path is not specified in <Excel_filename>, the file is searched in the current working folder.

<range_identifier>, string. It follows the syntax:
letter for the colum concatenated to the row number, then a : character, then letter for the column concatenated to the row number. Examples:
A4:F4 for row 4, columns A to F
A2:A12 for column A, rows 2 to 12.
If multiple sheets exist in the workbook, the required syntax is "sheet_name!range_of_cells" ; for example:
TOOLS!A9:F9
Other range examples: A1, A3:B3, tools!$b$2:$b$4

<tab_data>, array of strings, variable only. It contains the values to write to Excel. The array must be declared at the beginning of the script using Dim statement. First element of the array starts at 0.
 
<read_password>, optional string, read password if <Excel_filename> is password protected.

<write_password>, optional string, write password if <Excel_filename> is password write protected.

Return value

ret, optional numeric return value. If the function succeeds, the return value is the number of written cells. If the function fails, the return value is a nonzero value: -1 if an error is reported by Excel or the range is not a single cell, a column or a row ; -3 if the Excel file is opened (WriteExcel cannot write in an opened Excel file) - (see File Functions Error Codes). WriteExcel function tests if the Excel file is writable for about 5 seconds. If it cannot be written within this time, an error is issued

See also

CloseExcelCom
CreateExcelFile
ExecExcelMacro
ReadExcel

Examples

dim array$(20)
'fill array$ and then
ret=WriteExcel("C:\My documents\myexcel_file.xls","TOOLS!A9:F9",array$())



'The script below reads all the lines from an Excel file from A column to C column and writes them in another Excel file.
Dim arrayline$(10)
fileexcel$="c:\wttest\example1.xlsx"
filetowritein$="c:\wttest\test.xlsx"

j=1
repeat
' The complex string "sheet1!"+"a"+str$(j)+":c"+str$(j) means if j=1
' "Sheet1!a1:c1"
readexcel(fileexcel$,"sheet1!"+"a"+str$(j)+":c"+str$(j),arrayline$())
WriteExcel(filetowritein$,"sheet1!"+"a"+str$(j)+":c"+str$(j),arrayline$())
j=j+1
until arrayline$(0)=""