Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel VBA Copy sheet. Rename it and add it

I have a loop that creates sheets based upon a column.
Now I want to change the loop so that the copied sheet is a ‘template’

This is the current "code" that creates the sheets.

Set wksNew = ActiveWorkbook.Worksheets.Add(After:=Sheets(Sheets.Count))
wksNew.Name = rngCell.Value

All variables are defined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The change would be this:

Set wksNew = ActiveWorkbook.Worksheets("Base").Copy(After:=Sheets(Sheets.Count))
wksNew.Name = rngCell.Value

So I created a "template" with the name "Base"
It only works once then it spawns an error:
Object Required

>Solution :

Copy is a method of WorkSheet object. It doesn’t have return value.

Please ref to Micorsoft document.

https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.copy?WT.mc_id=M365-MVP-33461

Please try it.

Sub demo()
    Dim rngCell As Range
    ' Dim wksNew As Worksheet
    ' Assuming new sheets name is in Sheet1.Range("A1:A3")
    For Each rngCell In Sheet1.Range("A1:A3")
        ActiveWorkbook.Worksheets("Base").Copy after:=Sheets(Sheets.Count)
        ActiveSheet.Name = rngCell.Value
    Next
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading