Excel VBA: create sheet template

Hello Experts,
I record and customise the following procedure in order to add a specific sheet template to my personal.xlsb.
I was wondering if you can help me to clean the code and remove unnecessary lines.
Sub SheetTemplate()
    With ThisWorkbook
        Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1
        ActiveSheet.Name = ActiveSheet.Name & "-Template"
    End With
    Range("A1").Select
    ActiveWindow.DisplayGridlines = False
    Rows("1:1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Columns("A:I").Select
    Selection.ColumnWidth = 13
    Rows("2:2").Select
    ActiveWindow.FreezePanes = True
    Range("A1").Select
    Rows("1:1").RowHeight = 51
    Range("A1").Select
    Range("A1").Value = "Header A1"
    Range("B1").Value = "Header B1"
    Range("C1").Value = "Header C1"
    Range("D1").Value = "Header D1"
    Rows("1:1").Select
    Selection.Font.Bold = True
    End Sub

Open in new window

I attached example file.
If you have questions. Please contact me
Sheet-Template-Creation-10092018.xlsm
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
It's not clear what you are asking.

Usually a template is created manually then saved. Something like this attached example
Basic-Invoice.xls
0
LD16Author Commented:
Hello Roy,

Thank you for your comment.
My goal is to create a worksheet template with specific format (fill color, freeze etc,) in order to reuse it whenever I want instead of creating manually the format.
I have already the specific format as reported above.
I just want to clean the code as it has been generated with vba recorder.
0
Roy CoxGroup Finance ManagerCommented:
You don't need to run all that code each time. Formatting code is notoriously slow.  Format a sheet exactly how you want it and save.

Is this to be a workbook template or is it a sheet to use several times in one workbook like the example that I posted.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

LD16Author Commented:
Yes, you are right, however I would like to add this to my personal macro so with one shortcut key I can generate the sheet instead of opening the template workbook and save it with another name. The goal is to generate the template in any workbook this is why I want to add it as a personal macro. I already have the procedure I just want to clean up all unecesarry lines.
0
Roy CoxGroup Finance ManagerCommented:
You could keep the template sheet in the personal xlsb then simply use code to copy to the activeworkbook
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
This is the only code that you will need if you follow my suggestion.

Sub CreateCopy()
''/// copies template stored in PERSONAL.xlsb
    ThisWorkbook.Sheets("Template").Copy after:=ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)
    ActiveSheet.Name = ActiveSheet.Name & "-Template"
End Sub

Open in new window

0
LD16Author Commented:
Ok, thank you.
I was able to clean up the code. And this is my final result:

Sub Create_Sheet_Template()
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
    'Add new Sheet with "template" string
    With wb
    Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=1
    ActiveSheet.Name = ActiveSheet.Name & "-Template"
    End With
Set ws = ActiveSheet
    With ws
    'Remove gridlines
    ActiveWindow.DisplayGridlines = False
        'Apply Borders
        With .Range("A1:T400").Borders
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThin
        End With
        'Set columns width
        With .Columns("A:T")
        .ColumnWidth = 14
        End With
        With .Rows("1:1")
            'Fill first row with green
            With .Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 5287936
            .TintAndShade = 0
            .PatternTintAndShade = 0
            End With
            With .Font
            'Set font bold
            .Bold = True
            'Set font color white
            .ColorIndex = 2
            End With
            'Set row height
            .RowHeight = 51
        End With
        'Freeze pane as of first row
        With .Rows("2:2").Select
        ActiveWindow.FreezePanes = True
        End With
    End With
End Sub

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
Ignore good advice again
0
LD16Author Commented:
Both solutions works. Thank you for your help.
0
Roy CoxGroup Finance ManagerCommented:
I appreciate yours works but my suggestion will be a lot simpler. Anyway so long as you have it working.

By the way, your code won't work as you expect. The line below will refer to the PERSONAL.xlsb because it contains the code. You need to use ActiveWorkBook, ThisWorkBook will always refer to the workbook that actually holds the code.

Set wb = ThisWorkbook

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.