Saving Worksheet as New Workbook Using Cell Value as Title - excel

I have the following code:
Sub SaveFinalMTO()
Application.ScreenUpdating = False
Sheets("Final MTO").Select
Sheets("Final MTO").Copy
'grab the file name from b6:m6, put it in variable ThisFile
ThisFile = Sheets("Final MTO").Range("b6:m6").Value
Sheets("Final MTO").SaveAs Filename:="C:\Users\owner\Desktop\" & ThisFile & ".xlsm"
Application.ScreenUpdating = True
ActiveWorkbook.Close
End Sub
Everything works fine to an extent. The new workbook is created with the correct sheet. The new file is opened but the file name is "Book1" instead of the values in range B6:M6 which is a merged cell using a concatenate function. I tried using an unmerged with just a value, I was still pulling up a
run time error "13"
Any help solving this error would be much appreciated. Thank you.

If you are going to save as a macro enabled file type, then you have to specify that. Also, you can make the folder name dynamic using Environ$.
Sheets("Final MTO").SaveAs Filename:="C:\Users\" & Environ$("Username") & "\Desktop\" & ThisFile & ".xlsm", xlOpenXMLWorkbookMacroEnabled
XlFileFormat Enumerations

Related

Protecting a new excel sheet that has been exported

I have the following macro that exports a current excel sheet with some data to a new workbook into a specific path. The trouble i have, is that i want to protect that workbook new sheet after is created. How it can be done? I tried using ActiveWorkbook.Protect "Password" but did not worked.
Sub NuevoDia()
Dim FilePath As String
Dim NewName As String
FilePath = "C:\Users\Pol\Desktop\": NewName = FilePath & "Registros " & Format(Date, "DD-MM-YYYY") & ".xls"
Sheets("Registros").Select
Hoja3.Unprotect "LOG2020"
Sheets("Registros").Copy
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat _
:=xlWorkbookNormal, CreateBackup:=False
End Sub
Thanks for the help!
To protect a sheet I would suggest to do :
Sheets("Registros").Protect "password"
And if you wanted to protect workbook since you tried :
ActiveWorkbook.Protect Password:="password", Structure:=True, Windows:=True
Also note that it is better to not use select so
Sheets("Registros").Select
Hoja3.Unprotect "LOG2020"
Sheets("Registros").Copy
do the same as
Hoja3.Unprotect "LOG2020"
Sheets("Registros").Copy

Error - Can't execute in break mode - VBA

Thanks for everyone that answers questions on here. I use this site all the time. I'm not formally trained but have put together some stuff in the past.
Here is what my Code accomplishes. I have a macro enabled excel file that I store in SharePoint. My users edit the excel and run a macro that saves their changes into a CSV File that we use to Import into JIRA. I've been able to create the macro to do all this and it works great when I used it. But when others in my group use it they are getting a "Can't execute in break mode" error. I think I'm missing some validation code but I'm not sure how to achieve this. Any help would be greatly appreciated! I'm so close!!
'''
Sub Save_CSV_Debugger()
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Sheet1")
'Makes a copy of the Worksheet
ws.Copy
'Creates New FileName - Concatenates username and Desktop path with for
New Name
NewName = Environ("USERPROFILE") & "\Desktop\" & Range("A2").Value & " -
JIRA Import" & ".CSV"
Application.DisplayAlerts = False
'Saves WB with NewFileName
ActiveWorkbook.SaveAs Filename:=NewName, _
FileFormat:=xlCSV, CreateBackup:=False
Application.DisplayAlerts = True
'Hides saves dialog
If SaveAsUI = True Then Cancel = True
' Shows user a message
MsgBox "File saved to Desktop for JIRA Import " & vbNewLine & NewName
ActiveWorkbook.Close
'Reopens CSV File Without Macro - Clean CSV
Application.Workbooks.Open (NewName)
End Sub
'''

VBA Export and save to CSV issues

