Override VBA macro in Excel long enough to save very same macro - vba

I have a prefilled template that has ".NC" that needs numbers added in front of the ".NC" before it can be saved (example 12345678.NC). Maybe I am going about this the wrong way but.. I tried to make it alert if only ">NC" is in the box, problem is ".NC" is prefilled in the template sheet and needs to be there, this then flags my macro and will not allow me to save the very macro stopiing it from saving! My code works, it works so well that I cannot save my code.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Application.Sheets("Sheet1").Range("L3").Value = ".NC" Then
Cancel = True
MsgBox ("Please enter program number")
End If
End Sub
Is there a way to override that macro just long enough to save it? Maybe I am just going about this all wrong..

Here is my solution. When a user clicks the save button this run and prompt them to fill out that cell if the cell does not have ".NC" or is less than 3 characters long.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
With Range("L3")
If Len(.Value2) <= 3 Or Right$(.Value2, 3) <> ".NC" Then
'MsgBox ("Please enter program number")
.Value2 = InputBox("Please enter program number") & ".NC"
End If
End With
End Sub

Related

VBA excel macros: Run only from a location

I have a few Excel sheets with macros which I want any user to be able to run only from a particular location, in my case a particular sharepoint.
Should a user "SaveAs" the Excel file to any alternative location - he should get an error message.
I am looking for a VBA script to be put in these workbooks to allow me meet my objectives.
Can someone help please?
You can there display an error message and set the Cancel parameter to true if someone wants to save it to another location.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not ThisWorkbook.Path Like "//sharepoint/path/*" Then
MsgBox "You can't save the excel", vbCritical
Cancel = True
End if
End Sub
In the same way you can check in the Workbook_Open function if the Workbook is opened in a correct location:
Private Sub Workbook_Open()
If ThisWorkbook.Path Like "//sharepoint/path/*" Then
'Do things
Else
'Error message and close
End If
End Sub

VBA cells are not highlighted

Its a protected worksheet/workbook and I have a code that will throw a prompt for the user, whether to edit the sheet or not. Cells are editable, but the problem is cells are not getting highlighted with border. So its difficult for the user to know which cells is he working on.
I have 2 sheets here, Corefiller and Ad-filler, if dropdown on corefiller sheet is "No". User gets a prompt when he selects the sheet, he clicks ok to edit the sheet or cancel if he doesnt want to edit.
Code on Sheet "Ad-filler"
Option Explicit
Private mMessageDisplayed As Boolean
Private Sub Worksheet_Activate()
Carry
End Sub
Code on a module.
Public Sub Carry()
If ActiveSheet.ProtectContents And Not mMessageDisplayed Then
mMessageDisplayed = True
If ThisWorkbook.Sheets("Corefiller").Range("E29") = "NO" Then
If MsgBox("Click OK to include Filler for this request", vbOKCancel + vbInformation) = vbOK Then
ThisWorkbook.Worksheets("Corefiller").Range("E29") = "YES"
With ThisWorkbook.Sheets("Ad-filler")
.Range("E13:E14").Locked = False
End With
Else
With ThisWorkbook.Sheets("Ad-filler")
.Range("E13:E14").Locked = True
End With
End If
Else
Exit Sub
End If
End If
End Sub
Whats wrong in my code? why the cell is not highlighted. If i try to use protect/unprotect in the code, cells on the first sheet (Corefiller) will not be highlighted and I have to click on other sheets and come back to get the cell highlighted.
Can you restart, implement this and check whether the problem still exists:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Cells.Interior.ColorIndex = 0
Target.Interior.ColorIndex = 3
End Sub

Prompting a macro on close of workbook, but kill the closing if user press "No"

