Copy an excel sheet in the same file without losing formatting

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello, please help in modifying the code. I want to copy . sheet TTC to the same file and rename it to the last value in column A in Sheet DATA The code works, the problem .Copies are done, but the formatting and the size of columns and rows are lost, what is the solution?
Please help in modifying the code or provide me with a code that does the same task


VBA Code:
Option Explicit

Private Sub newSheet_Click()

Dim lastLine As Integer
Dim nameSheet As String
Dim nok As Boolean

lastLine = ThisWorkbook.Sheets("DATA").Range("A" & Rows.Count).End(xlUp).Row

nameSheet = ThisWorkbook.Sheets("DATA").Range("A" & lastLine)

nok = feuilleExiste(nameSheet)
If nok = True Then
    MsgBox " Cette feuille existe déjà"
Else
    ThisWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count())
    
    ThisWorkbook.Sheets(Worksheets.Count()).Name = nameSheet
    
    ThisWorkbook.Sheets("TTC").UsedRange.Copy Destination:=ThisWorkbook.Sheets(nameSheet).Range("A1")
    
    ThisWorkbook.Sheets("DATA").Activate
End If



End Sub

Function feuilleExiste(FeuilleAVerifier As String) As Boolean
'fonction qui vérifie si la "FeuilleAVerifier" existe dans le Classeur actif

On Error Resume Next
ThisWorkbook.Sheets(FeuilleAVerifier).Name = Sheets(FeuilleAVerifier).Name
feuilleExiste = (Err.Number = 0)
End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Perhaps this:

VBA Code:
Sub CopySht()
    Worksheets("TTC").Copy After:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = Worksheets("DATA").Cells(Rows.Count, 1).End(xlUp).Value
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

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