Can't figure out how to limit a header to one worksheet in a macro

TooZippy

Board Regular
Joined
Dec 30, 2018
Messages
70
I don't know that much about writing macro codes. I got this code on the internet, and it puts the header in all of my worksheets. I only want to put it in one "Enter Header Here". I see where in the code it is referencing the entire workbook, but I don't know what to put in to limit to the one worksheet. Can you help please?

Thank you,

toozippy

Sub AddHeaders()

Application.CutCopyMode = True

Dim Counter As Long

Counter = Sheets.Count
For i = 1 To Counter
Sheets("Enter Header Here").Cells(1,1).EntireRow.Copy
Sheets(i).Cells(1, 1).PasteSpecial

Next i

Application.CutCopyMode = False
Dim headers() As Variant
Dim ws As Worksheet
Dim wb As Workbook

Application.ScreenUpdating = False 'turn this offfor the macro to run a little faster

Set wb = ActiveWorkbook

headers() = Array("X1", "X2","X3", "X4", "X5", "X6", _
"X7", "X8","X9", "X10", "X11", "X12")
For Each ws In wb.Sheets
With ws
.Rows(1).Value = "" 'Thiswill clear out row 1
For i = LBound(headers()) ToUBound(headers())
.Cells(1, 1 + i).Value= headers(i)
Next i
.Rows(1).Font.Bold = True
End With
Next ws

<strike></strike>
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
.
First .. when posting your code always paste it between the POUND SYMBOL signs. It is located in the top menu of the QUICK REPLY window after you click REPLY TO THREAD.
Aside from being a Forum rule, it makes reading code so much easier. Thanks. :)


Code:
Sub AddHeaders()


Dim headers() As Variant
Dim ws As Worksheet
Dim wb As Workbook
Dim i As Integer


Application.ScreenUpdating = False 'turn this offfor the macro to run a little faster


headers() = Array("X1", "X2", "X3", "X4", "X5", "X6", _
"X7", "X8", "X9", "X10", "X11", "X12")
    
    With Sheet1
        .Rows(1).Value = "" 'Thiswill clear out row 1
        For i = LBound(headers()) To UBound(headers())
            .Cells(1, 1 + i).Value = headers(i)
        Next i
        .Rows(1).Font.Bold = True
    End With


Application.ScreenUpdating = True
End Sub
 
Upvote 0
.
First .. when posting your code always paste it between the POUND SYMBOL signs. It is located in the top menu of the QUICK REPLY window after you click REPLY TO THREAD.
Aside from being a Forum rule, it makes reading code so much easier. Thanks. :)


Code:
Sub AddHeaders()


Dim headers() As Variant
Dim ws As Worksheet
Dim wb As Workbook
Dim i As Integer


Application.ScreenUpdating = False 'turn this offfor the macro to run a little faster


headers() = Array("X1", "X2", "X3", "X4", "X5", "X6", _
"X7", "X8", "X9", "X10", "X11", "X12")
    
    With Sheet1
        .Rows(1).Value = "" 'Thiswill clear out row 1
        For i = LBound(headers()) To UBound(headers())
            .Cells(1, 1 + i).Value = headers(i)
        Next i
        .Rows(1).Font.Bold = True
    End With


Application.ScreenUpdating = True
End Sub
Thank you for the help! I will give it a try. I looks like it should work. To insert the code into a message for next time, does the button say "Wrap
Code:
 tags around selected text"?
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top