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

This snippet would check each sheet for its name and then enter the column
headings for columns A - D based on the sheet name.

Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
If sh.Name = "name1" Then
For i = 1 To 4 'or ever how many cols
sh.Cells(1, i) = "Col" & i
Next
ElseIf sh.Name = "name2" Then
For i = 1 to 4"
sh.cells(1, i) = "some" & i
Next
End If
Next

You can alter the number of columns and add ElseIf statements for as many
variations as you need.

"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