Forums     Knowledge Base     OfficeWriter Online     
 
 
This documentation is for
OfficeWriter v3.8.1
ASP/COM Platform

View Docs for Another
Version or Platform

ExcelApplication in Depth > Addressing Cells

ExcelApplication is not
available in ExcelWriterSE.

Addressing Cells

How to Address Cells

Cells can be addressed in two ways: by Name or by Number. Accessing by name is easier to read, more intuitive and matches the cell references seen in Microsoft Excel. Accessing by number is significantly faster, and is easier to program if it is necessary to walk a number of cells.

set xlw = Server.CreateObject("SoftArtisans.ExcelWriter")
set cells = xlw.worksheets(1).cells

'--- By name
cells("B3").value = 123

'--- Same cell by number
cells(3, 2).value = 123
Addressing Cells by Name Addressing Cells by Number
Format Cells("ColLetterRowNumber") Cells(row, column)
Example
Cells("D8")
Cells(8, 4)
Advantages Easy to read, intuitive, matches MS Excel cell references Faster, easier to use when iterating over a set of cells

Example 1: Addressing Cells by Name and Number

When referencing a cell by number, the format is cells(row, column). It is easy to mix and match different means of referencing a cell within your application.

<%@ Language=VBScript %>
<% Option Explicit %>
<!-- METADATA TYPE="TypeLib" UUID="{7BCD2133-64A0-4770-843C-090637114583}" -->
<%
Dim oXLW
Dim oCells

'--- Create an instance of ExcelApplication and get a reference
'--- to the first worksheet's cells.
Set oXLW = Server.CreateObject("SoftArtisans.ExcelWriter")
Set oCells = oXLW.Worksheets(1).Cells

'--- Reference a cell by its Excel address.
oCells("B3").value = 123

'--- Reference a cell using row/column addressing.
oCells(7,1).value = 123

oXLW.Save "address1.xls", saOpenInPlace

Set oXLW = Nothing
%>

Example 2: Iterating over Cells

It is much easier and faster to iterate over a set of cells by number.

<%@ Language=VBScript %>
<% Option Explicit %>
<!-- METADATA TYPE="TypeLib" UUID="{7BCD2133-64A0-4770-843C-090637114583}" -->
<%
Dim xlw, cells
Dim row, column, letterA, cellname

set xlw = Server.CreateObject("SoftArtisans.ExcelWriter")
set cells = xlw.worksheets(1).cells

'--- Address cells by number
'---
for row = 1 to 5
  for column = 1 to 5
    cells(row, column).value = CStr(row) & ", " & CStr(column)
  next ' column
next ' row

for row = 1 to 5
  for column = 27 to 31
    cells(row, column).value = CStr(row) & ", " & CStr(column)
  next ' column
next ' row

'--- Address cells by name.
'---
'--- This works only up to column Z. It will not work for column AA.
'---
letterA = Asc("A") ' ASCII numerical value of the letter 'A'
for row = 7 to 12
  for column = 1 to 5
    cellname = Chr(column + letterA - 1) & CStr(row)
    cells(CStr(cellname)).value = cellname
  next ' column
next ' row

'--- This is more complicated but works for any column
'---
for row = 7 to 12
  for column = 27 to 31 
    cellname = ""
    if (column mod 26) <> 0 then
      if (column \ 26) > 0 then cellname = Chr((column \ 26) + letterA - 1)
      cellname = cellname & Chr((column Mod 26) + letterA - 1) & CStr(row)
    else
      cellname = chr(column + letterA - 1) & cStr(row)
    end if  
    cells(CStr(cellname)).value = cellname
  next ' column
next ' row

for row = 14 to 18
  for column = 1 to 5
    cellname = ""
    if (column mod 26) <> 0 then
      if (column \ 26) > 0 then cellname = Chr((column \ 26) + letterA - 1)
      cellname = cellname & Chr((column Mod 26) + letterA - 1) & CStr(row)
    else
      cellname = chr(column + letterA - 1) & cStr(row)
    end if
    cells(CStr(cellname)).value = cellname
  next ' column
next ' row

'--- For a specific cell, addressing by name is easier
'---
cells("G1").Value = "Please scroll to cell AA1->>>"
cells("G1").Format.Font.Bold = true

xlw.Save "address2.xls", saOpenInPlace

Set xlw = nothing
Response.end
%>

Section Summary

  1. Cells can be referenced by name or by number.

  2. Accessing by name is easier to read, more intuitive and matches the cell references seen in Microsoft Excel.

  3. Accessing by number is significantly faster, and is easier to program if it is necessary to walk a number of cells.



Copyright 2007 © SoftArtisans, Inc. All Rights Reserved.