VBA Code - Save Worksheet as New Workbook with Certain Name

shaon814

New Member
Joined
Mar 23, 2017
Messages
10
Hi,

I'm trying to find a Macro to save the current worksheet I'm into a new workbook.

For example, when the user clicks the "Save Worksheet" Macro button, it'll save the worksheet they're in to a new workbook with

1. The new workbook named with the "old sheet name" + what information is in cell "N2" + "N5"

Any help would very much be appreciated!

-Thank you

Hussain
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
.
Here is one method :

Code:
Option Explicit

Sub uuu()
    Dim sFolder$, sFile$, sPath$
'--------------------------------
    sFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    'sFile = Range("c2") & ".xlsm"
    sFile = ActiveSheet.Name & " " & Range("N2") & " " & Range("N5") & " " & ".xlsm"
    sPath = sFolder & Application.PathSeparator & sFile
    ActiveWorkbook.SaveAs sPath
    MsgBox "File Saved to Desktop"
End Sub
 
Upvote 0
.
And here is another without carrying over the macro code :

Code:
Option Explicit

Sub SveShts()

Dim xPath As String
Dim xWs  As String

xPath = Application.ActiveWorkbook.Path


Application.ScreenUpdating = False
Application.DisplayAlerts = False

With ActiveSheet
    ActiveSheet.Copy
    Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & ActiveSheet.Name & " " & Range("N2").Value & " " & Range("N5").Value & ".xlsx"
    Application.ActiveWorkbook.Close False
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Perhaps something like this which will save the new workbook in the same folder as the workbook the code is run from.
Code:
Dim wbNew As Workbook
Dim strFileName As String

    With ActiveSheet
        strFileName = .Name & "-" &  .Range("N2").Value & "-" & .Range("N5").Value
        .Copy
    End With

    Set wbNew = ActiveWorkbook

    wbNew.SaveAs ThisWorkbook.Path & "\" & strFileName, xlOpenXMLWorkbook
 
Upvote 0
Perhaps something like this which will save the new workbook in the same folder as the workbook the code is run from.
Code:
Dim wbNew As Workbook
Dim strFileName As String

    With ActiveSheet
        strFileName = .Name & "-" &  .Range("N2").Value & "-" & .Range("N5").Value
        .Copy
    End With

    Set wbNew = ActiveWorkbook

    wbNew.SaveAs ThisWorkbook.Path & "\" & strFileName, xlOpenXMLWorkbook
Hi Norie,

In lieu of saving the range as a
xlOpenXMLWorkbook

Can you save it as a notepad?

Please let me know.
Thank you,
Pinaceous
 
Upvote 0
My suggestion (#3) posts to Notepad.
Hey I tried the post (#3) Notepad extension and the txt notepad came out gibberish.

VBA Code:
Sub SveShts()

Dim xPath As String
Dim xWs  As String

xPath = Application.ActiveWorkbook.path


Application.ScreenUpdating = False
Application.DisplayAlerts = False

With ActiveSheet
    ActiveSheet.Copy
    Application.ActiveWorkbook.SaveAs filename:=xPath & "\" & ActiveSheet.Name & " " & range("N2").Value & " " & range("N5").Value & ".txt"
    Application.ActiveWorkbook.Close False
End With

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hey I tried the post (#3) Notepad extension and the txt notepad came out gibberish.

Unless I'm missing something, you need to specify the file type using the FileFormat parameter in the SaveAs statement.

VBA Code:
Sub SveShts()
    
    Dim xPath As String
    Dim xWs  As String
    Dim xFilename As String
    
    xPath = Application.ActiveWorkbook.Path
    
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    With ActiveSheet
        ActiveSheet.Copy
        xFilename = xPath & "\" & ActiveSheet.Name & " " & Range("N2").Value & " " & Range("N5").Value & ".txt"
        
        Application.ActiveWorkbook.SaveAs Filename:=xFilename, FileFormat:=xlText
        Application.ActiveWorkbook.Close False
    End With
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 1
Unless I'm missing something, you need to specify the file type using the FileFormat parameter in the SaveAs statement.

VBA Code:
Sub SveShts()
   
    Dim xPath As String
    Dim xWs  As String
    Dim xFilename As String
   
    xPath = Application.ActiveWorkbook.Path
   
   
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
    With ActiveSheet
        ActiveSheet.Copy
        xFilename = xPath & "\" & ActiveSheet.Name & " " & Range("N2").Value & " " & Range("N5").Value & ".txt"
       
        Application.ActiveWorkbook.SaveAs Filename:=xFilename, FileFormat:=xlText
        Application.ActiveWorkbook.Close False
    End With
   
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
   
End Sub
Hi rlv01,

WOW! That actually worked perfect!

Curious, just a question to follow, how would I specify a range for the printout?

For example, range A1:AY38?

Thanks,
pinaceous
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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