run addin macro on open workbook - excel

I have seen where you can run a macro based on inserted text, but the macro has to be embedded in the sheet, i.e., Private Sub Worksheet_Change(ByVal Target As Range).
What I want to do is call the macro from my add-in from a user's workbook that will not have "Worksheet_Change..." already embedded at the sheet level. Is there a way to do this?
As for additional background, I know I can run the macro from the add in, but I want to activate it using a bar-code scan rather than calling the macro from a button or some other interface.
My bar-code reads as Make Landscape 1 pg when scanned. Hoping to use some modification of this:
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Value) = "Make Landscape 1 pg" Then
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
Application.ScreenUpdating = False
'ActiveWindow.SelectedSheets.PrintPreview
ActiveSheet.Select
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Application.ScreenUpdating = True
End If
End Sub

Here's a link that I found very useful when needing to access Worksheet/Workbook-level events via modules in my Add-In. The basic implementation is like so:
In Add-In's ThisWorkbook module:
Private WithEvents App As Application
Private Sub Workbook_Open()
Set App = Application
End Sub
Now App can be used to call/access workbook & worksheet events. If you're trying to check something upon selection change from a module in an add-in it has to be Workbook_Sheetselectionchange event (which you can read more about here) instead of the Worksheet_Change event. This event can be used in conjunction with the previously set App variable like so:
In Add-In's ThisWorkbook module:
Private Sub App_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
If (Target.Value) = "Make Landscape 1 pg" Then
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
End With
Application.ScreenUpdating = False
ActiveSheet.Select
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Application.ScreenUpdating = True
End If
End Sub

Related

Organise dropdown options into different Excel Sheets

So a multifaceted problem:
I have created a training request sheet that I have now been asked to split into different excel sheets for different requests
Using the VBA below I am able to move 1 option to a new sheet but when try to add an additional condition, by simply reproducing the same vba for each variable, it fails.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
If Target = "Head and Neck" Then
Target.EntireRow.Copy Sheets("HN").Cells(Sheets("HN").Rows.Count, "A").End(xlUp).Offset(1)
Rows(Target.Row).Delete
End If
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Also when the automation works it is deleting the cells that are assigned with the dropdown menu. Is there anyway to create to maintain the dropdown whilst deleting the input data?
Many Thanks

Workbook_SheetChange event triggers twice when it shouldn't

Below code that was in use for a while isn't working anymore.
I did a test in new sheet without other code with the same result.
We've recently moved to Office 365 and my current Excel version is 1902.
Unfortunately everyone here has the same version now, so I can't test it on an older one.
I mention this because I can't think of anything but this being due to a new bug?
Edit: I should add that this was used to prevent (re)moving rows or columns.
Edit: What doesn't work: It triggers twice every time. (I left this important part out after several edits)
Private Sub Workbook_SheetChange(ByVal wks As Object, ByVal Target As Range)
If ((Target.Address = Target.EntireRow.Address Or _
Target.Address = Target.EntireColumn.Address)) Then
With Application
.EnableEvents = False
.Undo
.EnableEvents = True
MsgBox "Do not modify the structure.", vbExclamation, "Notice"
End With
End If
End Sub
you could try and turn it into a SelectionChange event
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If ((Target.Address = Target.EntireRow.Address Or _
Target.Address = Target.EntireColumn.Address)) Then
With Application
.EnableEvents = False
Target.Cells(1, 1).Select
.EnableEvents = True
End With
MsgBox "Do not modify the structure.", vbExclamation, "Notice"
End If
End Sub
that would preserve you from Undo usage and all its consequences

Workbook Before Close Not Working for some but working for others

I have the following codes which is working for some people but not others.
The following does the reverse at opening the spreadsheet.
Could it be a setting it excel?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Control As Office.CommandBarControl
Application.CommandBars("ply").Enabled = True 'disables right-Click on
sheet tab
For Each Control In Application.CommandBars.FindControls(ID:=21)
'disables CUT
Control.Enabled = True
Next Control For Each Control In
Application.CommandBars.FindControls(ID:=19) 'disables COPY
Control.Enabled = True
Next Control
Application.DisplayFormulaBar = True
Application.CutCopyMode = True
Application.CellDragAndDrop = True
End Sub

How Do I Disable a VBA Sub Using Another Sub

I have a workbook that is designed to essentially operate as a standalone application. All sheets are protected, the individual excel tabs are hidden, and the top excel ribbon is hidden as well for the entire workbook.
The following VBA code performs the above procedures and is applied to every sheet within the workbook.
Sub masque()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayFullScreen = True
Application.DisplayStatusBar = Not Application.DisplayStatusBar
Application.WindowState = xlMaximized
ActiveWindow.WindowState = xlMaximized
Application.DisplayFormulaBar = False
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I want to use the following code as a button to override the prior code in order for an individual to easily switch between an "editor" mode and "user mode"
Sub masteredit()
Application.ScreenUpdating = False
ActiveWindow.View = xlNormalView
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayStatusBar = False
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True
ActiveWindow.DisplayWorkbookTabs = True
Application.DisplayFullScreen = False
Application.DisplayFormulaBar = True
Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
Application.ScreenUpdating = True
What is a good way to accomplish this?
well, you could have a predermined type of user, for example, by default the app will be for user_mode. So you use a special sub called auto_open(), that will be run each time you open the workbook. So we have
sub auto_open()
call masque
end sub
And you can add buttons whenever you want to change between editor mode and user mode. You can put sub auto_open() in any module you want, because it is detected as special by Vba
Solved the issue.
I forgot I had the following code on every sheet in the workbook.
Sub Worksheet_Open()
Call masque
End Sub
Sub Worksheet_Activate()
Application.ScreenUpdating = False
Call masque
Application.ScreenUpdating = True
End Sub
Therefore, any time I tried to navigate to a different sheet, the masque would be applied.
To solve my issue, I removed the prior code from every page. I then added a checkbox on my "Home" page that used the following code.
Sub Worksheet_Open()
Call masque
End Sub
Sub Worksheet_Activate()
Application.ScreenUpdating = False
Call masque
Application.ScreenUpdating = True
End Sub
This way, when you open up the workbook the masque is automatically applied. However when you click the checkbox you enter editor mode and the masteredit sub is applied. And when it is unchecked, the masque is once again applied.

Disable Insert Worksheet

Is there a way to prevent people from inserting worksheets manually. I have a form that creates the worksheets automatically based on the information the user puts in it. The code I currently uses will prevent people from creating worksheets but it also prevents my form from creating worksheets. Here is what I am using. It is in the workbook module.
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
End Sub
Declare this in a Module
Public BoolAdd As Boolean
This in your Workbook_NewSheet
Private Sub Workbook_NewSheet(ByVal Sh As Object)
If BoolAdd = False Then
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
Else
BoolAdd = False
End If
End Sub
and set the BoolAdd to TRUE in your userform before you add the sheet.
Private Sub CommandButton1_Click()
BoolAdd = True
Sheets.Add
End Sub
Logic: The userform will set the public variable to True. This will ensure that sheet deletion code will not run. Also we have to set it back to false else the user will be able to add the sheet after the userform is closed.

Resources