Copy table headers from current sheet to newly created sheet in excel vba

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I use below code to duplicate a worksheet with a dynamic table, then clear all contents, embedded items and pictures of whole sheet but I need to copy all headers' value of the table back to new sheet but failed. Need help !
CODE
Sub BlankSheet()
If ActiveWorkbook Is ThisWorkbook Then
Dim ws As Worksheet
Set ws = ActiveSheet
Dim LastColumn As Long

LastColumn = ws.Range("A1").CurrentRegion.Columns.Count


On Error Resume Next
Application.DisplayAlerts = False
ws.Range(Cells(1, 1), Cells(1, LastColumn)).Copy
ws.Copy before:=ActiveSheet
szToday = Format(Date, "d mmm yyyy")
ActiveSheet.name = szToday
Application.DisplayAlerts = True

'Clear All Contents
ActiveSheet.Cells.ClearContents
ActiveSheet.OLEObjects.Visible = True
ActiveSheet.OLEObjects.Delete
ActiveSheet.Pictures.Visible = True
ActiveSheet.Pictures.Delete
ActiveSheet.Range(Cells(1, 1)).Paste

End If
End Sub
 
Hi Vincent88,

it may have been to obvous for me but I´d like to ask if you considered using a template sheet set up as you would need it and use that for the copy and rename instead of copying an existing sheet, deleting items and rebuilding from scratch.

Ciao,
Holger
Hi HaHoBe,
Appreciate your suggestion. My file is using daily and we need to create a worksheet for that day.
I am not sure if a template will be easier in my case. Welcome your professional advice.
Thank you in advance.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Vincent88,

does the code work like you want it to or is there still any problem from your side? Like I stated I couldn´t find any errors when using your codes on my test workbook. Please clarify.

Ciao,
Holger
 
Upvote 0
Hi Vincent88,

does the code work like you want it to or is there still any problem from your side? Like I stated I couldn´t find any errors when using your codes on my test workbook. Please clarify.

Ciao,
Holger
Hi HaHoBe,
Your code works fine and it is what I want. The problem should be on my code. I just want the code to check if the sheet name of the day is already existed then no need to create new worksheet and a message box will pop up and no new sheet to create.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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