Excel VBA modules not working - excel

I'm using Excel 2010.
I added this code to my sheet and it works.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") > 0.5 Then
MsgBox "Discount too high"
End If
End Sub
But I want to create some way where I could create code and share that code across all excel workbooks on our network.
So I made a module and created an add-in and put the code INTO the module of the add-in.
I deleted the code from the sheet since I want the code to be accessible from the module.
I enabled the module.
And the VBA code doesn't work unless I put the code into the sheet. Please advise how I can just have this code in one file and have that code shared across all excel workbooks on our server.

You must put the Worksheet_Change under the Sheetname of the Excel Objects. So if it is Sheet1 that you want this procedure to run on, you will have to put it under Sheet1. If you want it to work on all sheets in the workbook, you will need to put it under ThisWorkbook AND you need to change the procedure name to Workbook_SheetChange

Related

Open Excel worksheet - run time error with other macros

I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub

Is it possible to prevent code available in personal.xlsb?

I've been self-teaching myself VBA for a couple of years now (working on it on and off to make my work life easier). I'm usually able to find answers to my questions on this forum, but not this one.
I have a lot of 'example' code in my own personal.xlsb. For instance I have a public sub with a modified messagebox with standard caption etc I always like to make visible, referred to as PublicSub_MyMsgBox
Often, the automation I create, will be used by my colleagues as well. I would create a specific workbook with specific buttons for a specific goal. I would copy the required code into a module in this shared workbook. The workbook would therefore have a module with the sub PublicSub_MyMsgBox as well.
Another sub in this shared workbook will have a line to call PublicSub_MyMsgBox.
How can I be sure that when I test a code created in this shared workbook, it does not use the PublicSub_MyMsgBox from my own personal.xlsb, but actually only uses the code from the workbook?
(to be able to verify that I have indeed copied all relevant code into the workbook for my colleagues to use as well).
Thank you very much for your help!
Linda
No code from personal.xlsb will run even if a reference to pers. workbook exists! You can be sure that no code from personal.xlsb will be run, instead of the one inside the workbook where the call is made.
Even if you added a reference to your personal.xlsb, for identic macro names VBA will choose the one in the workbook where the code runs. To check that, please, try the next scenario:
Create in your personal workbook the next macro:
Public Sub MyMsgBox()
MsgBox "Hello from Personal workbook!"
End Sub
Then create the next macro in a standard module of your new workbook:
Sub MyMsgBox()
MsgBox "Hello from This workbook!"
End Sub
Create a simple checking Sub, of course, in the new workbook, add a reference to your personal.xlsb workbook and run it:
Sub testMyMsgBox()
MyMsgBox
End Sub
It will run the Sub in your workbook. Now, comment the Sub from your workbook and run testMyMsgBox again. It will ran the procedure from personal.xlsb...

VBA Macro that Create Macro in Active Sheet

I found a way to highlight row when cell is selected. I would like to use it in every excel file I open but there is a problem. There are two steps. First, I have to create conditional formatting. Second, create a simple macro in the active sheet, not a normal module or personal.
My idea is that I create a personal macro that will do the first step (cond. form.) and then somehow create THE macro in the active sheet. But I don't know how.
Is it even possible?
This is the code I need to put into active sheeet module.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Calculate
End Sub
Thanks.

Running Excel Add-In Macro Based on Workbook Events

I want to create a macro that takes the user to the last previously active sheet. I need this macro to keep track of all previously active sheets, which it should be able to do automatically. I want to add this to an Excel add-in, since it needs to be compatible with all the worksheets that my company's using. The sheets themselves are not macro-enabled, so I can't have any code stored in the modules of ThisWorkbook.
I have tried out the examples that I've found on other websites. Unfortunately, all of them require using ThisWorkbook, which I won't be able to use. Is there a way to do this purely within an Excel add-in?
Public MyPrevSheet as String
Sub GoToPreviousSheet()
If Len(MyPrevSheet) > 0 Then
Sheets(MyPrevSheet).Activate
ActiveWorkbook.ActiveSheet.Previous
Else
MsgBox "You have not switched sheets yet since opening the file!"
End If
End Sub
Ideally, I would like for MyPrevSheet to automatically track the last active sheet in the file.
Update: I managed to make it work using Application Events. The idea is to create a class module that can then be assigned the Application.
Here are some useful links that helped me: http://www.cpearson.com/excel/AppEvent.aspx
https://www.jkp-ads.com/Articles/buildexceladdin05.asp

VBA code for cell formating linked to another sheet in a workbook

I have a code which looks like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Me.sheets("WAM Data").Range("BY5:HW35").Interior.Color = Me.sheets("WAM
Exception").Range("BO7:HM37").Interior.Color
End Sub
But its giving error in code.
What I want to do is, change the cell formatting (color) of "BY5 to HW35" as "BO7:HM37".
If anyone could help that would be great.
You can delete me. and use only Sheets..., or use ActiveWorkbook.Sheets.. to refer to the activeworkbook, or ThisWorkbook.Sheets... to refer to the workbook the macro is run from, or Workbooks("name").Sheets... to choose whichever workbook you want from the ones you have open.
However, your macro will just run on first click of a mouse, regardless where that happens on your spreadsheet, with no conditions add to it... is that what you want to do with your code?

Resources