Macro to save sheets into separated workbooks

jalrs

Active Member
Joined
Apr 6, 2022
Messages
300
Office Version
  1. 365
Platform
  1. Windows
Hello guys,

What im looking for here is to save sheets into different workbooks, where workbook name = sheet name. After doing my research i came to this bit of code, but, unfortunately it only copies the first sheet where the name is <> than "Readme" as code shows.

My code:

Sub CriarWBs()

totalsheets = ThisWorkbook.Sheets.Count

For i = 1 To totalsheets

strsheetname = ThisWorkbook.Sheets(i).Name

If strsheetname <> "Readme" Then

Sheets(strsheetname).Select
Sheets(strsheetname).Copy
ActiveWorkbook.SaveAs Filename:="C:\Users\joafrodrigue\Desktop\teste\Difusao\" & strsheetname & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close

End If

Next

End Sub

Thank you very much guys!
 

Attachments

  • 1.png
    1.png
    6.2 KB · Views: 8
  • 2.png
    2.png
    10.7 KB · Views: 9
  • 3.png
    3.png
    5 KB · Views: 9

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try:
VBA Code:
Sub CriarWBs()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Readme" Then
            Sheets(ws.Name).Copy
            With ActiveWorkbook
                .SaveAs Filename:="C:\Users\joafrodrigue\Desktop\teste\Difusao\" & ws.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                .Close False
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub CriarWBs()
    Application.ScreenUpdating = False
    Dim ws As Worksheet
    For Each ws In Sheets
        If ws.Name <> "Readme" Then
            Sheets(ws.Name).Copy
            With ActiveWorkbook
                .SaveAs Filename:="C:\Users\joafrodrigue\Desktop\teste\Difusao\" & ws.Name & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                .Close False
            End With
        End If
    Next ws
    Application.ScreenUpdating = True
End Sub
it indeed works. i actually found out why it was buggy for me, i had two hidden sheets.

Now when unhiding the sheets, both sheets also are copied. how to prevent this? i know it's somewhere on the if clause, i tried:

if ws.name <> "Readme" or ws.name <> "TAB_FDB" or ws.name <> "Resumo" then

But it didnt work, any help on this?

Thank you so much!

EDIT: Problem appeared, when i close and re-open the book, it shows (4), and when i click enable macros, the sheets simply don't appear (5). see attachments pls
 

Attachments

  • 4.png
    4.png
    18.1 KB · Views: 6
  • 5.png
    5.png
    1 KB · Views: 6
Upvote 0
it indeed works. i actually found out why it was buggy for me, i had two hidden sheets.

Now when unhiding the sheets, both sheets also are copied. how to prevent this? i know it's somewhere on the if clause, i tried:

if ws.name <> "Readme" or ws.name <> "TAB_FDB" or ws.name <> "Resumo" then

But it didnt work, any help on this?

Thank you so much!

EDIT: Problem appeared, when i close and re-open the book, it shows (4), and when i click enable macros, the sheets simply don't appear (5). see attachments pls

I found solution for the edit part. would appreciate any help regarding the question before edit

Thanks again
 
Upvote 0
Try:
VBA Code:
If ws.Name <> "Readme" And ws.Name <> "TAB_FDB" And ws.Name <> "Resumo" Then
 
Upvote 0
hey mumps, sorry to bother again!

was doing some research and i realized i need to bring another sheet with me, is there an easy way to do this? i mean, bring 2 sheets into a new workbook?

i looked here on mrexcel, and found this thread: Importing Multiple Workbooks that have multiple worksheets where you actually give it a go, but it's quite different from what i want.

also looked some tutorials but the ones i saw use arrays and its hard for me to explain myself using arrays.

if arrays is the only way to go, ill look through it. just trying my luck if you have an easier method.

thank you again!
 
Upvote 0
You are very welcome. :)

so what i'm needing now is: i have one sheet called "readme", one sheet called "resumo", one sheet called "armazém" and one sheet called "tab_fdb"

what im asking above if it wasnt clear, is to create a new workbook with "armazém" and "tab_fdb" there. i only need for one department, the others ill adapt myself

thanks
 
Upvote 0
so what i'm needing now is: i have one sheet called "readme", one sheet called "resumo", one sheet called "armazém" and one sheet called "tab_fdb"

what im asking above if it wasnt clear, is to create a new workbook with "armazém" and "tab_fdb" there. i only need for one department, the others ill adapt myself

thanks

other way of doing this is to just set the table on the "armazém" sheet.

for context, tab_fdb is a table where i navigate through with with vlookup and use it to autofill columns.

i tried to just copy and paste the table into the sheet im applying it, but when it generates the new workbook, the column where i have the vlookup assigned doesnt grab the formula nor the table grabs its values. what i got every single time was an empty table
 
Upvote 0

Forum statistics

Threads
1,215,410
Messages
6,124,755
Members
449,187
Latest member
hermansoa

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