I am trying to execute a macro on close of a workbook.
The macro works perfectly but the problem is the closing function. I want a user to be prompted to say "Yes" or "No" when closing the workbook. If the user presses "Yes" the workbook should save as xlsm and be closed.
If the user presses "No" the macro should be executed so that the user is sent to sheet "Projektinformation" and the workbook should not be closed.
Here is my code, any thoughts?
Sub Auto_Close()
Dim OutPut As Integer
OutPut = MsgBox("Projektinformation ifylld?", vbYesNo, "Projektinformation.")
If OutPut = 6 Then
'Output = 6(Yes)
ThisWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
'Output = 7(No)
Sheets("Projektinformation").Select
End If
End Sub
From your comment, I'm inferring that your code looks something like this on the Workbook_BeforeClose side:
Private Sub Workbook_BeforeClose(Cancel as Boolean)
Call Auto_Close()
End Sub
The problem is the code does exactly what you asked it to! It runs your Auto_Close subroutine (before the workbook closes) and then proceeds to close the workbook!
In order to achieve what you are trying to achieve, you have to change the Cancel parameter, passed into the Workbook_BeforeClose sub, to True. When Cancel = True the workbook will cancel the close event, otherwise it will continue as usual. I would either pass Cancel into your sub by reference and change the flag depending on what your user clicks or make Auto_Close() a function that returns a boolean, indicating whether or not to continue closing the workbook.
EXAMPLE
Private Sub Workbook_BeforeClose(Cancel as Boolean)
If SomeCondition = True Then
Cancel = True '<-- Workbook will stay open
Else
Cancel = False '<-- Workbook will close as usual
End If
End Sub
You are putting the code in the wrong place. It should be in the Workbook.BeforeClose Event event macro in the ThisWorkbook code sheet.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim OutPut As Integer
OutPut = MsgBox("Projektinformation ifylld?", vbYesNo, "Projektinformation.")
If OutPut = 6 Then
'Output = 6(Yes)
ThisWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
'Output = 7(No)
Cancel = True
Sheets("Projektinformation").Select
End If
End Sub
Note the Cancel = True. This tells Excel to halt the close operation and continue processing instructions.

Adding security to my workbook internally using VBA

For testing purposes lets assume the code to unlock the workbook is 1.
I want an input box to show up on opening, if the user puts in an incorrect code/Or selects cancel on the input box then the workbook closes.
Private Sub Workbook_Open()
ActiveSheet.Range("A1").Activate
Code = Application.InputBox("Enter School Code", CancelCode)
If Code < 0 > 1 Or False Then
Code = Int(1)
MsgBox ("Unrecognised Code")
Application.Workbooks("CODING.xlsm").Close 'False
End If
End Sub
At the moment it appears but runs if any number is entered and also closes the input box and allows access if cancel is clicked.
Try this:
Private Sub Workbook_Open()
Dim Code As Variant
Code = Application.InputBox("Enter School Code", CancelCode)
If Not Code = 1 Then
MsgBox ("Unrecognised Code")
ThisWorkbook.Close SaveChanges:=False
End If
End Sub
Just tested it and it works for me.
I should add, however, that this is extremely low-level security, which could be bypassed by anyone with even rudimentary knowledge of VBA. You simply have to set Application.EnableEvents = False in another workbook (or in the VBA Immediate window) before opening up your workbook, and the Workbook_Open event will not run at all.

Disable Excel save option but allow macro save

I'm creating an excel file and I want to disable the 'save' and 'save as...' option.
I found a lot of solutions on the internet, like this one in VBA:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
It prevents user from saving changes, but I can't save my changes and that's the problem because I need to do more changes in the VBA code.
Is there a way to save my macro changes ? Like an administrator mode etc... ?
Thank you for your future answers.
Use a global variable to override the Save disable:
Dim override as Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
if Not(override) then
MsgBox "You can't save this workbook!"
Cancel = True
end if
End Sub
Sub SaveMyChanges()
override = true
ActiveWorkbook.Save
override = false
End Sub
You can also save while in Design Mode in VBA.
Sorry for the necro, but this works also and is what I do.