copy sheets from main workbook to new one then clear ranges in all the sheets in new workbook

bthyaabd

New Member
Joined
Nov 16, 2022
Messages
5
Office Version
  1. 2007
Platform
  1. Windows
i am trying to duplicate my main workbook (copy and paste all the sheets), then in the newly duplicated workbook i need to clear from each sheet a specific range. Once the ranges are cleared then i need to copy from main sheets the last row of every sheet and paste it in the newly created workbook

In the code that i have i managed to copy/paste all the sheets and save the new workbook as macro enabled.

the problem i am having is when i try to clear contents from the "Main" sheet in the new workbook my code is clearing the contents and creating a new sheet also so i am having 2 sheet named main and main2
also i am not able to clear other sheets because it will do the same thing as the main sheet.

please help.
thank you

Sub Closing_year()


Dim fname As Variant
Dim NewWb As Workbook
Dim FileFormatValue As Long
Dim ws As Worksheet
Dim ws_temp As Worksheet
Dim wsname As String
Dim thiswb As Workbook
Set thiswb = ThisWorkbook
Dim MainSheet As Worksheet
Dim MainLastRow As Long
Dim PSheet As Worksheet
Dim LastRowP As Long
Dim PSheetName As String
Dim SuppSheet As Worksheet
Dim SuppLastRow As Long
Dim AccSheet As Worksheet
Dim AccLastRow As Long
Dim i As Integer







If Val(Application.Version) < 9 Then Exit Sub
If Val(Application.Version) < 12 Then


fname = Application.GetSaveAsFilename(InitialFileName:="", _
filefilter:="Excel Files (*.xls), *.xls", _
Title:="This example copies the ActiveSheet to a new workbook")

If fname <> False Then

ActiveSheet.Copy
Set NewWb = ActiveWorkbook


NewWb.SaveAs fname, FileFormat:=-4143, CreateBackup:=False
NewWb.Close False
Set NewWb = Nothing

End If
Else


fname = Application.GetSaveAsFilename(InitialFileName:="", filefilter:= _
" Excel Macro Free Workbook (*.xlsx), *.xlsx," & _
" Excel Macro Enabled Workbook (*.xlsm), *.xlsm," & _
" Excel 2000-2003 Workbook (*.xls), *.xls," & _
" Excel Binary Workbook (*.xlsb), *.xlsb", _
FilterIndex:=2, Title:="This example copies the ActiveSheet to a new workbook")


If fname <> False Then
Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
Case "xls": FileFormatValue = 56
Case "xlsx": FileFormatValue = 51
Case "xlsm": FileFormatValue = 52
Case "xlsb": FileFormatValue = 50
Case Else: FileFormatValue = 0
End Select


If FileFormatValue = 0 Then
MsgBox "Sorry, unknown file extension"
Else

ActiveSheet.Copy
Set NewWb = ActiveWorkbook
For Each ws In ThisWorkbook.Sheets
ws.Copy After:=NewWb.Sheets(NewWb.Worksheets.Count)
Next

Set ws = NewWb.Sheets("Main")
ws.Unprotect Password:="123"

With ws
MainLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

ws.Range("B28:M" & MainLastRow).ClearContents
Set MainSheet = ThisWorkbook.Sheets("Main")
MainSheet.Unprotect Password:="123"
MainSheet.Range("A" & MainLastRow).Copy
ws.Cells(28, "C").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False


NewWb.SaveAs fname, FileFormat:= _
FileFormatValue, CreateBackup:=False
NewWb.Close False
Set NewWb = Nothing

End If
End If
End If
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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