export selected sheets

vbachanger

New Member
Joined
Sep 15, 2021
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi, my workbook cointains 5 worksheets of which I need to export sheet1, sheet2 and sheet3 in seperate workbooks.

Need to copy format, without formulas. The code below copies the user form button into the new workbook. How to prevent that?

Workbooks should have the same name as the original file and the sheet name. How to change the code to save the file with the name of the sheet without defining the sheetname in the code?

VBA Code:
Sub sheet1_2_3 ()

strFile = ThisWorkbook.Fullname
strFile = Left(strFile, InStrRev(strFile, ".")-1) & "_sheet1.xlsx

Sheets("sheet1").Copy
With ActiveSheet.UsedRange
.Value = .Value
End With

ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, local:=True

End Sub

THX
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:

Application.CopyObjectsWithCells = False

VBA Code:
Sub sheet1_2_3()
  Dim strFile As String
  strFile = ThisWorkbook.FullName
  strFile = Left(strFile, InStrRev(strFile, ".") - 1) & "_sheet1.xlsx"
  Application.CopyObjectsWithCells = False
  Sheets("sheet1").Copy
  With ActiveSheet.UsedRange
  .Value = .Value
  End With
  ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, local:=True
  Application.CopyObjectsWithCells = True
End Sub
 
Upvote 0
Workbooks should have the same name as the original file and the sheet name. How to change the code to save the file with the name of the sheet without defining the sheetname in the code?

For that, try:

VBA Code:
Sub sheet1_2_3()
  Dim strFile As String
  
  Application.CopyObjectsWithCells = False
  Sheets("sheet1").Copy
  With ActiveSheet.UsedRange
    .Value = .Value
  End With
  strFile = ThisWorkbook.FullName
  strFile = Left(strFile, InStrRev(strFile, ".") - 1) & "_" & ActiveSheet.Name & ".xlsx"
  ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, local:=True
  Application.CopyObjectsWithCells = True
End Sub
 
Upvote 0
That is working! I've got other objects in the sheet, screenshots etc. I want to keep. Only the button should not be copied.

Do you know how to export the first three sheets to separate files?

Thank you!
 
Last edited:
Upvote 0
Something like this maybe. Check the name of each worksheet and only those sheets that have a number as worksheet name should be exported to a new xlsx and saved.

VBA Code:
Dim TexttoFind As String
TexttoFind = "isnumber"

For Each ws In ThisWorkbook.Sheets
    If InStr(1, ws.Name, TexttoFind, vbBinaryCompare) <> 0 Then
        ws.Copy
 
Upvote 0
Check that in the 3 sheets, the name of the button is the same. Replace in the macro "button1" by the name of your button.

Try this:
VBA Code:
Sub sheet1_2_3()
  Dim strFile As String
  Dim i As Long
  
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  For i = 1 To 3
    ThisWorkbook.Sheets(i).Copy
    With ActiveSheet.UsedRange
      .Value = .Value
    End With
    On Error Resume Next
    ActiveSheet.DrawingObjects("button1").Delete
    On Error GoTo 0
    strFile = ThisWorkbook.FullName
    strFile = Left(strFile, InStrRev(strFile, ".") - 1) & "_" & ActiveSheet.Name & ".xlsx"
    ActiveWorkbook.SaveAs Filename:=strFile, FileFormat:=xlOpenXMLWorkbook, local:=True
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,855
Members
449,096
Latest member
Erald

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