View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Barb Reinhardt Barb Reinhardt is offline
external usenet poster
 
Posts: 3,355
Default writing the column names in first row, based on the sheet name

Dim WS as excel.worksheet


for each WS in ThisWorkbook.worksheets
mytext = ""
if ws.name = "name1" then
myText = "Col"
elseif WS.name = "name2" then
myText = "some"
end if

if not mytext = "" then
for i = 1 to 10
ws.cells(1,i).value = mytext & i
next i
end if
Next WS

Select case would also work if you want to use that.

HTH,
Barb Reinhardt

"Sheela" wrote:

I am new to programming. This site has been very helpful for me. I am
learning as I am working.
I would need to write a macro to name columns based on the sheet name.
This code will run through all the sheets in the workbook and fill the first
row, based on the sheet name.

If sheetname name is €œname1€ then column names are ( €œCol1€, €œCol2€, €œCol3€)
( these will be written in the first row)
Else if sheetname is €œname2€ then column names are (€œsome1€ €œsome2€)

Could someone please give me a sample template of code. I am thinking of
select case statement. But do not know how to write this in VBA code.

Thank you very much for you help in advance

sheela