Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding excel columns 3

Status
Not open for further replies.

LindaRichard

Programmer
Feb 23, 2001
103
CA
Hi,

I have some code that adds columns to an excel sheet.
My problem is when I go behond the Z column, I get an error.
I know that Excel numbers form A...Z the AA AB etc...
My question is does anyone know of a different way of accessing each
column and avoiding writing a complex string building expression
Here is my code.

Thanks for your help
Linda

*----------------------------------------------------------------
*create excel object
*----------------------------------------------------------------
goExcel = CREATEOBJECT("Excel.application")
*----------------------------------------------------------------
*open a workbook
*----------------------------------------------------------------
WITH goExcel
*add workbook sheets (this adds 3 sheets by default)
.Application.Workbooks.Add
*delete the other 2 sheets don't need them
*turn the warning message off
.APPLICATION.DisplayAlerts = 0
*delete sheet2
.Worksheets("Sheet2").DELETE
*delete sheet3
.Worksheets("Sheet3").DELETE
*turn the warning message on again
.APPLICATION.DisplayAlerts = 1
*make the first sheet the active one
.Application.Worksheets("Sheet1").Activate && Select sheet
giLineCount=1
*print the field titles
FOR t=1 TO giSelectedNumber
*print the field label
.RANGE(CHR(96+t)+ALLTRIM(STR(giLineCount))).VALUE=gaSelectedFieldName(t)
NEXT t
ENDWITH
 
LindaRichard

Where in your code are you adding a column as the title of your thread suggests?
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
The string building routine isn't all that complicated:
Code:
PROC ExcelCol( pnCol )
LOCAL lnCol,lcCol
lnCol = pnCol
lcCol = ''
do while lnCol>0
  lcCol = chr(asc('A')+ (lnCol-1)%26 ) + lcCol
  lnCol = int( (lnCol-1) / 26 )
enddo
RETURN lcCol
 
Thanks for your response,

I gave your code a try and it works great.

Linda
 
in place of

.RANGE(CHR(96+t)+ALLTRIM(STR(giLineCount))).VALUE=gaSelectedFieldName(t)

use

Code:
      .Cells(lnRowNumber,lnColumnNum).value=gaSelectedFieldName(t)

where lnRownumber,lncolumnNumber are numeric values
 
rusu,

Thanks for the tip, the cells property works great.

Linda
 
Linda,

One thing I realized is your assumption that adding a new workbook will always add 3 sheets. But you can actually set it between 1 to 255 if you want.
In Excel goto :
Tools -> Options -> General -> Sheets in new workbook

I was wondering if it was set something other than 3 sheets in new workbook will your code generate a error ? Probably you would like to check on it.

 
BTW.. you can use "SheetsInNewWorkbook" property to check it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top