Saving selected worksheets in VBA

singcbl

Well-known Member
Joined
Feb 8, 2006
Messages
518
The codes below allows me to save the active worksheet into a new workbook using the active worksheet tab name. My question is I like to save the selected sheets with a new name instead of just one sheet which the code below is doing.

Code:
Sub exportwksht()
    Dim myPath As String, NewName As String, ws As Worksheet
    myPath = ThisWorkbook.Path
    NewName = ActiveSheet.Name
    Application.DisplayAlerts = False
    For Each ws In Sheets
    On Error Resume Next
    If ws.Name <> NewName Then ws.Delete
    Next ws
    On Error GoTo 0
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs NewName & ".xls"
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi
try the following modifications
Code:
Sub exportwksht()
    Dim myPath As String, NewName As String, ws As Worksheet
    myPath = ThisWorkbook.Path
    NewName = ActiveSheet.Name
    Application.DisplayAlerts = False
  For a = 1 To Sheets.Count
  Cells(a, 1) = Worksheets(a).Name
  Cells(a, 2) = InputBox("enter new name for sheet " & Cells(a, 1) & " leave blank to delete")
  Next a
  For b = Sheets.Count To 1 Step -1
  If Cells(b, 2) = "" Then
  Worksheets(b).Delete
  Else
  Worksheets(b).Name = Cells(b, 2)
  End If
  Next b
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs NewName & ".xls"
End Sub
run the macro
It will ask for new name for old sheet name. if you return blank they will be deleted. rest of the sheets will be saved under activesheet name
 
Upvote 0
Ravishanker,

This error message appeared while running the codes. The macro stops at

Cells(a, 1) = Worksheets(a).Name

Code:
Runtime error '1004'
Application-defined or object -defined error
 
Upvote 0
Hi
I tested the codes again and they work as expected. retains renamed sheets and deletes ones without added names. Just give it a go again.
Ravi
 
Upvote 0
Ravi,

Do you think in my case it did not work because I have a few hidden sheets in this workbook which I need to retain as well.
 
Upvote 0
try
Code:
Sub exportwksht()
    Dim myPath As String, NewName As String, ws As Worksheet
    Dim myList As STring, x
    myPath = ThisWorkbook.Path
    NewName = ActiveSheet.Name
    For Each ws In ActiveWindow.SelectedSheets
         myList = myList & ":" & ws.Name
    Next
    x = Split(Mid$(myList,2),":")
    Application.DisplayAlerts = False
    For Each ws In Sheets
         If IsError(Application.Match(ws.Name, x, 0)) Then ws.Delete
    Next
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs NewName & ".xls"
End Sub
 
Upvote 0
try
Code:
Sub exportwksht()
    Dim myPath As String, NewName As String, ws As Worksheet
    Dim myList As STring, x
    myPath = ThisWorkbook.Path
    NewName = ActiveSheet.Name
    For Each ws In ActiveWindow.SelectedSheets
         myList = myList & ":" & ws.Name
    Next
    x = Split(Mid$(myList,2),":")
    Application.DisplayAlerts = False
    For Each ws In Sheets
         If IsError(Application.Match(ws.Name, x, 0)) Then ws.Delete
    Next
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs NewName & ".xls"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,421
Messages
6,119,392
Members
448,891
Latest member
tpierce

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