View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Otto Moehrbach[_2_] Otto Moehrbach[_2_] is offline
external usenet poster
 
Posts: 1,071
Default writing the column names in first row, based on the sheet name

Sheela
Do you have just 2 sheets or a bunch? Are the column headers always some
text followed by a number starting with 1? If so then a For loop
incorporating a Select Case construct would do it. Something like this
perhaps: HTH Otto
Sub Sheela()
Dim sh As Worksheet
Dim TheWord As String
For Each sh In ThisWorkbook.Sheets
Select Case sh.Name
Case "name1": TheWord = "Col"
Case "name2": TheWord = "some"
'etc, etc, etc
End Select
With sh
.Range("A1") = TheWord & "1"
.Range("A1").AutoFill Destination:=.Range("A1:N1")
End With
Next sh
End Sub

"Sheela" wrote in message
...
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