Disabling macros from other Excel sheets - vba

I'm working on a few VBA macros in Excel for a project. Is there a way to ensure that VBA macros don't launch from other Excel workbooks when you are opening them through a macro?
For example, let's say I have a macro inside one workbook to open some other Excel file:
Sheets("Sheet1").Select
PathName = Range("D3").Value
Filename = Range("D4").Value
TabName = Range("D5").Value
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=PathName &; Filename
What I've usually done is include a Application.EnableEvents = False to the subroutine to ensure that I'm not triggering code upon opening the other workbooks. This is also the methodology suggested by this prior SO post.
But two questions come to mind:
Is this "secure"? Are there other ways that users could write their own macros that execute on my active application even if I disable events?
Are there other workarounds besides fully disabling events? This seems somewhat limiting and almost "throwing the baby out with the bathwater".

This seems to open the file without the macros and it's read only.
When testing this, I can't even see the macro module in VBA editor for the opened file..
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open Filename:=strFilepath & strFilename, ReadOnly:=True
Application.AutomationSecurity = msoAutomationSecurityByUI

Related

Excel Personal Macro Workbook reference Book1

I've finally figured out why my code was crashing. I have this set up as part of my Personal Macro Workbook so when I open a default Book1 I can run it. However, the issue is that since it's running the macro from the PMW the "Sheet.Copy After:=ThisWorkbook.Sheets(1)" is crashing.
How can I make it that the code below running from the PMW would copy the sheets into the default Book1?
Original code below;
Sub GetSheets()
Application.AutoRecover.Enabled = False
LInput:
PL = Application.InputBox("Threshold Report Path", "", "C:\Users\")
Path = PL
Filename = Dir(Path & "*.csv")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
ThisWorkbook refers to the workbook with the macro.
You can refer to it by name:
Sheet.Copy After:=Workbooks("Foo").Sheets(1)
I think you misunderstand the purpose of the Personal Macro Workbook; it shouldn't be auto-running anything. It's not a template. It's a place to store macros that you use often, so that instead of copying the macros to different workbooks, you can leave it in one place an run it from there.
I think what you want is a Personal Template that includes the template worksheet already, so nothing needs to be copied every time you create a new document.
Create a workbook, copy the worksheet in manually, and save it as a template. Avoid auto-run code in the template as well.
See links below for more information.
More information:
What you are trying to use:
Office.com : Create and save all your macros in a single workbook
Office.com : Create and save all your macros in a single workbook
What you should be using:
Office.com : Save a workbook as a template
Makeuseof : How to Quickly Create a Custom Excel Template to Save Time

VBA Overwrite Addin Workbook

So I have done some research but I can't get the feature I am looking for to work.
I am deploying a workbook to a lot of people on a network. I have an add-in on a network drive that all of the workbooks reference to. It works great when running the macros, no problem at all. My issue is updating the macros when people have one of the workbooks open. People leave things open for days sometimes, I know, cause I do the same thing sometimes.
I found this link, with the code below but I can't get it to work.
https://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment
The macro saves it as a .xlsm or a macro enabled workbook. It won't save it as a .xla
Sub DeployAddIn()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: To deploy finished/updated add-in to a network
' location as a read only file
Dim strAddinDevelopmentPath As String
Dim strAddinPublicPath As String
'Set development and public paths
strAddinDevelopmentPath = ThisWorkbook.Path & Application.PathSeparator
strAddinPublicPath = "F:\Addins" & Application.PathSeparator
'Turn off alert regarding overwriting existing files
Application.DisplayAlerts = False
'Save the add-in
With ThisWorkbook
'Save to ensure work is okay in case of a crash
.Save
'Save read only copy to the network (remove read only property
'save the file and reapply the read only status)
On Error Resume Next
SetAttr strAddinPublicPath & .Name, vbNormal
On Error Goto 0
.SaveCopyAs Filename:=strAddinPublicPath & .Name
SetAttr strAddinPublicPath & .Name, vbReadOnly
End With
'Resume alerts
Application.DisplayAlerts = True
End Sub
I've tried doing
.SaveAs FileFormat:=xlAddIn
Or
.`SaveAs FileFormat:=18`
Niether of those work. SaveCopyAs doesn't have a fileformat option.
Any suggestions or any other methods that might work?
You keep a copy of the XLAM on your local machine that you update, and then when you are ready to deploy it, you copy the XLAM to the server. Everyone else runs off a copy that is on the server (you dont want them to copy it to their local machine, btw) - The key is that when you copy it to the server, you have to set it to read-only in the file properties. You have to do that every time you copy it. Then you can overwrite it at any time.
Comment if you have any questions - I do this every day for work.

How to keep unrelated workbooks open in excel when running a macro

I am writing a macro in VB in excel 2013. The only remaining bug left is when the macro runs, all of the other workbooks i have open close, even ones unrelated to the macro. I do not want users of my macro to lose other tabs of excel they may have open if they run my program. I have tried using "Application.Visible = true" but it is not giving me the desired behavior. Is there a way to run a macro without affecting any other currently open workbooks? Or is it possible that something else I have written is overriding the Application.Visible method? If someone could provide a recommendation it would be much appreciated.
Application.Visible = false
Simply does what it says, set the application "Excel in your case" to invisible.
If you want to hide something specific you have to call this:
e.g Sheet
Sheets("Whatever").Visible = False
e.g Workbook
ActiveWorkbook.Windows(1).Visible = False
//ActiveWorkbook needs to have an reference to the specified Workbook which you get as an example if you store the opening of the workbook (ActiveWorkbook = Workbooks.Open )
It sounds like you have a line
Workbooks.close
(which will close all workbooks) when what you wanted to do was close the specific workbook with the macro. That should be
ThisWorkbook.Close

Macro to copy sheets into workbook stops after one sheet

I have a workbook where to speed computation (long story) I created a macro to copy out three of the sheets to another file and then another macro to copy them back it.
The macro to copy out works fine, however the macro to copy back in halts after copying in one sheet.
I searched within StackOverflow and found some similar questions but couldn't find an answer which worked. One post thought it was related to Office versions and one to a Shift key issue.
Here is the code:
Application.Calculation = xlCalculateManual
Application.ScreenUpdating = False
Application.DisplayAlerts = True
Application.EnableEvents = False
'
' Set up the workbooks
'
Set ThisWkb = ThisWorkbook
Fname = Application.GetOpenFilename( _
fileFilter:="Excel Macro Files, *.xlsm", _
Title:="Select the Storage File", _
MultiSelect:=False)
Set StorageWbk = Workbooks.Open(Fname)
'
MsgBox ("Beginning process - please click ok to any macro warning - you will see a confirmation when complete")
StorageWbk.Sheets("Sh A").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh B").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Sheets("Sh C").Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)
StorageWbk.Close
I sometimes find that if I then delete the new sheet and run the macro again it sometimes works and reads all three sheets in, but it also sometimes doesn't.
Any help is greatly appreciated.
Quoting both YowE3K and nbayly to give the correct answer from the comments directly here (I had to look for it):
Unhide your hidden Sheets and copy the three links at once using:
StorageWbk.Sheets(Array("Sh A", "Sh B", "Sh C")).Copy After:=ThisWkb.Sheets(ThisWkb.Sheets.Count)

Excel VBA code is causing my worksheets to appear as jargon

I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)