bsharath
asked on
Need a excel macro to copy headers
Hi,
I need a way to copy the headers alone to new sheets in an excel.Only the headers has to be copied to new sheets .
Regards
Sharath
I need a way to copy the headers alone to new sheets in an excel.Only the headers has to be copied to new sheets .
Regards
Sharath
Add this code to the ThisWorkbook code module. It will copy the header row (row 1) to the new sheet from the last active sheet whenever a new sheet is inserted.
Option Explicit
Private mLastActiveSheet As Object
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.OnTime Now(), "ThisWorkbook.Workbook_Aft erNewSheet "
End Sub
Private Sub Workbook_SheetDeactivate(B yVal Sh As Object)
Set mLastActiveSheet = Sh
End Sub
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = mLastActiveSheet.Rows(1).V alue
End Sub
Kevin
Option Explicit
Private mLastActiveSheet As Object
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.OnTime Now(), "ThisWorkbook.Workbook_Aft
End Sub
Private Sub Workbook_SheetDeactivate(B
Set mLastActiveSheet = Sh
End Sub
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = mLastActiveSheet.Rows(1).V
End Sub
Kevin
ASKER
This copies only to sheet 2 i want it to copy to all sheets and to any new sheet inserted too.Sheet name can be any name but the headers should be copied.
This macro will copy the header from the active sheet to all other sheets.
Public Sub CopyHeaders()
Dim Worksheet As Worksheet
For Each Worksheet In Worksheets
If Worksheet.Name <> ActiveSheet.Name Then
Worksheet.Rows(1).Value = ActiveSheet.Rows(1).Value
End If
Next Worksheet
End Sub
Kevin
Public Sub CopyHeaders()
Dim Worksheet As Worksheet
For Each Worksheet In Worksheets
If Worksheet.Name <> ActiveSheet.Name Then
Worksheet.Rows(1).Value = ActiveSheet.Rows(1).Value
End If
Next Worksheet
End Sub
Kevin
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R ows(1).Val ue
End Sub
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R
End Sub
' insert this in the ThisWorkBook module
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R ows(1).Val ue
End Sub
Public Sub aCopy()
For i=2 to ActiveWorkBook.Sheets.Coun t
ActiveWorkBook.Sheets(1).R ows(1).Val ue = ActiveSheet.Sheets(1).Rows (1).Value
Next
End Sub
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R
End Sub
Public Sub aCopy()
For i=2 to ActiveWorkBook.Sheets.Coun
ActiveWorkBook.Sheets(1).R
Next
End Sub
Hitesh,
You sure you know what you are doing there buddy?
You might want to take a closer look at your code. Maybe stop copying mine unless you know how it works.
Kevin
You sure you know what you are doing there buddy?
You might want to take a closer look at your code. Maybe stop copying mine unless you know how it works.
Kevin
ASKER
I get this.
Run time error 91
Run time error 91
With what code?
Kevin
Kevin
hey kevin i know exactly what i am doing and i am not copying your code
Really? How does this routine work and when is it called:
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R ows(1).Val ue
End Sub
Kevin
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkBook.Sheets(1).R
End Sub
Kevin
i made a mistake in the previous code sharath here you go
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkbook.Sheets(1).R ows(1).Val ue
End Sub
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun t
ActiveWorkbook.Sheets(i).R ows(1) = ActiveWorkbook.Sheets(1).R ows(1)
Next
End Sub
Private Sub Workbook_AfterNewSheet()
ActiveSheet.Rows(1).Value = ActiveWorkbook.Sheets(1).R
End Sub
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun
ActiveWorkbook.Sheets(i).R
Next
End Sub
o yes kevin i know you are a genius
here the final macro
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun t
ActiveWorkbook.Sheets(1).R ows(1) = ActiveWorkbook.Sheets(1).R ows(1)
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R ows(1).Val ue
End Sub
here the final macro
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun
ActiveWorkbook.Sheets(1).R
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R
End Sub
ASKER
Hitesh,
I used this code.
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun t
ActiveWorkbook.Sheets(1).R ows(1) = ActiveWorkbook.Sheets(1).R ows(1)
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R ows(1).Val ue
End Sub
But the headers what are in sheet 1 are disapearing....
I used this code.
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun
ActiveWorkbook.Sheets(1).R
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R
End Sub
But the headers what are in sheet 1 are disapearing....
sorry made another error
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun t
ActiveWorkbook.Sheets(i).R ows(1) = ActiveWorkbook.Sheets(1).R ows(1)
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R ows(1).Val ue
End Sub
Public Sub aCopy()
For i = 2 To ActiveWorkbook.Sheets.Coun
ActiveWorkbook.Sheets(i).R
Next
End Sub
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sh.Rows(1).Value = ActiveWorkbook.Sheets(1).R
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hitesh
Still not coping the header from sheet 1 to sheet 2 and sheet 3.When i create a new sheet also this does not copy after running the macro.
Still not coping the header from sheet 1 to sheet 2 and sheet 3.When i create a new sheet also this does not copy after running the macro.
there the deserving kevin gets the points :),
Sheet1.Rows(1).Copy Destination:= Sheet2.Rows(1)
End Sub