Error - Can't execute in break mode - VBA - excel

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
'''

Related

How to save copy of Excel workbook?

I've been trying to make a macro that automatically saves a backup copy of my Excel workbook before I edit any of its data.
Every time I call it, even though it successfully saves a new copy, when it reaches the end the code stops execution, and macros I try to call below it don't execute.
I think when I run the code and the new backup version is created, the code continues to run on the backup workbook instead of the main workbook, so when I close the backup workbook I abruptly end the code.
I'd like to save a backup version of my main workbook, close the backup version and continue the code on the main workbook.
Public Sub BackupWorkbook()
Dim CurrentFile As String, BackupFile As String, DesiredWorkbookName As String
Dim NowDate As String
'Save current code and Excel spreadsheet data
ActiveWorkbook.Save
'Get necessary strings for filenames
CurrentFile = ThisWorkbook.FullName
NowDate = Replace(Format(Now, "dd-mm-yyyy, hh:mm:ss"), ":", ".")
BackupFile = ThisWorkbook.Path & "\" & "Chem Chart Backups" & "\" & "Chemical Chart" _
& " (" & NowDate & ")" & ".xlsm"
'Save as active workbook to backup file location, then reopen main workbook
ActiveWorkbook.SaveAs BackupFile, FileFormat:=52
Workbooks.Open CurrentFile
'This should close the backup version of workbook that opened because of SaveAs method
Workbooks(DesiredWorkbookName & " (" & NowDate & ")" & ".xlsm").Close SaveChanges:=True
End Sub
Public Sub TestMacros()
Call BackupWorkbook
'If this message box pops up after macro is called, it is successful
MsgBox "Success!"
End Sub
I use
Sub BackUp()
Dim BackUpPath As String
BackUpPath = "Your path"
Dim BackUpFile As String
BackUpFile = BackUpPath & "BackUp.xlsm"
ThisWorkbook.SaveCopyAs Filename:=BackUpFile
End Sub
I call this at various times in my project to backup the document as required, this seems to work okay for me and my code will continue to run in the original workbook.
I also generate a unique filename each time and do not actually use "BackUp" as the name of the new workbook as this overwrites my previous backup so I would recommend doing something similar.

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

How can I copy the following excel data into a different workbook and have it prompt the user to save?

Essentially I have the following code stating what cells I would like to copy over into another workbook:
Application.CopyObjectsWithCells = False
ActiveSheet.Copy
Range("A1:T40").Copy
Range("A1:T40").PasteSpecial Paste:=xlPasteValues
Application.CopyObjectsWithCells = True
The VBA script automatically opens a new workbook with all of the data pasted from the other workbook.
But now I would also like it to prompt the user with a save window as soon as the new workbook opens, how can this be done?
UPDATE:
The following code prompts the user to save the excel workbook, what can I add to have it save to a specific file with a specific name?
fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Excel Workbooks (*.xlsx*),*.xlsx*")
If fileSaveName <> False Then
MsgBox "Save as " & fileSaveName
End If
UPDATE 2
Dim path As String
Dim filename1 As String
Application.CopyObjectsWithCells = False
ActiveSheet.Copy
Range("A2:T40").Copy
Range("A2:T40").PasteSpecial Paste:=xlPasteValues
Application.CopyObjectsWithCells = True
path = "C:\Users\jmills\Documents\Report\"
filename1 = Range("H1")
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs Filename:=path & filename1 & ".xlsx", FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
End Sub
How can I keep the save as prompt up? I would like it to allow me to edit the name before it saves
Currently with the code above it saves automatically with whatever is written in cell H1
You can trigger the save filename dialog with Application.SaveAsFileName. Here is more info on what you can do with it: https://learn.microsoft.com/en-us/office/vba/api/excel.application.getsaveasfilename
The output of this is a path were you can save the workbook to.
So afterwards, you need to use something like ActiveWorkbook.SaveAs Filename:=[output from application.saveasfilename]
Like this:
path = "C:\Users\jmills\Documents\Report\"
fileSaveName = Application.GetSaveAsFilename( initialFilename:=path & "myworkbook", _
fileFilter:="Excel Workbooks (*.xlsx*),*.xlsx*")
Application.DisplayAlerts = True
ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlOpenXMLWorkbook
Where you can change myworkbook to whatever you want. The user can change that as well in the filedialog if they want.

Workbooks.open hangs

I have a macro that will open another workbook from a network location, compare some values in a range, copy/paste any that are different, and then close the file. I use variables to open the file, because the appropriate filename is based on the current date. I also set Application.ScreenUpdating = False, and Application.EnableEvents = False
for some reason, the code has begun to hang on the worksheets.open line and I can't even CTRL+Break to get out of it. I have to manually close Excel and sometimes it give me an error message, complaining about there not being "enough memory to complete this action".
I can put a stop in the code and confirmed the variables are supplying the correct string, which equates to:
"\Clarkbg01\public\PRODUCTION MEETING\PROD MEETING 3-21-18.xlsm"
I can paste this into Windows Explorer and it will open right up with no issues. I can manually select the file from Explorer and it will open with no issues. I can paste the following line into the immediate window and it will hang...
workbooks.Open("\\Clarkbg01\public\PRODUCTION MEETING\PROD MEETING 3-21-18.xlsm")
This happens even if I open a blank sheet and execute that line from the immediate window.
from my macro, stepping through the code goes without a hitch. I can verify all the variables are correct, but when it steps across workbooks.open, it hangs.
I have other macros that open workbooks, do much more complicated routines, then close them with zero issues, but I'm really stuck on why this one is giving me so many problems.
Any ideas?
Here is the code:
'This will open the most recent meeting file and copy over the latest for jobs flagged with offsets
Dim Path As String
Path = ThisWorkbook.Path
'Debug.Print Path
Dim FileDate As String
FileDate = ThisWorkbook.Sheets("MEETING").Range("3:3").Find("PREVIOUS NOTES").Offset(-1, 0).Text
'Debug.Print FileDate
Dim FileName As String
FileName = "PROD MEETING " & FileDate & ".xlsm"
Debug.Print "Looking up Offsets from: " & FileName
Dim TargetFile As String
TargetFile = Path & "\" & FileName
Debug.Print TargetFile
Application.ScreenUpdating = False
Application.EnableEvents = False
'The old way I was opening it...
'Workbooks.Open FileName:=Path & "\" & FileName, UpdateLinks:=False ', ReadOnly:=True
'The most recent way to open
Dim wb As Workbook
Set wb = Workbooks.Open(TargetFile, UpdateLinks:=False, ReadOnly:=True)
'Do Stuff
wb.Close savechanges:=False
Application.ScreenUpdating = True
Application.EnableEvents = True
MsgBox "Offsets should now reflect settings made in meeting on " & FileDate
End Sub
If the workbook you're opening contains code in the Workbook_Open event then this will attempt to execute when the event fires .
To stop this behaviour use the Application.AutomationSecurity Property.
Public Sub Test()
Dim OriginalSecuritySetting As MsoAutomationSecurity
OriginalSecuritySetting = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
'Open other workbook
Application.AutomationSecurity = OriginalSecuritySetting
End Sub

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

Resources