VBA Copy Worksheet Then Save As

andersen_yunan

New Member
Joined
Feb 7, 2018
Messages
36
Hi All,

I'm currently trying to copy certain worksheet in my workbook and then save it as new excel file in download folder.

Sub RectangleRoundedCorners1_Click()
Dim FName As String
Dim FPath As String

FPath = "C:"
FName = "ResultsLessThan" & Format(Date, "ddmmyy") & ".xlsx"

Sheets("Less Than").Copy
ThisWorkbook.Sheets("Less Than").SaveAs Filename:=FPath & "" & FName
End Sub

I'm using this as the code but it does not excecute as per I wanted. Is there something I did wrong or is there any other option to do this?

Thanks in advance!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi,
try this update to your code

Code:
Sub RectangleRoundedCorners1_Click()
    Dim FName As String
    Dim FPath As String
    
    FPath = "C:"
    FName = "ResultsLessThan" & Format(Date, "ddmmyy") & ".xlsx"
    
    Sheets("Less Than").Copy
    ActiveWorkbook.SaveAs Filename:=FPath & "\" & FName, FileFormat:=51
End Sub

Dave
 
Upvote 0
Hi,
try this update to your code

Code:
Sub RectangleRoundedCorners1_Click()
    Dim FName As String
    Dim FPath As String
    
    FPath = "C:"
    FName = "ResultsLessThan" & Format(Date, "ddmmyy") & ".xlsx"
    
    Sheets("Less Than").Copy
    ActiveWorkbook.SaveAs Filename:=FPath & "\" & FName, FileFormat:=51
End Sub

Dave


Hi Dave,
Thanks for the response! This works perfectly, question though why would the FileFormat:=51 needed?
 
Upvote 0
Hi Dave,
Thanks for the response! This works perfectly, question though why would the FileFormat:=51 needed?

Hi,
glad resolved your issue.

When using SaveAs you should specify the FileFormat required in this case, you are saving file in xlsx format (no macros) fileformat name [FONT=&quot]xlOpenXMLWorkbook[/FONT] or value 51

You can read guide here: http://codevba.com/excel/workbook_saveas.htm#.XSwhWOtKjIV

Choices of File Format enumeration: https://docs.microsoft.com/en-us/office/vba/api/Excel.XlFileFormat


Dave
 
Upvote 0
Upvote 0
Hi,
glad resolved your issue.

When using SaveAs you should specify the FileFormat required in this case, you are saving file in xlsx format (no macros) fileformat name xlOpenXMLWorkbook or value 51

You can read guide here: http://codevba.com/excel/workbook_saveas.htm#.XSwhWOtKjIV

Choices of File Format enumeration: https://docs.microsoft.com/en-us/office/vba/api/Excel.XlFileFormat


Dave

Hi Dave,

One more question though, I am copying a worksheet in which I protected it. Can I save it as non-protected when copying it into a new workbook? In addition to that I also realize that the worksheet cannot be hide when running the VBA, in which I want to hide it. Is there any way to solve this?
 
Upvote 0
Hi Dave,

One more question though, I am copying a worksheet in which I protected it. Can I save it as non-protected when copying it into a new workbook? In addition to that I also realize that the worksheet cannot be hide when running the VBA, in which I want to hide it. Is there any way to solve this?

I counted two questions

Untested but maybe following update will do what you want


Rich (BB code):
Sub RectangleRoundedCorners1_Click()
    Dim FName As String, FPath As String
    Dim ws As Worksheet
    
    Const MyPassword As String = "YOUR PASSWORD HERE"
    
    Application.ScreenUpdating = False
    Set ws = ThisWorkbook.Worksheets("Less Than")
    
    FName = "ResultsLessThan" & Format(Date, "ddmmyy") & ".xlsx"
    
    With ws
        .Visible = xlSheetVisible
        .Unprotect Password:=MyPassword
        .Copy
        .Visible = xlSheetHidden
        .Protect Password:=MyPassword
    End With
    
    With ActiveWorkbook
        .SaveAs Filename:=FPath & "\" & FName, FileFormat:=51
        .Close False
    End With
    
    Application.ScreenUpdating = True
End Sub

Enter you worksheet password where shown in red as required

Dave
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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