VBA not save extension when run

scorpio3st

New Member
Joined
Sep 7, 2021
Messages
22
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Sub SaveValues()
Dim SourceBook As Workbook, DestBook As Workbook, SourceSheet As Worksheet, DestSheet As Worksheet

Dim SavePath As String, i As Integer
Dim strFilename As String
Set rngRange = Worksheets("Sheet1").Range("M1")
Set SourceBook = ThisWorkbook

'*********************************************
'Edit next two lines as necessary
strFilename = rngRange.Value & Format(Now(), "dd.mm.yyyy")
SavePath = "C:\Users\akardos\Desktop\Privremeno/" & Sheets("Sheet1").Range("M1") & strFilename
Set SourceSheet = SourceBook.Sheets("Sheet1")

'*********************************************

Set DestBook = Workbooks.Add
Set DestSheet = DestBook.Worksheets.Add

Application.DisplayAlerts = False
For i = DestBook.Worksheets.Count To 2 Step -1
DestBook.Worksheets(i).Delete
Next i
Application.DisplayAlerts = True

SourceSheet.Cells.Copy
With DestSheet.Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats 'Delete if you don't want formats copied
End With

DestSheet.Name = SourceSheet.Name

Application.DisplayAlerts = False 'Delete if you want overwrite warning
DestBook.SaveAs Filename:=SavePath
Application.DisplayAlerts = True 'Delete if you delete other line

SavePath = DestBook.FullName
DestBook.Close 'Delete if you want to leave copy open
MsgBox ("A copy has been saved to " & SavePath)

End Sub

This is Macro, but when run it saves document without xlsx or xls. Please can you help me to get extension.

1708503830640.png

Thanks.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You can add it here, like this:
VBA Code:
strFilename = rngRange.Value & Format(Now(), "dd.mm.yyyy") & ".xlsx"

By the way, this other line seems wrong to me. Instead of this:
VBA Code:
SavePath = "C:\Users\akardos\Desktop\Privremeno/" & Sheets("Sheet1").Range("M1") & strFilename
it should be (see a Backslash instead of a Slash):
VBA Code:
SavePath = "C:\Users\akardos\Desktop\Privremeno\" & Sheets("Sheet1").Range("M1") & strFilename
 
Upvote 0
Solution
for Xlsx try this
VBA Code:
DestBook.SaveAs Filename:=SavePath & ".xlsx", FileFormat:=xlOpenXMLWorkbook

for Xls try this
VBA Code:
DestBook.SaveAs Filename:=SavePath & ".xls", FileFormat:=xlExcel8
 
Upvote 0
Thanks for the positive feedback(y), glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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