Found the below code online, though it's a bit strange when I run the macro for dates as my dates will convert from:
[DD/MM/YYYY] to [MM/DD/YYYY]
example:
31/07/2017 to 07/31/2017.
anyone able to assist, I would want to keep it was [DD/MM/YYYY].
Refer to below:
Dim strName As String
Dim filepath As String
Application.ScreenUpdating = False
strName = ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & " " & ActiveSheet.Name & ".csv"
ActiveSheet.Copy 'copy the sheet as a new workbook
ActiveWorkbook.SaveAs Filename:=strName, FileFormat:=xlCSV
ActiveWorkbook.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "File has been Created and Saved as: " & vbCr & strName, , "Copy & Save Report"
thanks,
Depending on your local language settings (specified in Control Panel), the following should work:
ActiveWorkbook.SaveAs Filename:=strName, FileFormat:=xlCSV, Local:=True
This should ideally be used by specifying the TextCodepage parameter of the SaveAs method.
However, according to MSDN reference, this parameter is ignored for all languages of Excel.
A previous question on SO addresses this, and suggests instead using FileFormat:=xlUnicodeText, and then setting the extension to .csv, i.e.:
ActiveWorkbook.SaveAs Filename:=strName & ".csv", FileFormat:=xlUnicodeText
However, this does not seem to work for my part when testing your scenario.

Delete Cell content after Saveas

I am trying to delete the contents of few cell in the saved copies of my workbook that is under different file names. As code below, this is deleting the content from original workbook and retaining the content in the saved wb. It is doing the right opposite task that I wanted for!
Also, any suggestion on how to disable few modules and delete few pictures in the saved wb ?
Thanks in Advance for help !
Sub SaveAsNewCopy()
Dim Path As String
Dim FileName1 As String
Application.DisplayAlerts = False
FileName1 = Range("D3")
ThisWorkbook.SaveCopyAs FileName:="C:\Users\..\..\..\" & FileName1 & "-" & "List" & ".xlsm"
MsgBox "File Saved successfully!", , "Save"
ThisWorkbook.Sheets("Sheet1").Range("E5:F5").ClearContents
ThisWorkbook.Sheets("Sheet1").Range("E9:F9").ClearContents
Application.DisplayAlerts = True
End Sub
You need to get a handle on the workbook you just saved, make the changes you want and then save it again. The easiest way to do this is to assign a variable to it. In your declarations do something like this:
Dim wb as Workbook
then before your save-as line assign the saved workbook to that variable like this:
Set wb = ThisWorkbook.SaveCopyAs FileName:="C:\Users......\" & FileName1 & "-" & "List" & ".xlsm"
Then you can work with wb as required as save it with wb.Save True etc etc

How to copy just one sheet (not all) to .xls file in Excel VBA?

Please Help.
I want to copy just one Sheet ‘MainFinal’ among twelve other sheets(not all of the sheets in the original), to another .xls file using Excel VBA . The code I have attached below works , except it copies all worksheets and not JUST the one, and also when new file created it opens and source file is closed . Anyone have any suggestions what wrong with my code? I have tried various combinations of Worksheet, Sheets, Activesheet but without successes)?
Sub CopyMainFin()
'
' CreaMainFin Macro
'
Dim LastCopyRow As String
Dim MyStr As String
MyStr = Format(Date, "mmddyyyy")
LastCopyRow = “BT307”
Application.ScreenUpdating = False
Worksheets("MainFinal").Range("A1", LastCopyRow).Activate
Worksheets("MainFinal").Range("A1",LastCopyrow).Select
Worksheets("MainFinal").Range("A1", LastCopyRow).Copy
‘ I noticed that my rage selected
ActiveSheet.SaveAs Filename:="C:\Documents and Settings\algorn\My Documents\Excel files\" & "OutputFile" & MyStr & ".xls", CreateBackup:=False
'ActiveWorkbook.SaveAs Filename:="C:\Documents and Settings\My Documents\Excel files\" & "OutputFile" & MyStr & ".xls", CreateBackup:=False = Also not working
End Sub
This will move the named sheet into a brand new file. No extra coding needed.
Sheets("MainFinal").Copy
This line should save your new file.
Workbooks(workbooks.count).saveas _
Filename:="C:\Documents and Settings\algorn\My Documents\Excel files\" & _
"OutputFile" & MyStr & ".xls", CreateBackup:=False

Resources