Copy and paste EVERYTHING from a template using vba?

Dan91

New Member
Joined
Jan 12, 2023
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
Hi
im having some problems copy / paste from one sheet to another.
I have is a 'home' sheet and a 'Base_sheet' sheet. What i wish to do is click a button on the 'home' sheet, and it will copy everything from the 'Base_Sheet' , create a new sheet and paste all the data on the new sheet. it also needs to paste the buttons, formulas. literally copy and paste everything. i have a popup box asking what to call the new sheet.

here is the code i currently have and its not working. any help would be greatly appreciated.

VBA Code:
Private Sub NewStudent_Click()

  
   ' Declare variables
    Dim sheetName As String
    Dim newSheet As Worksheet
    Dim baseSheet As Object ' Use Object type to handle both worksheet and module sheets
    Dim baseCodeModule As Object ' Use Object type to handle both worksheet and module sheets
    Dim newCodeModule As Object ' Use Object type to handle both worksheet and module sheets
    Dim codeLines As Variant
    
    Set baseSheet = ThisWorkbook.Sheets("Base_Sheet")
    
    ' Check if the sheet is a worksheet or a module sheet
    If TypeName(baseSheet) = "Worksheet" Then
        ' The sheet is a worksheet
  [COLOR=rgb(250, 197, 28)]      Set baseCodeModule = baseSheet.CodeModule[/COLOR]
    Else
        ' The sheet is a module sheet
        Set baseCodeModule = baseSheet.VBProject.VBComponents(baseSheet.Name).CodeModule
    End If
    
    ' Show popup box to enter sheet name
    sheetName = InputBox("Enter the name of the new sheet:")
    
    ' Check if sheet name is not empty
    If sheetName <> "" Then
        ' Create new sheet
        Set newSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        newSheet.Name = sheetName
        
        ' Copy data and objects from base sheet to new sheet
        baseSheet.Cells.Copy Destination:=newSheet.Cells
        
        ' Loop through all the shapes on the base sheet and copy them to the new sheet
        Dim i As Integer
        i = 1
        For Each sh In baseSheet.Shapes
            sh.Copy
            newSheet.Paste
            newSheet.Shapes(newSheet.Shapes.Count).Top = baseSheet.Shapes(i).Top
            newSheet.Shapes(newSheet.Shapes.Count).Left = baseSheet.Shapes(i).Left
            i = i + 1
        Next sh
        
        ' Place sheet name in cell A3
        newSheet.Range("A3").Value = sheetName
        
        ' Copy VBA code from base sheet to new sheet
        codeLines = baseCodeModule.Lines(1, baseCodeModule.CountOfLines)
        Set newCodeModule = ThisWorkbook.VBProject.VBComponents(newSheet.CodeName).CodeModule ' Use the CodeName property to get the module name of the new sheet
        newCodeModule.InsertLines 1, Join(codeLines, vbCrLf)
    End If
End Sub

There is a runtime error 438 here : Set baseCodeModule = baseSheet.CodeModule

Thanks

/Dan
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Why not dupplicate the sheet (Copy) ?
For example:
VBA Code:
Option Explicit
Private Sub NewStudent_Click()
Sheets("Base_Sheet").Copy after:=Sheets(Sheets.Count)
With ActiveSheet
    .Name = InputBox("Enter the name of the new sheet:")
    .Range("A3").Value = .Name
End With
End Sub
 
Upvote 0
I mean, yeah! wow that should work. ill give it a test but thank you! is so simple. haha
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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